Python script to copy text to clipboard

I just need a python script that copies text to the clipboard.

After the script is executed, the text is output, which will be inserted into another source. Is it possible to write a python script that does this work?

+70
python clipboard pyperclip
Jun 16 2018-12-12T00:
source share
11 answers

See Pyperclip . Example (taken from the Pyperclip website):

import pyperclip pyperclip.copy('The text to be copied to the clipboard.') spam = pyperclip.paste() 

Also see Xerox . But he seems to have more dependencies.

+91
Jun 16 '12 at 12:35
source share

On mac, I use this function.

 import os data = "hello world" os.system("echo '%s' | pbcopy" % data) 

It will copy "hello world" to the clipboard.

+32
Jun 28 '13 at 18:27
source share

Use Tkinter:

stack overflow

 try: from Tkinter import Tk except ImportError: from tkinter import Tk r = Tk() r.withdraw() r.clipboard_clear() r.clipboard_append('i can has clipboardz?') r.update() # now it stays on the clipboard after the window is closed r.destroy() 

(Original author: https://stackoverflow.com/users/449571/atomizer )

+27
Aug 24 '14 at 21:25
source share

Pyperclip seems to fit the task.

+12
Jun 16 '12 at 12:35
source share

To use your own Python directories, use:

 import subprocess def copy2clip(txt): cmd='echo '+txt.strip()+'|clip' return subprocess.check_call(cmd, shell=True) 

Then use:

 copy2clip('This is on my clipboard!') 

to call a function.

+10
Dec 08 '16 at 0:47
source share

This is the only way that worked for me using Python 3.5.2 plus it is easiest to implement using the standard PyData package

Open the https://stackoverflow.com/users/4502363/gadi-oron answer (I completely copied it) from How to copy a string to the clipboard in Windows using Python?

 import pandas as pd df=pd.DataFrame(['Text to copy']) df.to_clipboard(index=False,header=False) 

I wrote a small wrapper for it, which I entered in my ipython profile <3

+4
Dec 16 '16 at 19:38
source share

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() 
+3
Feb 25 '15 at 15:29
source share

I am trying to use 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 
+3
Jun 28 '16 at 6:34
source share

One more answer for improvement: https://stackoverflow.com/a/412560/2/212612/ and ( https://stackoverflow.com/questions/464877/how-to-create-a-string-in-java/41282951#41280077

Tkinter is good because it is either included in Python (Windows) or easy to install (Linux), and therefore requires minimal dependencies for the end user.

Here I have a “full-blown” example that copies arguments or standard input to the 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() 

These display cases:

  • Tk import through Py2 and Py3
  • raw_input and print() compatibility
  • hide the Tk root window if necessary
  • waiting for Linux to be released in two different ways.
+2
Nov 04 '15 at 13:35
source share

PyQt5:

 from PyQt5.QtWidgets import QApplication from PyQt5 import QtGui from PyQt5.QtGui import QClipboard import sys def main(): app=QApplication(sys.argv) cb = QApplication.clipboard() cb.clear(mode=cb.Clipboard ) cb.setText("Copy to ClipBoard", mode=cb.Clipboard) sys.exit(app.exec_()) if __name__ == "__main__": main() 
+1
Nov 09 '15 at 11:00
source share

This is a modified version of @Martin Thoma's answer for GTK3 . I found that the original solution led to the process not ending and my terminal freezing when I called the script. Changing the script to the next solution to the problem 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 which client clipboardText will be bound to, in this script it is assigned to the parameter called with the script.

In a new installation of ubuntu 16.04, I found that I had to install the python-gobject package so that it worked without a module import error.

+1
Apr 25 '16 at 11:34
source share



All Articles