[常用工具] PyAutoGUI使用教程

2023-02-25,,

PyAutoGUI使用教程

目录
PyAutoGUI使用教程
1 基础知识
2 一般函数
3 故障保险
4 鼠标函数
4.1 鼠标移动
4.2 鼠标拖动
4.3 鼠标单击
4.4 鼠标滚动
4.5 鼠标按下
4.6 缓动/渐变(Tween / Easing)函数
5 键盘函数
5.1 文字输入
5.2 快捷键
5.3 hold()上下文管理器
6 消息框函数
7 截图函数
7.1 截屏
7.2 图像定位
8 参考

PyAutoGUI允许Python脚本控制鼠标和键盘,并自动与其他应用程序交互。PyAutoGUI的API设计很简单,非常容易上手,PyAutoGUI适用于 Windows、macOS和Linux系统,支持在Python2和3上运行。PyAutoGUI安装很简单。

windows安装:

pip install pyautogui

Linux安装:

python3 -m pip install pyautogui
sudo apt-get install scrot
sudo apt-get install python3-tk
sudo apt-get install python3-dev

PyAutoGUI的主要功能有:

移动鼠标并单击其他应用程序的窗口。
向应用程序发送击键信号,如填写表格。
截取屏幕截图,并给出一个图像(例如,按钮或复选框的图像),然后在屏幕上找到它。
找到应用程序的窗口,移动、调整大小、最大化、最小化或关闭它(目前仅适用于 Windows)。
显示警报和消息框。

但是PyAutoGUI只能处理主屏幕,双屏幕不支持。也无法确定当前键盘某个键是否被按下。本文是使用PyAutoGU的快速入门教程,您可以控制鼠标和键盘以及执行基本的图像识别来自动执行计算机上的任务。pyautogui的官方地址为pyautogui官方仓库。更多教程见:

pyautogui英文教程
pyautogui中文教程

1 基础知识

导入库很简单。

import pyautogui

屏幕上的位置由X和Y笛卡尔坐标表示。X坐标从左侧的0开始,向右增加。与数学不同,Y坐标从顶部的0开始,向下增加。左上角的像素位于坐标(0, 0)。如果您的屏幕分辨率为 1920 x 1080,则右下角的像素将为(1919, 1079),因为坐标从0开始,而不是1。坐标系如下所示。

0,0       X increases -->
+---------------------------+
| | Y increases
| | |
| 1920 x 1080 screen | |
| | V
| |
| |
+---------------------------+ 1919, 1079

2 一般函数

屏幕分辨率大小由size()函数作为两个整数的元组返回。鼠标光标的当前X和Y坐标由position()函数返回。

# 获取当前鼠标位置
print(pyautogui.position())
# 获取当前屏幕的分辨率
print(pyautogui.size())
# 判断某个坐标是否在屏幕上
x=10
y=20
print(pyautogui.onScreen(x, y))

3 故障保险

在每次调用PyAutoGUI的函数后设置2.5秒的暂停:

# 暂停2.5s
pyautogui.PAUSE = 2.5

此外,为了防止程序出问题,当鼠标移动到屏幕左上角,会引发pyautogui.FailSafeException错误进而中止程序。关闭命令如下(不建议关闭):

pyautogui.FAILSAFE = False

4 鼠标函数

和图像坐标系一样,屏幕左上角的坐标点为(0, 0),X向右增加,Y向下增加。

4.1 鼠标移动

# 用num_seconds(秒)将鼠标移动到(x,y)位置
x = 200
y = 100
num_seconds = 1
pyautogui.moveTo(x, y, duration=num_seconds) # 用num_seconds(秒)将鼠标从当前位置向右移动xOffset,向下移动yOffset
# 如果duration为0或未指定,则立即移动。
xOffset = 30
yOffset = -50
num_seconds = 0.5
pyautogui.moveRel(xOffset, yOffset, duration=num_seconds)

4.2 鼠标拖动

# 用num_seconds(秒)将鼠标推动到(x,y)位置
# 鼠标拖动是指按下鼠标左键移动鼠标。
x = 200
y = 100
num_seconds= 1
pyautogui.dragTo(x, y, duration=num_seconds) # 用num_seconds(秒)将鼠标从当前位置向右拖动xOffset,向下推动yOffset
xOffset = 30
yOffset = -50
num_seconds = 0.5
pyautogui.dragRel(xOffset, yOffset, duration=num_seconds)

4.3 鼠标单击

# 将鼠标移动到(moveToX,moveToY)位置,点击鼠标num_of_clicks次,每次点击间隔secs_between_clicks秒
# button表示单击方式,'left'左键单击,'middle'中键单击,'right'右键单击
moveToX = 500
moveToY = 600
num_of_clicks = 1
secs_between_clicks = 1
pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left')

所有的鼠标点击都可以用click()完成,但也存在一些函数是为了方便阅读,如下所示。

moveToX = 10
moveToY = 20
# 右键单击
pyautogui.rightClick(x=moveToX + 50, y=moveToY)
# 中键单击
pyautogui.middleClick(x=moveToX + 50, y=moveToY)
# 左键双击
pyautogui.doubleClick(x=moveToX + 50, y=moveToY)
# 左键三击
pyautogui.tripleClick(x=moveToX + 50, y=moveToY)

4.4 鼠标滚动

moveToX = 100
moveToY = 200
# 鼠标在当前位置向下滑动100格
# pyautogui.scroll(clicks=-100)
# 鼠标移动到(moveToX,moveToY)位置,然后向上滚动150格
pyautogui.scroll(clicks=150, x=moveToX, y=moveToY)

4.5 鼠标按下

# 鼠标移动到(moveToX,moveToY)位置,鼠标左键按下
pyautogui.mouseDown(x=moveToX, y=moveToY, button='left')
# 鼠标移动到(moveToX,moveToY)位置,鼠标右键松开(按下右键的情况下)
pyautogui.mouseUp(x=moveToX, y=moveToY, button='right')
# 鼠标在当前位置,按下中键
pyautogui.mouseDown(button='middle')

4.6 缓动/渐变(Tween / Easing)函数

缓动/渐变函数的作用是让光标的移动更炫。如果你不需要用到的话,你可以忽略这些。PyAutoGUI有30种缓动/渐变函数,可以通过以下函数查看

print(pyautogui.ease*?)

常用缓动/渐变函数使用示例如下:

moveToX = 100
moveToY = 100
# #开始慢,结束快
pyautogui.moveTo(moveToX + 5 , moveToY+ 45, 2, pyautogui.easeInQuad)
# 开始快,结束慢
pyautogui.moveTo(moveToX + 15, moveToY+ 35, 2, pyautogui.easeOutQuad)
# 快速开始和结束,中间缓慢
pyautogui.moveTo(moveToX + 25, moveToY+ 25, 2, pyautogui.easeInOutQuad)
# 最后反弹
pyautogui.moveTo(moveToX + 35, moveToY+ 15, 2, pyautogui.easeInBounce)
# 反复横跳
pyautogui.moveTo(moveToX + 45, moveToY+ 5, 2, pyautogui.easeInElastic)

5 键盘函数

5.1 文字输入

键盘控制文字输入的主要函数就是typewrite()/write()。这个函数可以实现字符输入,可以用interval参数设置两次输入间时间间隔。

# 在当前位置输入文字text,每个字符输入间隔secs_between_keys秒
# \n表示换行
text = 'Hello world!\n'
secs_between_keys = 0.1
pyautogui.typewrite(message=text, interval=secs_between_keys)
# 在当前位置按下键盘各种键
pyautogui.typewrite(['\t', 'a', 'b', 'c', 'left', 'backspace', 'enter', 'f1','\n'], interval=secs_between_keys)
# 查看所有支持按键
print(pyautogui.KEYBOARD_KEYS)

5.2 快捷键

通过keyDown/keyUp按下或者松开键盘,通过hotkey执行快捷键操作。

# ctrl+c 复制文字
pyautogui.hotkey('ctrl', 'c')
# ctrl+v 粘贴文字
pyautogui.hotkey('ctrl', 'v') # 按下ctrl键
pyautogui.keyDown('ctrl')
# 按下v键,相当文字粘贴
pyautogui.keyDown('v')
# 松开ctrl键盘
pyautogui.keyUp('ctrl')

当然可以使用press()函数设置按下某个键再释放某个键,如下所示。

#  按下shift键
pyautogui.keyDown('shift')
pyautogui.press('left')
pyautogui.press('left')
pyautogui.press('left')
# 松开shift键
pyautogui.keyUp('shift')

同时也可以和typewrite()函数一样,用数组把一组键传入press(),或者设置press按压次数。

# 按下三个left键
pyautogui.press(['left', 'left', 'left'])
# 按left键五次
pyautogui.press('left', presses=5)

5.3 hold()上下文管理器

hold()函数可以用作上下文管理器,并从pyautogui.KEYBOARD_KEYS传递一个字符串,并且该键将在上下文块的持续时间内保持。示例如下:

# 按住shift
with pyautogui.hold('shift'):
# 连续按left,然后松开shift
pyautogui.press(['left', 'left', 'left']) # 上面代码功能和下面代码实现功能相同
# 按下shift键
pyautogui.keyDown('shift')
pyautogui.press('left')
pyautogui.press('left')
pyautogui.press('left')
# 松开shift键
pyautogui.keyUp('shift')

6 消息框函数

如果你需要暂停程序直到用户点击确定,或者想向用户显示一些信息,可以使用消息框函数。这里消息框函数的使用方式和javascript一样。

# 警告窗口
alert_result = pyautogui.alert('点击确定返回字符串OK')
# 确认窗口
confirm_result = pyautogui.confirm('点击确定返回字符串OK,点击取消返回字符串Cancel')
# 点击ok保存输入的文字,点击Cancel返回None
prompt_result = pyautogui.prompt('输入文字')
# 点击ok保存输入的密码,点击Cancel返回None
# default默认文字,mask用什么符号代替输入的密码
password_result = pyautogui.password(text='', title='', default='', mask='*')

