pyautogui

PyAutoGUI 可实现控制鼠标、键盘、消息框、截图、定位等功能,最近做了个自动化需要这些,故了解并记录下

资料来源:https://pyautogui.readthedocs.io/en/latest/general.html

PyAutoGUI适用于Windows / Mac / Linux以及Python 2和3.从PyPI安装。pip install pyautogui

PyAutoGUI 鼠标键盘方法记录

moveTo(x,y)将鼠标移动到指定的 x、y 坐标。
moveRel (xOffset,yOffset)相对于当前位置移动鼠标。
dragTo(x,y)按下左键移动鼠标。
dragRel (xOffset,yOffset)按下左键,相对于当前位置移动鼠标。
click(x,y,button)模拟点击(默认是左键)。
rightClick() 模拟右键点击。
middleClick() 模拟中键点击。
doubleClick() 模拟左键双击。
mouseDown(x,y,button)模拟在 x、y 处按下指定鼠标按键。
mouseUp(x,y,button)模拟在 x、y 处释放指定键。
scroll (units)模拟滚动滚轮。正参数表示向上滚动,负参数表示向下滚动。
typewrite(message)键入给定消息字符串中的字符。
typewrite([key1,key2,key3])键入给定键字符串。
press(key)按下并释放给定键。
keyDown(key)模拟按下给定键。
keyUp(key)模拟释放给定键。
hotkey([key1,key2,key3])模拟按顺序按下给定键字符串,然后以相反的顺序释放。
screenshot() 返回屏幕快照的 Image 对象

一般功能

>>> pyautogui.position()  # current mouse x and y
(968, 56)
>>> pyautogui.size()  # current screen resolution width and height
(1920, 1080)
>>> pyautogui.onScreen(x, y)  # True if x & y are within the screen.
True

失败保险

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

>>> import pyautogui
>>> pyautogui.PAUSE = 2.5

当故障安全模式是True,将鼠标移动到左上角将引发pyautogui.FailSafeException可以中止程序的:

>>> import pyautogui
>>> pyautogui.FAILSAFE = True

鼠标功能

XY坐标在屏幕的左上角有0,0原点。X增加向右,Y增加向下。

>>> pyautogui.moveTo(x, y, duration=num_seconds)  # move mouse to XY coordinates over num_second seconds
>>> pyautogui.moveRel(xOffset, yOffset, duration=num_seconds)  # move mouse relative to its current position

如果duration为0或未指定,则立即移动。注意:在Mac上拖动不能立即。

>>> pyautogui.dragTo(x, y, duration=num_seconds)  # drag mouse to XY
>>> pyautogui.dragRel(xOffset, yOffset, duration=num_seconds)  # drag mouse relative to its current position

调用click()只需用鼠标当前位置的左键单击鼠标一次,但关键字参数可以改变:

>>> pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left')

button关键字参数可以是'left''middle''right'

所有点击都可以完成click(),但这些功能是为了便于阅读而存在。关键字args是可选的:

>>> pyautogui.rightClick(x=moveToX, y=moveToY)
>>> pyautogui.middleClick(x=moveToX, y=moveToY)
>>> pyautogui.doubleClick(x=moveToX, y=moveToY)
>>> pyautogui.tripleClick(x=moveToX, y=moveToY)

正向滚动将向上滚动,负向滚动将向下滚动:

>>> pyautogui.scroll(amount_to_scroll, x=moveToX, y=moveToY)

可以单独调用单个按钮向下和向上事件:

>>> pyautogui.mouseDown(x=moveToX, y=moveToY, button='left')
>>> pyautogui.mouseUp(x=moveToX, y=moveToY, button='left')

键盘功能

按键可以转到键盘光标处于功能调用时的任何位置

