Python 操作 Windows 粘贴板,实现Ctrl + C 和 Ctrl + V 功能
需要先以管理员权限安装PyWin
代码:
common.py
import win32clipboard as w
import win32con
def getText():
w.OpenClipboard()
d = w.GetClipboardData(win32con.CF_TEXT)
w.CloseClipboard()
return d
def setText(aString):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_TEXT, aString)
w.CloseClipboard()
print "I set '%s' for clipboard." %aString
main.py
import common
common.setText("Hi,Jeff")
content = common.getText()
print "The content in clipboard is: '%s'." %content
运行结果为:
I set 'Hi,Jeff' for clipboard.
The content in clipboard is: 'Hi,Jeff'.
有关windows 粘贴板的相关函数,可以参考:
http://msdn.microsoft.com/en-us/library/ms649051(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ff729168(v=VS.85).aspx
参考自:http://woshao.com/article/d117cecade2211df9677000c295b2b8d/