3

So I want to be able to use a python script to copy the contents of a folder and then be able to paste those contents to a location of my choosing i.e. text file, browser, etc... I came across this solution for copying text to the clipboard, but when i implement this solution I am not able to paste anything. I am using python 3.4. Below is code i am using:

import os 
import tkinter as tk
import tkinter.filedialog

r = tk.Tk()
r.withdraw()
photo_path= tkinter.filedialog.askdirectory(title='Which folder would you like to copy the contents from?', initialdir='/')

# Get list of filenames in current directory
file_list=[]

for filename in os.listdir(photo_path):
    if os.path.splitext(filename)[1]=='.JPG':
        file_list.append(os.path.splitext(filename)[0])
    else: pass

file_search='code:('+' OR '.join(file_list)+')'

r.clipboard_clear()
r.clipboard_append(file_search)
r.destroy()
Community
  • 1
  • 1
Grr
  • 15,553
  • 7
  • 65
  • 85
  • To get access to Linux's (if you meant the OS's clipboard, not Tkinter's) you use subprocess.Popen to get access to xclip, xsel or xclipboard, depending on the flavor of Linux. On OSX use subprocess for pbcopy or pbpaste. Use the built in "find" command to see which is installed. –  Jul 29 '15 at 01:37
  • @CurlyJoe Sorry I wasn't clear on that. I am running this in windows 7. – Grr Jul 29 '15 at 13:47
  • Does this answer your question? [How do I read text from the clipboard?](https://stackoverflow.com/questions/101128/how-do-i-read-text-from-the-clipboard) – jdhao Feb 03 '21 at 09:19

2 Answers2

3

If you don't use the clipboard content before your script ends, it is discarded. Keep it running until you no longer need the clipboard content. The following program will keep '1234' in the clipboard for 10 seconds. If you don't paste it within that time, it is lost. If you do paste it within that time, it will remain in the clipboard even after the program ends.

import tkinter as tk

r = tk.Tk()
r.withdraw()

r.clipboard_clear()
r.clipboard_append('1234')
r.after(10000, lambda: r.destroy())
r.mainloop()
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • I tried implementing this, but i still have the same problem. Script runs, pulls up window for selecting folder, I select folder and click ok, but when I right click anywhere paste is still grayed out. – Grr Jul 29 '15 at 13:52
  • @Grr - I ran your posted program with the following changes: changed the `'.JPG'` to `'.txt'`, commented out `r.withdraw()` for convenience (so I could close the program more easily), and changed `r.destroy()` to `r.mainloop()`. It worked perfectly. Please post your implementation so that I can attempt to reproduce your issue. – TigerhawkT3 Jul 29 '15 at 17:04
  • It worked after replacing `r.destroy()` with `r.mainloop()`. Thank you. Not sure why your original code didn't work, I may have to work back through that and see where it was failing. – Grr Jul 30 '15 at 18:00
-2

How do I read text from the (windows) clipboard from python?

"Worth noting, in py34, win7, SetClipboardText did not work without a preceding call to EmptyClipboard"

import win32clipboard

# set clipboard data
win32clipboard.OpenClipboard()
win32clipboard.SetClipboardText('testing 123')
win32clipboard.CloseClipboard()

# get clipboard data
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
print data
Community
  • 1
  • 1
cmglorioso
  • 65
  • 3
  • 1
    The OP isn't using `pywin32`. Also, if you want to link to another question without adding any of your own information to it, you can just link it in a comment. – TigerhawkT3 Jul 28 '15 at 20:16