Home Backend Development Python Tutorial 用pywin32实现windows模拟鼠标及键盘动作

用pywin32实现windows模拟鼠标及键盘动作

Jun 16, 2016 am 08:44 AM

因为要批量用某软件处理一批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  msg="YC-00"+str(i)
 elif 10  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!"

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How Do I Use Beautiful Soup to Parse HTML? How Do I Use Beautiful Soup to Parse HTML? Mar 10, 2025 pm 06:54 PM

How Do I Use Beautiful Soup to Parse HTML?

How to Use Python to Find the Zipf Distribution of a Text File How to Use Python to Find the Zipf Distribution of a Text File Mar 05, 2025 am 09:58 AM

How to Use Python to Find the Zipf Distribution of a Text File

Image Filtering in Python Image Filtering in Python Mar 03, 2025 am 09:44 AM

Image Filtering in Python

How to Perform Deep Learning with TensorFlow or PyTorch? How to Perform Deep Learning with TensorFlow or PyTorch? Mar 10, 2025 pm 06:52 PM

How to Perform Deep Learning with TensorFlow or PyTorch?

Serialization and Deserialization of Python Objects: Part 1 Serialization and Deserialization of Python Objects: Part 1 Mar 08, 2025 am 09:39 AM

Serialization and Deserialization of Python Objects: Part 1

How to Implement Your Own Data Structure in Python How to Implement Your Own Data Structure in Python Mar 03, 2025 am 09:28 AM

How to Implement Your Own Data Structure in Python

Introduction to Parallel and Concurrent Programming in Python Introduction to Parallel and Concurrent Programming in Python Mar 03, 2025 am 10:32 AM

Introduction to Parallel and Concurrent Programming in Python

Mathematical Modules in Python: Statistics Mathematical Modules in Python: Statistics Mar 09, 2025 am 11:40 AM

Mathematical Modules in Python: Statistics

See all articles