7

Is there a way to use the win32clipboard module to store a reference to a file in the windows clipboard in python. My goal is to paste an image in a way that allows transparency. If I drag and drop a 'png' file into OneNote or I copy the file and then paste it into OneNote, this seems to preserve transparency. As far as I can tell, the clipboard can't store transparent images which is why it has to be a reference to a file.

My research suggests that it might involve the win32clipboard.CF_HDrop attribute but I'm not sure.

So, just to summarize, my goal is to have a bit of python code which I can click and which uses a specific file on my Desktop named 'img.png' for instance. The result is that 'img.png' gets stored in the clipboard and can be pasted into other programs. Essentially, the same behavior as if I selected the file on the Desktop myself, right-clicked and selected 'Copy'.

EDIT: This page seems to suggest there is a way using win32clipboard.CF_HDrop somehow: http://timgolden.me.uk/pywin32-docs/win32clipboard__GetClipboardData_meth.html

It says "CF_HDROP" is associated with "a tuple of Unicode filenames"

Henry B.
  • 477
  • 2
  • 5
  • 8

2 Answers2

1
from PythonMagick import Image
Image("img.png").write("clipboard:") 

Grab the windows binaries for PythonMagick

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Doesn't seem to work. When I paste the image, it no longer has a transparent background. – Henry B. Jun 13 '12 at 11:40
  • So you are saying that the copy paste operation actually works, but your image is somehow corrupted? – Burhan Khalid Jun 13 '12 at 11:52
  • I don't think 'corrupted' is the right word. As mentioned in my question, pasting an image that has a transparent background is what concerns me. I think the issue is the clipboard can both store image information directly or some kind of reference to a file. If the image data is stored then transparency is lost. If the reference to the file is stored, windows seems to know how to keep the transparency when inserting a 'png' into a file. – Henry B. Jun 13 '12 at 12:25
  • The clipboard can store multiple file formats simultaneously, and programs retrieve the first format that matches what they ask for. Sounds like your clipboard is storing and delivering a version in a format that doesn't support transparency; but it should be able to store the format that supports tarnsparency as well. Perhaps this can guide you to retrieve the right version. – alexis Jul 13 '12 at 22:35
  • This is the easiest way. The only thing is to do a pip. – Yiping May 29 '21 at 15:27
1

I write this as an answer, although it's just a step that might help you, because comments don't have a lot of formatting options.

I wrote this sample script:

import win32clipboard as clp, win32api

clp.OpenClipboard(None)

rc= clp.EnumClipboardFormats(0)
while rc:
    try: format_name= clp.GetClipboardFormatName(rc)
    except win32api.error: format_name= "?"
    print "format", rc, format_name
    rc= clp.EnumClipboardFormats(rc)

clp.CloseClipboard()

Then I selected an image file in explorer and copied it; then, the script reports the following available clipboard formats:

format 49161 DataObject
format 49268 Shell IDList Array
format 15 ?
format 49519 DataObjectAttributes
format 49292 Preferred DropEffect
format 49329 Shell Object Offsets
format 49158 FileName
format 49159 FileNameW
format 49171 Ole Private Data

This “Preferred DropEffect” seems suspicious, although I'm far from a Windows expert. I would try first with FileNameW, though, since this might do the job for you (I don't have OneNote installed, sorry). It seems it expects as data only the full pathname encoded as 'utf-16-le' with a null character (i.e encoded as '\0\0') at the end.

tzot
  • 92,761
  • 29
  • 141
  • 204