I just need a python script that copies text to the clipboard.
After the script gets executed i need the output of the text to be pasted to another source. Is it possible to write a python script that does this job?
38 Answers
See Pyperclip. Example (taken from Pyperclip site):
import pyperclip
pyperclip.copy('The text to be copied to the clipboard.')
spam = pyperclip.paste()Also, see Xerox. But it appears to have more dependencies.
4On macOS, use subprocess.run to pipe your text to pbcopy:
import subprocess
data = "hello world"
subprocess.run("pbcopy", universal_newlines=True, input=data)It will copy "hello world" to the clipboard.
5To use native Python directories, use:
import subprocess
def copy2clip(txt): cmd='echo '+txt.strip()+'|clip' return subprocess.check_call(cmd, shell=True)on Mac, instead:
import subprocess
def copy2clip(txt): cmd='echo '+txt.strip()+'|pbcopy' return subprocess.check_call(cmd, shell=True)Then use:
copy2clip('This is on my clipboard!')to call the function.
8PyQt5:
from PyQt5.QtWidgets import QApplication
import sys
def main(): app = QApplication(sys.argv) cb = QApplication.clipboard() cb.clear(mode=cb.Clipboard ) cb.setText("Copy to ClipBoard", mode=cb.Clipboard) # Text is now already in the clipboard, no need for further actions. sys.exit()
if __name__ == "__main__": main() 2 GTK3:
#!/usr/bin/python3
from gi.repository import Gtk, Gdk
class Hello(Gtk.Window): def __init__(self): super(Hello, self).__init__() clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) clipboard.set_text("hello world", -1) Gtk.main_quit()
def main(): Hello() Gtk.main()
if __name__ == "__main__": main() 1 One more answer to improve on: and (Tkinter).
Tkinter is nice, because it's either included with Python (Windows) or easy to install (Linux), and thus requires little dependencies for the end user.
Here I have a "full-blown" example, which copies the arguments or the standard input, to clipboard, and - when not on Windows - waits for the user to close the application:
import sys
try: from Tkinter import Tk
except ImportError: # welcome to Python3 from tkinter import Tk raw_input = input
r = Tk()
r.withdraw()
r.clipboard_clear()
if len(sys.argv) < 2: data = sys.stdin.read()
else: data = ' '.join(sys.argv[1:])
r.clipboard_append(data)
if sys.platform != 'win32': if len(sys.argv) > 1: raw_input('Data was copied into clipboard. Paste and press ENTER to exit...') else: # stdin already read; use GUI to exit print('Data was copied into clipboard. Paste, then close popup to exit...') r.deiconify() r.mainloop()
else: r.destroy()This showcases:
- importing Tk across Py2 and Py3
raw_inputandprint()compatibility- "unhiding" Tk root window when needed
- waiting for exit on Linux in two different ways.
This is an altered version of @Martin Thoma's answer for GTK3. I found that the original solution resulted in the process never ending and my terminal hung when I called the script. Changing the script to the following resolved the issue for me.
#!/usr/bin/python3
from gi.repository import Gtk, Gdk
import sys
from time import sleep
class Hello(Gtk.Window): def __init__(self): super(Hello, self).__init__() clipboardText = sys.argv[1] clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) clipboard.set_text(clipboardText, -1) clipboard.store()
def main(): Hello()
if __name__ == "__main__": main()You will probably want to change what clipboardText gets assigned to, in this script it is assigned to the parameter that the script is called with.
On a fresh ubuntu 16.04 installation, I found that I had to install the python-gobject package for it to work without a module import error.
I try this clipboard 0.0.4 and it works well.
import clipboard
clipboard.copy("abc") # now the clipboard content will be string "abc"
text = clipboard.paste() # text will have the content of clipboard 2