Pyautogui is a cross platform gui module that enables you to perform keyboard and mouse actions through your python scripts. When you combine this with other modules you can easily create scripts that will automate tasks that require you to go web pages, fill out forms, download music and so on. Pyautogui is not a standard python library, so to use it you must first install it on your computer. The easiest way to do it is to use the command:
Windows: pip install pyautogui or Mac/Linux: pip3 install pyautogui.
Once you have it installed, you can import it like any other module.
To begin using pyautogui for mouse functions, you will need to know the exact location of the object on the screen you want to interact with. So you typically use these functions in the python repl to find the location of the object you want:
Note* To access the repl open your terminal/command window and type in your version of Python such as “Python3.8” and it will open a section where you can run python code in your terminal.
Once you have the location of the items you want to interact with, you can perform functions like moving to that location, clicking, scrolling and highlighting (via dragging).
As mentioned above you can also use pyautogui to perform keyboard functions like typing in keys to a form or using keyboard shortcuts like ctrl c to copy and paste in your scripts.
Message Box Functions
If you want to pause your script and have your user presented with a message box, pyautogui provides functions for that:
Lastly, pyautogui comes with the ability to take screenshots, and to save the results to files that can be used later on.
Pyautogui gives you many options to make your scripts more dynamic and to some extent allows you to mimic human behaviour. What we discussed in this article is only a portion of what this library has to offer, if you find this interesting and what more to learn more here a couple of good resources. The official pyautogui cheat sheet that tells you about all of the functions of pyautogui. There’s also a chapter dedicated to pyautogui in Automate the Boring Stuff with Python that covers it in detail, provides exercises and project ideas if you want to build real practical applications or build up your portfolio.