简体   繁体   English

在python中将文件存储在剪贴板中

[英]Storing a file in the clipboard in python

Is there a way to use the win32clipboard module to store a reference to a file in the windows clipboard in python. 有没有办法使用win32clipboard模块在python中的Windows剪贴板中存储对文件的引用。 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. 如果我将'png'文件拖放到OneNote中,或者我复制文件然后将其粘贴到OneNote中,这似乎可以保持透明度。 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. 我的研究表明它可能涉及win32clipboard.CF_HDrop属性,但我不确定。

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. 所以,总而言之,我的目标是获得一些我可以单击的python代码,并使用我桌面上名为'img.png'的特定文件。 The result is that 'img.png' gets stored in the clipboard and can be pasted into other programs. 结果是'img.png'存储在剪贴板中并可以粘贴到其他程序中。 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 编辑:这个页面似乎表明有一种方法使用win32clipboard.CF_HDrop以某种方式: http ://timgolden.me.uk/pywin32-docs/win32clipboard__GetClipboardData_meth.html

It says "CF_HDROP" is associated with "a tuple of Unicode filenames" 它说“CF_HDROP”与“一个Unicode文件名元组”相关联

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. 虽然我远非Windows专家,但这种“首选DropEffect”似乎很可疑。 I would try first with FileNameW, though, since this might do the job for you (I don't have OneNote installed, sorry). 我会首先尝试使用FileNameW,因为这可能会为您完成工作(我没有安装OneNote,抱歉)。 It seems it expects as data only the full pathname encoded as 'utf-16-le' with a null character (ie encoded as '\\0\\0' ) at the end. 它似乎只希望数据只是在末尾编码为'utf-16-le'的完整路径名,并带有空字符(即编码为'\\0\\0' )。

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

Grab the windows binaries for PythonMagick 抓取PythonMagick的Windows二进制文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM