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.
Answer from robert on Stack OverflowSee 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.
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.
How to implement copying to the clipboard in Python?
How to copy selected text to clipboard?
python - How do I copy a string to the clipboard? - Stack Overflow
Script to copy lines of text?
Videos
How can I select text and copy it to the clipboard?
The selection part actually works. Either with "ctrl+a" or a triple left mouseclick. But I can't "ctrl+c" it into the clipboard.
I tried it with pyautogui like a normal user would, but it doesn't work.
pyautogui.keyDown('ctrl')
pyautogui.keyDown('c')
pyautogui.keyUp('c')
pyautogui.keyUp('ctrl')
Pyperclip works to read out the content of the clipboard, but it seems like it can only copy strings to the clipbard, that need to be added manually like this:
Pyperclip.copy('test')
But I need to copy my text selection to the clipboard.
edit: if you know any other way to store selected text in a variable, that would be alright too
Actually, pywin32 and ctypes seem to be an overkill for this simple task. tkinter is a cross-platform GUI framework, which ships with Python by default and has clipboard accessing methods along with other cool stuff.
If all you need is to put some text to system clipboard, this will do it:
from tkinter import Tk # in Python 2, use "Tkinter" instead
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()
And that's all, no need to mess around with platform-specific third-party libraries.
If you are using Python 2, replace tkinter with Tkinter.
I didn't have a solution, just a workaround.
Windows Vista onwards has an inbuilt command called clip that takes the output of a command from command line and puts it into the clipboard. For example, ipconfig | clip.
So I made a function with the os module which takes a string and adds it to the clipboard using the inbuilt Windows solution.
import os
def addToClipBoard(text):
command = 'echo ' + text.strip() + '| clip'
os.system(command)
# Example
addToClipBoard('penny lane')
# Penny Lane is now in your ears, eyes, and clipboard.
As previously noted in the comments however, one downside to this approach is that the echo command automatically adds a newline to the end of your text. To avoid this you can use a modified version of the command:
def addToClipBoard(text):
command = 'echo | set /p nul=' + text.strip() + '| clip'
os.system(command)
If you are using Windows XP it will work just following the steps in Copy and paste from Windows XP Pro's command prompt straight to the Clipboard.
» pip install clipboard