Maybe this will help:
import os
def addToClipBoard(text):
command = 'echo ' + text.strip() + '| clip'
os.system(command)
lst = [...] #your list
addToClipBoard(str(lst))
Answer from Sid on Stack OverflowMaybe this will help:
import os
def addToClipBoard(text):
command = 'echo ' + text.strip() + '| clip'
os.system(command)
lst = [...] #your list
addToClipBoard(str(lst))
Here's a quick example for macOS based on this) that doesn't require additional packages.
import subprocess
list_to_put_on_clipboard = [(('infection',), 548), (('data',), 543), (('plant',), 514), (('host',), 513), (('species',), 489)]
list_as_str = '\n'.join([str(item) for item in list_to_put_on_clipboard])
subprocess.run("pbcopy", universal_newlines=True, input=list_as_str)
Here is the result when copying to excel
Notes:
- Not all types may support conversion to str
- You may want to use a different delimiter than '\n' above, but '\n' produces what I think is the desired Excel output in this case
For other operating systems, according to comments in the above link, instead of "pbcopy" use:
- "xclip" for Linux
- "clip" for Windows
Copy to system clipboard
Copying/Pasting list with Pyperclip
How to implement copying to the clipboard in Python?
Is there a way to convert the clipboard content into list in the run in Python? - Stack Overflow
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.
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.
Hi! I know pyperclip can be used to copy/paste strings like so:
import pyperclip
pyperclip.copy('some string here')
pyperclip.paste()
'some string here'Is there a way to copy and paste a list of numbers? I don't know if pyperclip has this function. Let's say I have a list here:
listOfNum = [100, 200, 300, 400, 500]
I want to use pyperclip (or something similar) to copy listOfNum to clipboard and paste to an excel doc. I'm looking to paste a number on each row, like so:
| A | B |
|---|---|
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
| 4 | 400 |
| 5 | 500 |
» pip install clipboard
Are you trying to simply read the clipboard and split the contents? On linux:
import pyperclip
print pyperclip.paste().split(" ")
Windows:
from Tkinter import Tk
root = Tk()
print Tk.clipboard_get(root).split(" ")
OS X:
from AppKit import *
pb = NSPasteboard.generalPasteboard()
pbstring = pb.stringForType_(NSStringPboardType)
print pbstring.split(" ")
[EDIT]
It looks as though pyperclip should work under windows and OS X as well, so that seems to be a portable solution. Ref: https://pypi.python.org/pypi/pyperclip/
From your comments, it's become clear that you're not actually asking how to read the contents of the clipboard; you're just asking how to parse some data that happen to have come from the clipboard (presumably by you/the user manually pasting it).
So the real question is: how do you parse something that looks kind of like a list literal from Python source?
If it actually is a list literal from Python source, you use the literal_eval function from the ast module:
>>> import ast
>>> s = "['a',2, (4,'b')]"
>>> lst = ast.literal_eval(s)
>>> print(lst)
['a', 2, (4, 'b')]
However, if you don't actually know where the data come from, there's a good chance it's actually JSON (which you should parse with json.loads), YAML, or some other format that's superficially similar to Python literals but not identical. If you try to parse JSON with literal_eval, it'll work on your initial tests, and then fail in real life because of a false instead of False or similar. That's why it's important to actually know what kind of data you have. If it came from Python source code, or from some Python script calling print with a list, that's what literal_eval is for.
Hi!
I need help.
I wants to copy a file into the clipboard so that I can paste it using right click and paste.
I don't wants to copy content of the file but file itself. I wants to use right click or {CTRL + V} to paste file at desired location. I'm unable to find anything on google
Can any please help me?