前言 pyautogui是用来做GUI桌面应用自动化的Python包,功能类似于按键精灵,可以实现控制鼠标、键盘、消息框、截图、定位功能,支持跨平台。不过也有缺点,比如说不支持中文输入(一般配合pyperclip解决此问题)
安装 1 2 3 4 5 6 7 8 9 10 11 12 pip install pyautogui -i https://pypi.tuna.tsinghua.edu.cn/simple pip install pyobjc-core pip install pyobjc pip install pyautogui pip install python3-xlib pip install pyautogui
操作 鼠标操作 以屏幕左上角的为原点,向右为x轴正向,向下为y轴正向,单位是像素,通过(x,y)确定位置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 pyautogui.PAUSE=2 pyautogui.FAILSAFE = True pyautogui.size() pyautogui.position() pyautogui.onScreen(x, y) pyautogui.moveTo(x, y) pyautogui.move(x,y) pyautogui.click() pyautogui.rightClick() pyautogui.middleClick() pyautogui.doubleClick() pyautogui.dragTo(x, y, button='left' ) pyautogui.drag(x, y, time) pyautogui.mouseDown() pyautogui.mouseUp() pyautogui.scroll()
1 2 3 4 5 6 7 8 9 10 import pyautoguiprint ("屏幕分辨率:" , pyautogui.size())print ("鼠标位置:" , pyautogui.position())pyautogui.PAUSE = 2 pyautogui.moveTo(500 , 500 ) pyautogui.rightClick() pyautogui.click(clicks=2 , interval=0.3 ) pyautogui.scroll(-500 )
键盘操作 1 2 3 4 5 6 7 8 pyautogui.KEYBOARD_KEYS pyautogui.write(msg) pyperclip.copy(msg) pyautogui.write([key1,key2]) pyautogui.press(key) pyautogui.keyDown(key) pyautogui.keyUp(key) pyautogui.hotkey('ctrl' , 'v' )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import pyautoguiimport pyperclippyautogui.PAUSE = 1 print (pyautogui.KEYBOARD_KEYS) pyautogui.write("hello world" ) pyautogui.press('enter' ) pyautogui.keyDown('shift' ) pyautogui.press('3' ) pyautogui.keyUp('shift' ) pyperclip.copy("这个是中文文本" ) pyautogui.hotkey('ctrl' , 'v' )
信息弹窗 1 2 3 4 pyautogui.alert() pyautogui.confirm() pyautogui.prompt() pyautogui.password()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import pyautoguipyautogui.alert(text='警告内容' , title='警告框' , button='OK' ) result = pyautogui.confirm(text='请选择' , title='确认框' , buttons=['确定' , 'Cancel' ]) print (result)content = pyautogui.prompt(text='' , title='请输入' , default='' ) print (content)content = pyautogui.password(text='' , title='' , default='' , mask='*' ) print (content)
截图定位 1 2 3 pyautogui.screenshot(path,regon) pyautogui.locateOnScreen(region,grayscale) pyautogui.locateCenterOnScreen()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import pyautoguipyautogui.screenshot("./images/1.png" , region=(0 , 0 , 500 , 500 )) rect = pyautogui.locateOnScreen('./images/1.png' ) if rect: print (type (rect)) print (rect[0 ], rect[1 ], rect[2 ], rect[3 ]) x, y = pyautogui.center(rect) print () point = pyautogui.locateCenterOnScreen('./images/1.png' ) if point: print (type (point)) x, y = point print (x, y)
修改可以提供定位效率的参数 1 2 3 4 5 6 7 8 9 10 import pyautoguipyautogui.screenshot("./images/1.png" , region=(0 , 0 , 500 , 500 )) rect = pyautogui.locateOnScreen('./images/1.png' , confidence=0.6 ) print (rect)rect = pyautogui.locateOnScreen('./images/1.png' , region=(0 , 0 , 1000 , 1000 )) print (rect)rect = pyautogui.locateOnScreen('./images/1.png' , grayscale=True ) print (rect)
具体代码案例 我们可以根据上面的知识点做个小小的例子,就是,自动打开记事本,然后输入内容,再保存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import pyautoguiimport pyperclipimport osimport timedef write_word (texts="" , interval=0.1 ): for t in texts: pyperclip.copy(t) pyautogui.hotkey("ctrl" , "v" ) time.sleep(interval) def main (): res = os.system("start notepad" ) if res != 0 : print ("打开记事本失败" ) return time.sleep(2 ) pyautogui.PAUSE = 1 x, y = pyautogui.locateCenterOnScreen("./images/notepad.jpg" , confidence=0.6 , grayscale=True ) pyautogui.click(x, y) texts = "hello world," write_word(texts, 0.1 ) pyautogui.hotkey("ctrl" , "s" ) write_word("自动文档.txt" , 0.1 ) pyautogui.press("enter" ) pyautogui.press("y" ) pyautogui.hotkey("alt" , "f4" ) if __name__ == '__main__' : main()