学好PyAutoGUI,让机器帮你干活就能实现这么舒爽的目标
有同学看了以前对python办公自动化:让PyAutoGUI来帮你干活,很感兴趣,开始动手实践。但是遇到很多问题,怎么能让大家能更快的入门PyAutoGUI呢,特地准备了以下系列,从入门到使用技巧,对PyAutoGUI做了全方位的解读。
这是使用PyAutoGUI的快速入门参考。PyAutoGUI是跨平台的GUI自动化模块,适用于Python 2和3.您可以控制鼠标和键盘以及执行基本的图像识别,以自动执行计算机上的任务。
此页面示例中的所有关键字参数都是可选的。
>>> import pyautogui
PyAutoGUI适用于Windows / Mac / Linux和Python 2和3.从PyPI安装。pip install pyautogui
一般功能
>>> 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
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!\n', 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
>>> pyautogui.screenshot() # returns a Pillow/PIL Image object
>>> pyautogui.screenshot('foo.png') # returns a Pillow/PIL Image object, and saves it to a file
如果您有一个想要点击的图像文件,可以在屏幕上找到它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如果在屏幕上找不到图像,则会返回这些功能。注意:定位功能很慢,可能需要一两秒钟。以上对PyAutoGUI做了一个快速入门及基本点回顾,下一篇将会对鼠标控制功能做更详细的介绍。