Python script to copy text to clipboard [duplicate] Python script to copy text to clipboard [duplicate] python python

Python script to copy text to clipboard [duplicate]


See Pyperclip. Example (taken from Pyperclip site):

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

Also, see Xerox. But it appears to have more dependencies.


On 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.


Use Tkinter:

https://stackoverflow.com/a/4203897/2804197

try:    from Tkinter import Tkexcept ImportError:    from tkinter import Tkr = 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 closedr.destroy()

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