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?
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?
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.
Currently only handles plaintext. On Windows, no additional modules are needed. On Mac, this module makes use of the pbcopy and pbpaste commands, which should come with the os. On Linux, this module makes use of the xclip or xsel commands, which should come with the os. Otherwise run "sudo apt-get install xclip" or "sudo apt-get install xsel" (Note: xsel does not always seem to work.) Otherwise on Linux, you will need the gtk or PyQt4 modules installed. So check for dependencies on your platform/system.
Jun 27, 2023 at 4:22
On macOS, use subprocess.run to pipe your text to pbcopy:
import subprocess
data = "hello world"
subprocess.run("pbcopy", text=True, input=data)
It will copy "hello world" to the clipboard.
echo 'CUSTOM STRING' | xsel --clipboard --input . Usage example with python subprocess.Popen(['/bin/sh', '-c', f'echo "{123}" | xsel --clipboard --input'])
Feb 25, 2021 at 2:12
To 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.
PyQt5:
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()
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()
I try this clipboard 0.0.4 and it works well.
https://pypi.python.org/pypi/clipboard/0.0.4
import clipboard
clipboard.copy("abc") # now the clipboard content will be string "abc"
text = clipboard.paste() # text will have the content of clipboard
pyperclip. Use that instead.
Jul 19, 2016 at 20:07
One more answer to improve on: https://stackoverflow.com/a/4203897/2804197 and https://stackoverflow.com/a/25476462/1338797 (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:
raw_input and print() compatibilitysudo apt-get install python-tk, BTW.
May 10, 2016 at 12:45
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.