user2283466
user2283466

Reputation: 1

I can use clip.exe to copy files to the clipboard in Python 2.7, but it will not paste into Windows Explorer

I am trying to put files into the Windows clipboard from a Python program so that the user can simply paste the files to the location of the choice with Windows Explorer. The below code is adding the files to the clipboard and the files can be pasted with cmd.exe, but in Windows Explorer "Paste" is grayed out and Ctrl+V will not paste anything.

os.system("dir %s | CLIP.exe" % self.clip_folder)

Upvotes: 0

Views: 1050

Answers (2)

Minu
Minu

Reputation: 438

Although, in my case of Python 3.7

I can make it possible with with pythoncom and win32clipboard.

Here is my sample code and github repo for it. You can get more information from Microsoft home page.

stg = pythoncom.STGMEDIUM()
stg.set(pythoncom.TYMED_HGLOBAL, buf)
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
try:
    print(stg)
    print(stg.data)
    win32clipboard.SetClipboardData(win32clipboard.CF_HDROP, stg.data)
    print("clip_files() succeed")
finally:
    win32clipboard.CloseClipboard()

Upvotes: 2

vcsjones
vcsjones

Reputation: 141678

clip.exe does not put files on to the clipboard. Only text. If you opened up notepad, you would probably paste the output of dir %s.

You'll need to find a Python package that can do this for you, or call the Win32 function SetClipboardData with the CF_HDROP type to specify a list of files you want on the clipboard.

Upvotes: 1

Related Questions