7 截图函数

PyAutoGUI使用Pillow/PIL库实现图像的处理。在Linux上,您必须运行以下命令安装scrot库才能使用屏幕截图功能。

sudo apt-get install scrot

7.1 截屏

# 截屏返回result对象
result = pyautogui.screenshot()
# result是PIL中的Image对象
print(type(result))
# 保存图像
result.save('result1.jpg')
# 展示图片
#result.show() # imageFilename参数设置文件保存为止,在截屏前保存图片到本地foo.png文件
# region设置截图区域[x,y,w,h],以(x,y)为左上角顶点,截宽w,高h的区域
result = pyautogui.screenshot(imageFilename='result2.jpg',region=[10,20,100,50])

7.2 图像定位

PyAutoGUI提供了多个定位函数。都是从左上角原点开始向右向下搜索截图位置。具体如下:

locateOnScreen(image, grayscale=False):在屏幕中,返回和image图片最类似区域的坐标(left, top, width, height),如果没找到返回None。grayscale设置是否灰度查找。
locateCenterOnScreen(image, grayscale=False):在屏幕中,返回和image图片最类似区域的中心坐标(x, y),如果没找到返回None。
locateAllOnScreen(image, grayscale=False):在屏幕中,返回和image图片所有类似区域的坐标(left, top, width, height)的生成器
locate(needleImage, haystackImage, grayscale=False):在haystackImage中,返回和image图片最类似区域的坐标(left, top, width, height)。
locateAll(needleImage, haystackImage, grayscale=False):在haystackImage中,返回和image图片所有类似区域的坐标(left, top, width, height)的生成器。

官方说在1920x1080屏幕上,screenshot()函数大约需要100毫秒。但实测图像定位需要花费3秒左右,而且常常找不到图片相似区域。可选的confidence关键字参数指定函数在屏幕上定位图像的准确性。如果由于像素差异可忽略不计,函数无法定位图像,调低confidence将提高查找命中结果。但是需要安装OpenCV才能使confidence关键字工作。

图像定位函数基础使用如下:

# 在屏幕返回和result1.jpg图片类似的区域坐标,返回值(左上角x坐标,左上角y坐标,宽度,高度)
# 如果没找到返回None
result = pyautogui.locateOnScreen('result1.jpg')
# 在屏幕返回和result1.jpg图片类似的区域中间位置的XY坐标,confidence返回区域最低置信度
result = pyautogui.locateCenterOnScreen('result1.jpg', confidence=0.9)
# 为查找图片找到的所有位置返回一个生成器
results = pyautogui.locateAllOnScreen('result1.jpg', confidence=0.6)
print(results)
# 打印各组的(左上角x坐标,左上角y坐标,宽度,高度)
for i in results:
print(i)
# 将结果保存为list
list_result = list(pyautogui.locateAllOnScreen('result1.jpg', confidence=0.6) # 在haystackImage中,返回和image图片最类似区域的坐标
result = pyautogui.locate(needleImage='result1.jpg', haystackImage='result.jpg', confidence=0.5)
# 在haystackImage中,返回和image图片所有类似区域的坐标(left, top, width, height)
result = pyautogui.locateAll(needleImage='result1.jpg', haystackImage='result.jpg', confidence=0.5)

这些“定位”功能相当昂贵;他们可能需要整整几秒钟的时间才能运行。加速它们的最好方法是传递一个region参数(一个(左、上、宽、高)的4整数元组)来只搜索屏幕的较小区域而不是全屏。但是这个region区域必须比待搜索截图区域大,否则会引发错误。代码如下:

result = pyautogui.locateOnScreen('result1.jpg', region=(0,0, 300, 400))
result = pyautogui.locate(needleImage='result1.jpg', haystackImage='result.jpg', confidence=0.5, region=(0,0, 300, 400))

您可以传递grayscale=True给定位函数以提供轻微的加速(大约30%左右)。这会降低图像和屏幕截图的颜色饱和度,加快定位速度,但可能会导致误报匹配。

result_location = pyautogui.locateOnScreen('result.jpg', grayscale=True,confidence=0.6)

此外要获取截屏某个位置的RGB像素值,可以用PIL中Image对象的getpixel()方法,也可以用PyAutoGUI的pixel()函数。

im = pyautogui.screenshot()
print(im.getpixel((100, 200)))
print(pyautogui.pixel(100, 200))

如果您只需要验证单个像素是否与给定像素匹配,请调用该pixelMatchesColor()函数,并将其表示的颜色的X坐标、Y坐标和RGB元组传递给它:

# 颜色匹配
pyautogui.pixelMatchesColor(100, 200, (255, 255, 255))
# tolerance参数可以指定红、绿、蓝3种颜色误差范围
pyautogui.pixelMatchesColor(100, 200, (248, 250, 245), tolerance=10)

8 参考

pyautogui官方仓库
pyautogui英文教程
pyautogui中文教程

[常用工具] PyAutoGUI使用教程的相关教程结束。

《[常用工具] PyAutoGUI使用教程.doc》

下载本文的Word格式文档,以方便收藏与打印。