>>> pyautogui.typewrite('Hello world!
', interval=secs_between_keys)  # useful for entering text, newline is Enter

键名称列表也可以传递:

>>> pyautogui.typewrite(['a', 'b', 'c', 'left', 'backspace', 'enter', 'f1'], interval=secs_between_keys)

密钥名称的完整列表在pyautogui.KEYBOARD_KEYS

Ctrl-S或Ctrl-Shift-1等键盘热键可以通过将键名列表传递给hotkey()

>>> pyautogui.hotkey('ctrl', 'c')  # ctrl-c to copy
>>> pyautogui.hotkey('ctrl', 'v')  # ctrl-v to paste

可以单独调用单个按钮向下和向上事件:

>>> pyautogui.keyDown(key_name)
>>> pyautogui.keyUp(key_name)

消息框功能

如果您需要暂停程序直到用户单击“确定”,或者想要向用户显示某些信息,则消息框函数具有与JavaScript类似的名称:

>>> pyautogui.alert('This displays some text with an OK button.')
>>> pyautogui.confirm('This displays text and has an OK and Cancel button.')
'OK'
>>> pyautogui.prompt('This lets the user type in a string and press OK.')
'This is what I typed in.'

如果用户单击“取消”,该prompt()函数将返回None

截图函数

PyAutoGUI使用Pillow / PIL作为其图像相关数据。

在Linux上,您必须运行才能使用屏幕截图功能。sudo apt-get install scrot

  • locateOnScreen(image, grayscale=False)- 返回image屏幕上第一个找到的实例的(左,顶部,宽度,高度)坐标。ImageNotFoundException如果在屏幕上找不到则会引发。
  • locateCenterOnScreen(image, grayscale=False)- 返回image屏幕上第一个找到的实例中心的(x,y)坐标。ImageNotFoundException如果在屏幕上找不到则会引发。
  • locateAllOnScreen(image, grayscale=False) - 返回一个生成器,该生成器生成(左,顶部,宽度,高度)元组,用于在屏幕上找到图像的位置。
  • locate(needleImage, haystackImage, grayscale=False)-返回(左,上,宽度,高度)的第一坐标发现的实例needleImagehaystackImageImageNotFoundException如果在屏幕上找不到则会引发。
  • locateAll(needleImage, haystackImage, grayscale=False)- 返回一个生成器(生成(左,顶部,宽度,高度)元组的位置needleImagehaystackImage
>>> pyautogui.screenshot()  # returns a Pillow/PIL Image object
<PIL.Image.Image image mode=RGB size=1920x1080 at 0x24C3EF0>
>>> pyautogui.screenshot('foo.png')  # returns a Pillow/PIL Image object, and saves it to a file
<PIL.Image.Image image mode=RGB size=1920x1080 at 0x31AA198>

如果您有一个想要点击的图像文件,可以在屏幕上找到它locateOnScreen()

>>> pyautogui.locateOnScreen('looksLikeThis.png')  # returns (left, top, width, height) of first place it is found
(863, 417, 70, 13)

locateAllOnScreen()函数将为屏幕上找到的所有位置返回一个生成器:

>>> for i in pyautogui.locateAllOnScreen('looksLikeThis.png')
...
...
(863, 117, 70, 13)
(623, 137, 70, 13)
(853, 577, 70, 13)
(883, 617, 70, 13)
(973, 657, 70, 13)
(933, 877, 70, 13)
>>> list(pyautogui.locateAllOnScreen('looksLikeThis.png'))
[(863, 117, 70, 13), (623, 137, 70, 13), (853, 577, 70, 13), (883, 617, 70, 13), (973, 657, 70, 13), (933, 877, 70, 13)]

locateCenterOnScreen()函数只返回在屏幕上找到图像的中间的XY坐标:

>>> pyautogui.locateCenterOnScreen('looksLikeThis.png')  # returns center x and y
(898, 423)

None如果在屏幕上找不到图像,则会返回这些功能。

注意:定位功能很慢,可能需要一两秒钟。

position() - 返回整数的元组:(x,y)表示鼠标光标的当前位置。

size() - 返回整数的元组:(宽度,高度)表示主监视器的大小。TODO - 添加多显示器支持

https://pyautogui.readthedocs.io/en/latest/keyboard.html

故乡明
原文地址:https://www.cnblogs.com/luweiweicode/p/14696238.html