用pywin32实现windows模拟鼠标及键盘动作
因为要批量用某软件处理一批eps文件,所以要模拟鼠标及键盘动作,使其能够自动化操作。
#-*-coding:utf-8-*-
import os
import time
import win32gui
import win32api
import win32con
from pil import imagegrab
#os.startfile("d:\\artcut6\\prog\\artcut6.exe")
#time.sleep(1)
wdname1=u"文泰刻绘2009[] - [无标题-1]"
w1hd=win32gui.findwindow(0,wdname1)
print w1hd
w2hd=win32gui.findwindowex(w1hd,none,none,none)
print w2hd
def aotohelper_wt(i):
#获取窗口焦点
win32gui.setforegroundwindow(w2hd)
#快捷键alt+f
win32api.keybd_event(18,0,0,0) # alt
win32api.keybd_event(70,0,0,0) # f
win32api.keybd_event(70,0,win32con.keyeventf_keyup,0) #释放按键
win32api.keybd_event(18,0,win32con.keyeventf_keyup,0)
#快捷键i
win32api.keybd_event(73,0,0,0) # i
win32api.keybd_event(73,0,win32con.keyeventf_keyup,0)
time.sleep(0.2)
wdname3=u"打开"
w3hd=win32gui.findwindow(none,wdname3) #”打开“ 窗口句柄
#print w3hd
#win32gui.movewindow(w3hd, 50, 50, 300, 200, true)
if i<=9:
msg="yc-00"+str(i)
elif 10<=i<=99:
msg="yc-0"+str(i)
else:
msg="yc-"+str(i)
edithd=win32gui.findwindowex(w3hd,none,"edit",none)
win32api.sendmessage(edithd,win32con.wm_settext,none,msg)
time.sleep(0.1)
#btnhd=win32gui.findwindowex(w3hd,none,"button",none)
#print btnhd
#模拟快捷键alt+o
win32api.keybd_event(18,0,0,0) # alt
win32api.keybd_event(79,0,0,0) # o
win32api.keybd_event(79,0,win32con.keyeventf_keyup,0) #释放按键
win32api.keybd_event(18,0,win32con.keyeventf_keyup,0)
time.sleep(0.1)
#模拟鼠标操作
win32api.setcursorpos([30,150]) #为鼠标焦点设定一个位置
win32api.mouse_event(win32con.mouseeventf_leftdown,0,0,0,0)
win32api.setcursorpos([500,500])
win32api.mouse_event(win32con.mouseeventf_leftup,0,0,0,0)
time.sleep(0.1)
#模拟快捷键f7(极限观察)
win32api.keybd_event(118,0,0,0) # f7
win32api.keybd_event(118,0,win32con.keyeventf_keyup,0) #释放按键
time.sleep(0.2)
#利用pil截屏
path="c:\\users\\ly\\desktop\\pic\\"
filename="yc-"+str(i)+".jpg"
im=imagegrab.grab()
im.save(path+filename)
#模拟快捷键f8(回到原页面大小)
win32api.keybd_event(119,0,0,0) # f8
win32api.keybd_event(119,0,win32con.keyeventf_keyup,0) #释放按键
#模拟键盘事件delete
win32api.keybd_event(46,0,0,0) # delete
win32api.keybd_event(46,0,win32con.keyeventf_keyup,0) #释放按键
time.sleep(1)
for i in range(2,85):
aotohelper_wt(i)
print i
print "work done!"
上一篇: Django中使用group_by的方法
下一篇: python动态参数用法实例分析