PyAutoGUI is a powerful Python library that allows you to control the mouse and keyboard programmatically. With PyAutoGUI, you can automate repetitive tasks, create custom shortcuts, and even develop simple bots. In this article, we will explore various features of PyAutoGUI, including how to install the library, interact with the mouse and keyboard, and use screenshots to control your applications.
Before we can start using PyAutoGUI, we need to install it. You can install the library using pip by running the following command:
pip install pyautogui
Once installed, you can import the library in your Python script:
import pyautogui
PyAutoGUI provides several functions to control the mouse, such as moving the cursor, clicking, scrolling, and dragging.
To move the cursor to a specific position on the screen, use the moveTo()
function:
pyautogui.moveTo(x, y, duration=seconds)
For example, to move the cursor to the position (100, 200) in 2 seconds:
pyautogui.moveTo(100, 200, duration=2)
To simulate a mouse click, use the click()
function:
pyautogui.click(x, y, button='left')
For example, to click at position (100, 200) with the right mouse button:
pyautogui.click(100, 200, button='right')
To scroll the mouse wheel, use the scroll()
function:
pyautogui.scroll(units, x=None, y=None)
For example, to scroll up by 200 units:
pyautogui.scroll(200)
To scroll down by 200 units:
pyautogui.scroll(-200)
PyAutoGUI enables you to send keystrokes, type strings, and perform keyboard shortcuts.
To simulate a single key press, use the press()
function:
pyautogui.press('key')
For example, to press the ‘enter’ key:
pyautogui.press('enter')
To type a string, use the typewrite()
function:
pyautogui.typewrite('text', interval=seconds)
For example, to type the string ‘hello world’ with a delay of 0.1 seconds between each character:
pyautogui.typewrite('hello world', interval=0.1)
To perform a keyboard shortcut, use the hotkey()
function:
pyautogui.hotkey('key1', 'key2', ...)
For example, to perform the ‘Ctrl+C’ shortcut:
pyautogui.hotkey('ctrl', 'c')
PyAutoGUI can obtain screen information, such as screen size and pixel color, and perform image recognition.
To get the screen size, use the size()
function:
width, height = pyautogui.size()
To get the color of a pixel, use the pixel()
function:
color = pyautogui.pixel(x, y)
To locate an image on the screen, use the locateOnScreen()
function:
location = pyautogui.locateOnScreen('image.png', confidence=0.9)
The confidence
parameter specifies the required match accuracy (0 to 1).
Let’s create a simple script that opens the ‘Notepad’ application, types some text, and saves the file:
import pyautogui
import time
## Open Notepad
pyautogui.hotkey('win', 'r')
time.sleep(0.5)
pyautogui.typewrite('notepad', interval=0.1)
pyautogui.press('enter')
time.sleep(2)
## Type some text
pyautogui.typewrite('Hello, PyAutoGUI!\n', interval=0.1)
time.sleep(1)
## Save the file
pyautogui.hotkey('ctrl', 's')
time.sleep(1)
## Type the file name and save
pyautogui.typewrite('example.txt', interval=0.1)
pyautogui.press('enter')
time.sleep(1)
## Close Notepad
pyautogui.hotkey('alt', 'f4')
This script demonstrates how to combine mouse and keyboard interactions using PyAutoGUI to automate a simple task. With a bit of creativity and practice, you can utilize PyAutoGUI to automate more complex tasks and save time in your daily workflow.