Pywin32 to achieve Windows simulation mouse and keyboard action
- 2020-04-02 13:37:17
- OfStack
Because a batch of software to deal with a batch of eps file, so to simulate the mouse and keyboard action, so that it can be automated operation.
#-*-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" Wen tai carved painting 2009[] - [ No title -1]"
w1hd=win32gui.FindWindow(0,wdname1)
print w1hd
w2hd=win32gui.FindWindowEx(w1hd,None,None,None)
print w2hd
def aotohelper_wt(i):
# Get window focus
win32gui.SetForegroundWindow(w2hd)
# shortcuts 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) # Release the button
win32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0)
# shortcuts I
win32api.keybd_event(73,0,0,0) # I
win32api.keybd_event(73,0,win32con.KEYEVENTF_KEYUP,0)
time.sleep(0.2)
wdname3=u" Open the "
w3hd=win32gui.FindWindow(None,wdname3) # "Open" Handle to the window
#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
# Analog shortcut 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) # Release the button
win32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0)
time.sleep(0.1)
# Simulate mouse operation
win32api.SetCursorPos([30,150]) # Set a position for mouse focus
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)
# Analog shortcut F7( Limit to observe )
win32api.keybd_event(118,0,0,0) # F7
win32api.keybd_event(118,0,win32con.KEYEVENTF_KEYUP,0) # Release the button
time.sleep(0.2)
# using PIL screenshots
path="C:\Users\LY\Desktop\pic\"
filename="YC-"+str(i)+".jpg"
im=ImageGrab.grab()
im.save(path+filename)
# Analog shortcut F8( Return to original page size )
win32api.keybd_event(119,0,0,0) # F8
win32api.keybd_event(119,0,win32con.KEYEVENTF_KEYUP,0) # Release the button
# Simulated keyboard events delete
win32api.keybd_event(46,0,0,0) # Delete
win32api.keybd_event(46,0,win32con.KEYEVENTF_KEYUP,0) # Release the button
time.sleep(1)
for i in range(2,85):
aotohelper_wt(i)
print i
print "work done!"