pyautogui桌面自动化基础使用方法
PyAutoGUI通过接管鼠标、键盘使用权,基本上模拟真人操作,可以解放人重复操作的行为。
1. 获取基础信息
1.1 屏幕信息
获取屏幕尺寸(分辨率×分辨率)
width, height = pyautogui.size()
print('屏幕宽:', width, '屏幕高:', height)
1.2 鼠标信息
获取鼠标当前位置
print('鼠标X轴位置:', pyautogui.position().x, '鼠标Y轴位置:', pyautogui.position().y)
判断鼠标是否在屏幕范围内
print('该坐标是否在屏幕内:', pyautogui.onScreen(pyautogui.position().x, pyautogui.position().y))
2. 控制鼠标
2.1 移动鼠标
- 尽量不要移动到边界,否则会触发保护报错 当然使用下方代码也可以关闭
pyautogui.FAILSAFE = False
- 鼠标移动到绝对位置
pyautogui.moveTo(200, 100)
- 鼠标移动相对位置 move = moveRel
pyautogui.moveRel(50, 50, 1)
2.2 鼠标点击
- 移动至屏幕中心点击一下左键,过渡时间0.5秒
pyautogui.click(width / 2, height / 2, duration=0.5)
- 不指定x、y,在当前位置点击一下右键
pyautogui.click(button='right')
- 移动至(100,100)点击3次左键,点击间隔0.1s,鼠标移动过渡时间0.5秒
pyautogui.click(100, 100, clicks=3, interval=0.1, duration=0.5)
- 移动至(100,100)点击2次右键,点击间隔0.5s,鼠标移动过渡时间0.2秒
pyautogui.click(100, 100, clicks=2, interval=0.5, button='right', duration=0.2)
2.3 鼠标滚轮
- 将鼠标从当前位置拖至屏幕中心,默认左键
pyautogui.dragTo(width / 2, height / 2)
- 将鼠标从当前位置向左100像素、向右200像素拖动,过渡时间0.5秒,指定右键
pyautogui.dragRel(-100, 200, duration=0.5, button='right')
2.3 鼠标拖曳
- 将鼠标从当前位置拖至屏幕中心,默认左键
pyautogui.dragTo(width / 2, height / 2)
- 将鼠标从当前位置向左100像素、向右200像素拖动,过渡时间0.5秒,指定右键
pyautogui.dragRel(-100, 200, duration=0.5, button='right')
3. 键盘控制
3.1 按键
- 键名用字符串表示,支持的所有键名,存在pyautogui.KEYBOARD_KEYS变量中,包括26个字母、数字、符号、F1~F20、方向等等所有按键
pyautogui.press('a') # 按字母A键,字母支持大小写都可以识别到对应键
- 程序向终端输入了字符a,若程序运行时输入法为中文状态,由于没有继续输入空格或回车,输入法仅列出候选字,并不会输入到终端
pyautogui.press('Shift')
- 传入键名列表(按键p、按键y、空格),按键之间间隔0.1秒(默认0)
pyautogui.press(['p', 'y', 'space'], interval=0.1)
- 运行前将输入法切换到中文状态,往终端直接输入了“朋友”等中文候选第一个词…
3.2 连续输入
- typewrite方式一:传入字符串,不支持中文字符,因为函数无法知道输入法需要什么按键才能得到中文字符
pyautogui.typewrite('hello, PyAutoGUI!\n')
- 程序把字符串"'hello, PyAutoGUI!"和换行符输入到了终端
- typewrite方式二:传入键名列表,按键之间间隔0.1秒(默认0)
pyautogui.typewrite(['s', 'r', 'f', 'space'], interval=0.1)
- 运行前将输入法切换到中文状态,往终端直接输入了“输入法”3个字
- 大小写字母是自动支持的,仍然尝试一次切换到大写
pyautogui.typewrite(['capslock', 'p', 'y'])
- CapsLock按键灯被点亮,程序往终端输入了"PY"
3.3 快捷键
- hotkey屏蔽了需要反复keyDown、keyUp的细节,参数是任意个键名,而非列表
pyautogui.hotkey('ctrl', 'shift', 'esc') # 调出任务管理器 pyautogui.hotkey('alt', 'ctrl', 'delete') # 并未调出重启界面
4. 消息窗口
- 警告弹窗
pyautogui.alert(text='警告', title='PyAutoGUI消息框', button='OK')
- 确认弹窗
pyautogui.confirm(text='请选择', title='PyAutoGUI消息框', buttons=['1', '2', '3'])
- 输入弹窗
pyautogui.prompt(text='请输入', title='PyAutoGUI消息框', default='请输入')
- 输入密码弹窗
pyautogui.password(text='输入密码', title='PyAutoGUI消息框', default='', mask='*')
5. 截图
- imageFilename参数,截图要保存的文件全路径名,默认
None
,不保存;
region参数,截图区域,由左上角坐标、宽度、高度4个值确定,如果指定区域超出了屏幕范围,超出部分会被黑色填充,默认None
,截全屏pyautogui.screenshot('shot.png', region=(1000, 600, 600, 400))
6. 图片定位
- 输入图片,可选在指定位置查找
print(pyautogui.locateOnScreen("ScreenShot/vscode.png"))
- region参数限制查找范围,加快查找速度, 左上角坐标、宽度、高度4个
vscode_location = pyautogui.locateOnScreen("ScreenShot/vscode.png", region=(770, 0, 100, 60)) print(vscode_location) if vscode_location: x, y = pyautogui.center(vscode_location) pyautogui.moveTo(x, y) pyautogui.click()]
7. 完整代码
import pyautogui
from PIL import Image
# 1.
# 1.1 屏幕信息
# 获取屏幕尺寸(分辨率×分辨率)
width, height = pyautogui.size()
print('屏幕宽:', width, '屏幕高:', height)
# 1.2 鼠标信息
# 获取鼠标当前位置
print('鼠标X轴位置:', pyautogui.position().x, '鼠标Y轴位置:', pyautogui.position().y)
# 判断鼠标是否在屏幕范围内
print('该坐标是否在屏幕内:', pyautogui.onScreen(pyautogui.position().x, pyautogui.position().y))
# 2. 控制鼠标
# 2.1 移动鼠标
# 尽量不要移动到边界,否则会触发保护报错 当然使用下方代码也可以关闭
pyautogui.FAILSAFE = False
# 鼠标移动到绝对位置
pyautogui.moveTo(200, 100)
# 鼠标移动相对位置 move = moveRel
pyautogui.moveRel(50, 50, 1)
# 2.2 鼠标点击
# 移动至屏幕中心点击一下左键,过渡时间0.5秒
pyautogui.click(width / 2, height / 2, duration=0.5)
# 不指定x、y,在当前位置点击一下右键
pyautogui.click(button='right')
# 移动至(100,100)点击3次左键,点击间隔0.1s,鼠标移动过渡时间0.5秒
pyautogui.click(100, 100, clicks=3, interval=0.1, duration=0.5)
# 移动至(100,100)点击2次右键,点击间隔0.5s,鼠标移动过渡时间0.2秒
pyautogui.click(100, 100, clicks=2, interval=0.5, button='right', duration=0.2)
# 2.3 鼠标滚轮
# 将鼠标从当前位置拖至屏幕中心,默认左键
pyautogui.dragTo(width / 2, height / 2)
# 将鼠标从当前位置向左100像素、向右200像素拖动,过渡时间0.5秒,指定右键
pyautogui.dragRel(-100, 200, duration=0.5, button='right')
# 2.3 鼠标拖曳
# 将鼠标从当前位置拖至屏幕中心,默认左键
pyautogui.dragTo(width / 2, height / 2)
# 将鼠标从当前位置向左100像素、向右200像素拖动,过渡时间0.5秒,指定右键
pyautogui.dragRel(-100, 200, duration=0.5, button='right')
# 3. 键盘控制
# 3.1 按键
# 键名用字符串表示,支持的所有键名,存在pyautogui.KEYBOARD_KEYS变量中,包括26个字母、数字、符号、F1~F20、方向等等所有按键
pyautogui.press('a') # 按字母A键,字母支持大小写都可以识别到对应键
# 程序向终端输入了字符a,若程序运行时输入法为中文状态,由于没有继续输入空格或回车,输入法仅列出候选字,并不会输入到终端
pyautogui.press('Shift')
# 传入键名列表(按键p、按键y、空格),按键之间间隔0.1秒(默认0)
pyautogui.press(['p', 'y', 'space'], interval=0.1)
# 运行前将输入法切换到中文状态,往终端直接输入了“朋友”等中文候选第一个词...
# 3.2 连续输入
# typewrite方式一:传入字符串,不支持中文字符,因为函数无法知道输入法需要什么按键才能得到中文字符
pyautogui.typewrite('hello, PyAutoGUI!\n')
# 程序把字符串"'hello, PyAutoGUI!"和换行符输入到了终端
# typewrite方式二:传入键名列表,按键之间间隔0.1秒(默认0)
pyautogui.typewrite(['s', 'r', 'f', 'space'], interval=0.1)
# 运行前将输入法切换到中文状态,往终端直接输入了“输入法”3个字
# 大小写字母是自动支持的,仍然尝试一次切换到大写
pyautogui.typewrite(['capslock', 'p', 'y'])
# CapsLock按键灯被点亮,程序往终端输入了"PY"
# 3.3 快捷键
# hotkey屏蔽了需要反复keyDown、keyUp的细节,参数是任意个键名,而非列表
pyautogui.hotkey('ctrl', 'shift', 'esc') # 调出任务管理器
pyautogui.hotkey('alt', 'ctrl', 'delete') # 并未调出重启界面
# 4. 消息窗口
# 4.1 警告弹窗
pyautogui.alert(text='警告', title='PyAutoGUI消息框', button='OK')
# 4.2 确认弹窗
pyautogui.confirm(text='请选择', title='PyAutoGUI消息框', buttons=['1', '2', '3'])
# 4.3 输入弹窗
pyautogui.prompt(text='请输入', title='PyAutoGUI消息框', default='请输入')
# 4.4 输入密码弹窗
pyautogui.password(text='输入密码', title='PyAutoGUI消息框', default='', mask='*')
# 5. 截图
# imageFilename参数,截图要保存的文件全路径名,默认`None`,不保存;
# region参数,截图区域,由左上角坐标、宽度、高度4个值确定,如果指定区域超出了屏幕范围,超出部分会被黑色填充,默认`None`,截全屏
pyautogui.screenshot('shot.png', region=(1000, 600, 600, 400))
# 6. 图片定位
# 输入图片,可选在指定位置查找
print(pyautogui.locateOnScreen("ScreenShot/vscode.png"))
# region参数限制查找范围,加快查找速度, 左上角坐标、宽度、高度4个
vscode_location = pyautogui.locateOnScreen("ScreenShot/vscode.png", region=(770, 0, 100, 60))
print(vscode_location)
if vscode_location:
x, y = pyautogui.center(vscode_location)
pyautogui.moveTo(x, y)
pyautogui.click()