Python中带有剪贴板的问题 [英] Troubles with clipboard in Python

查看:67
本文介绍了Python中带有剪贴板的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习Python,我想编写一些脚本来帮助我的工作。这个想法是: True:从剪贴板读取一些字符串,对其进行修改,然后将其返回剪贴板,然后 sleep 。因此,我可以将修改后的数据粘贴到任何地方。

I'm learning Python right now and I want to write some script that will helps me with work. The idea is: while True: read some string from clipboard, modify it then return it into the clipboard then sleep. So I can paste modified data to anywhere.

现在,我不得不使用 win32clipboard 模块。我正在使用此代码:

Now I'm stuck in using win32clipboard module. I'm using this code:

import win32clipboard

def openClipboard():
    win32clipboard.OpenClipboard()

def closeClipboard():
    try:
        win32clipboard.CloseClipboard()
    except Exception as e:
        print(e)

def getClipboardData():
    if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_TEXT):
        return win32clipboard.GetClipboardData()
    else:
        return None

def setClipboardData(data):
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardData(win32clipboard.CF_TEXT, data)

#assume that I copied '000'

openClipboard()
data = getClipboardData()
print(data) //output: 000, so it's ok
closeClipboard()

openClipboard()
win32clipboard.EmptyClipboard()
setClipboardData(data + '123')
closeClipboard()

openClipboard()
data = getClipboardData()
print(data) //output: 0 0 0 1 2 3, but wtf? o_0
closeClipboard()

我不明白为什么第二个输出中有空格?

I can't understand why there are spaces in the second output?

推荐答案

ralu ,谢谢。我得到你的答案的同时。我应该使用 win32clipboard.SetClipboardText(data)而不是 win32clipboard.SetClipboardData(win32clipboard.CF_TEXT,data)

ralu, thanks. I've got it at the same time I've got your answer. I should've use win32clipboard.SetClipboardText(data) instead of win32clipboard.SetClipboardData(win32clipboard.CF_TEXT, data).

这篇关于Python中带有剪贴板的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