1. pywinauto    

pywinauto是一组用于自动化Microsoft Windows GUI的python模块。 最简单的是,它允许您将鼠标和键盘操作发送到窗口对话框和控件。

下载: https://pypi.org/project/pywinauto/

安装: pip install pywinauto

示例

from pywinauto.application import Application
app = Application(backend="uia").start('notepad.exe')

# 描述Notepad.exe进程内的窗口
dlg_spec = app.UntitledNotepad
# 等到窗户真的开着
actionable_dlg = dlg_spec.wait('visible') 


from subprocess import Popen
from pywinauto import Desktop

Popen('calc.exe', shell=True)
dlg = Desktop(backend="uia").Calculator
dlg.wait('visible')

2. Pywin32    

Pywin32提供了很多访问windows的API。较重要的三个模块就是win32api、win32gui和win32con
下载: https://pypi.org/project/pywin32/223/     

安装:pip install pywin32

示例:

import time
import win32gui,win32con
import keyboardEmulation as ke

def get_windows(windowsname,filename):
    # 获取窗口句柄
    hwnd = win32gui.FindWindow(None,windowsname)
    # 将窗口放在前台,并激活该窗口
    win32gui.SetForegroundWindow(hwnd)

    # 输入helloworld

    scancodes = [0x23, 0x12, 0x26, 0x26, 0x18, 0x11, 0x18, 0x13, 0x26, 0x20, 0x2a]

    for code in  scancodes:
        ke.key_press(code)

    # 保存
    ke.key_down(0x1d)
    ke.key_down(0x1f)
    ke.key_up(0x1d)
    ke.key_up(0x1f)

    # 关闭窗口

    time.sleep(1);
    win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)

get_windows("新建文本文档 (2).txt - 记事本","截图.png")

3. pyautogui    

PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,多平台支持(Windows,OS X,Linux)


下载: https://pyautogui.readthedocs.io/en/latest/     

安装:pip3 install pyautogui

示例:

#导入模块
from PIL import ImageGrab
import pyautogui as auto
#定义类
class Screenshoot:
  def __init__(self):
    #self.bbox = bbox
    #self.name = name
    #self.im = ImageGrab.grab(self.bbox)
    #定位xy坐标,confidence为相似度判断,最好不要使用1.0完全相似,比较容易不识别
    self.position_1 = auto.locateCenterOnScreen('1.png', confidence=0.9)
    self.position_2 = auto.locateCenterOnScreen('2.png', confidence=0.9)
    self.position_3 = auto.locateCenterOnScreen('+.png', confidence=0.9)
    self.position_4 = auto.locateCenterOnScreen('=.png', confidence=0.9)
    pass
  def fullshoot(self):
    #全屏截图
    #self.im.save('01.png')
    pass
  def partialshoot(self):
    #局部精确截图
    #self.im.save(self.name+'.png')
    pass
  def position_show(self):
    #打印各坐标
    print(self.position_1)
    print(self.position_2)
    print(self.position_3)
    print(self.position_4)
  def caculate(self):
    #依次点击按钮
    auto.click(self.position_1)
    auto.click(self.position_3)
    auto.click(self.position_2)
    auto.click(self.position_4)
#对象初始化
shoot1 = Screenshoot()
#对象函数执行
shoot1.position_show()
shoot1.caculate()
#shoot1.partialshoot()
#shoot1.fullshoot()

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