安装
pip install pyautogui
pip install pypiwin32
demo
下面的例子,展示了如下功能:
1. 列出所有的活动应用程序,
2. 模拟点击事件,
3. 在当前程序指定位置输入参数
4. 截图保存
5. 打开应用程序
6. 最大化应用程序,并在最顶层窗口展示
7. 最小化应用程序
import os
import time
import pyautogui
import win32gui, win32con, win32api
'''
该接口中可以操作其他Windows应用程序,可是实现点击按钮,输入参数,截图保存等
'''
class OtherProUiControl:
def __init__(self, app_path):
'''
初始化函数
:param app_path: 操作的应用程序的绝对路径
'''
self.app_path = app_path
## 模拟点击事件-->对当前打开的应用程序执行操作
@classmethod
def mouse_click(cls, x, y, leftOrRight=1):
'''
点击界面上的应用程序
:param x: 点击处的x坐标
:param y: 点击处的y坐标
:param leftOrRight: 鼠标左键还是右键:value=1:左键,value=0: 右键
:return:
'''
try:
pyautogui.click(x, y) if leftOrRight else pyautogui.click(x, y, button='right')
except Exception as e:
print(f'click error: {e}')
## 对当前窗口指定的位置输入指定的参数
@classmethod
def setPara(cls, x, y, text: str):
'''
这里使用两次删除键跟enter按键,主要是解决输入法为中文时,有时候会报错
:param x:
:param y:
:param text:
:return:
'''
OtherProUiControl.mouse_click(x, y)
# pyautogui.press(['backspace' for x in range(4)])
pyautogui.hotkey('ctrl', 'a') # 全选后直接输入,或者使用backspace都可以
pyautogui.typewrite(text, interval=0.15) # 每个输入字符之间的时间间隔为0.15s, 模拟人输入的方式
pyautogui.press('enter')
pyautogui.press('enter') # 二次输入解决中文输入法问题
## 操作完成后进行截屏-->filename: 保存文件的绝对路径
@classmethod
def saveImg(cls, filename):
pyautogui.screenshot(filename)
print(f'{filename} save successfully...')
# pyautogui.press('F1')
## 遍历windows下 所有句柄及窗口名称
@staticmethod
def checkActivePro():
'''
检测当前的活动窗口,如果不确定窗口的名字,可以使用该方法直接获取句柄,来操作窗口
:return: 窗口句柄: 窗口Title 组成的字典
'''
hwnd_title = dict()
def get_all_hwnd(hwnd, mouse):
if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
print()
hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})
win32gui.EnumWindows(get_all_hwnd, 0)
# for h, t in hwnd_title.items():
# if t != "":
# print(h, t)
# if t == '新建 DOCX 文档.docx - WPS Office':
# win32gui.ShowWindow(h, win32con.SW_MAXIMIZE)
# ## 将窗口放在前台,并激活该窗口(窗口不能最小化)
# win32gui.SetForegroundWindow(h)
# print(hwnd_title)
return hwnd_title
# 打开应用程序
def open_app(self):
try:
win32api.ShellExecute(0, 'open', self.app_path, '', '', 1)
except:
print(f'Program can not be opened...')
# 把即将要操作的应用程序窗口在桌面最顶层显示
def showWin(self):
try:
windowsname = os.path.basename(self.app_path).split('.')[0]
self.handle = win32gui.FindWindow(None, windowsname)
# 应用最小化时,使用该指令可以让该应用重新最大化-->该语句非常重要
win32gui.ShowWindow(self.handle, win32con.SW_MAXIMIZE)
## 将窗口放在前台,并激活该窗口(窗口不能最小化)
win32gui.SetForegroundWindow(self.handle)
time.sleep(1) # 如果操作完成,立刻截图,最好先sleep,不然可能会出现图片错位
except Exception as e:
print(f'showWin error: {e}')
# 根据传输的句柄,将相应的进程界面,最大化显示
@classmethod
def setWinFront(cls, handle):
win32gui.ShowWindow(handle, win32con.SW_MAXIMIZE)
## 将窗口放在前台,并激活该窗口(窗口不能最小化)
win32gui.SetForegroundWindow(handle)
# 最小化指定的应用程序
@classmethod
def minimizeWindow(cls, handle):
win32gui.ShowWindow(handle, win32con.SW_SHOWDEFAULT) # 还原最大化前的尺寸
win32gui.ShowWindow(handle, win32con.SW_MINIMIZE)
if __name__ == '__main__':
for h, t in res.items():
if t != "":
print(h, t)
if 'Google Chrome' in t:
win32gui.ShowWindow(h, win32con.SW_MAXIMIZE)
## 将窗口放在前台,并激活该窗口(窗口不能最小化)
win32gui.SetForegroundWindow(h)
time.sleep(5)
OtherProUiControl.minimizeWindow(h)
OtherProUiControl.mouse_click(1284, 705)
pyqwin32,操作参考
Python中最全的窗口操作,如窗口最大化、最小化、窗口置顶、获取缩放比例等_python 窗口_是小菜欸的博客-CSDN博客