pywin32 for py2.7 下载:http://pan.baidu.com/s/1i5gjs1J
1、文本内容
# coding=utf-8
import win32clipboard as w
import win32con
import win32api
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()
if __name__ == '__main__':
setText("test") # 将“test”写入剪切板
# 自动粘贴剪切板中的内容
win32api.keybd_event(17, 0, 0, 0) # ctrl的键位码是17
win32api.keybd_event(86, 0, 0, 0) # v的键位码是86
win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放按键
win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(13, 0, 0, 0) # Enter的键位码是13
win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
# coding=utf-8
import win32con
import win32clipboard
import win32gui
import win32api
import traceback
from PIL import Image
from cStringIO import StringIO
def setImage(data): # 写入剪切板
win32clipboard.OpenClipboard()
try:
# Unicode tests
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32con.CF_DIB, data)
except:
traceback.print_exc()
finally:
win32clipboard.CloseClipboard()
if __name__ == '__main__':
img = Image.open('test.jpg')
output = StringIO()
img.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:]
output.close()
setImage(data)
pwin = win32gui.FindWindow(None, u'测试') # 获取窗口句柄
win32gui.ShowWindow(pwin, win32con.SW_RESTORE)
win32gui.SetActiveWindow(pwin)
win32gui.SetForegroundWindow(pwin)
rect = win32gui.GetWindowRect(pwin) # 获取窗口位置
x = (rect[0] + rect[2]) / 2
y = rect[3] - 50
win32api.SetCursorPos((x, y)) # 设置鼠标位置
win32api.mouse_event(0x0002, 0, 0, 0, 0) # 模拟鼠标按下
win32api.mouse_event(0x0004, 0, 0, 0, 0) # 模拟鼠标弹起
win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0) # ctrl的键位码是17
win32api.keybd_event(86, 0, 0, 0) # v的键位码是86
win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放按键
win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放按键
win32api.keybd_event(13, 0, 0, 0) # Enter的键位码是13
win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放按键