Python PyAutoGUI Module
PyAutoGUI lets your Python scripts control the mouse and keyboard to automate interactions with other applications. It works on Windows, macOS, and Linux.
Installation
pip install pyautogui
Importing the Module
import pyautogui
Safety Features
Fail-Safe
If your script goes out of control, you can move the mouse cursor to any of the four corners of the primary screen to trigger a pyautogui.FailSafeException, stopping the script.
pyautogui.FAILSAFE = True # Enabled by default
Pauses
You can add a delay after each PyAutoGUI call to give the application time to process the input.
pyautogui.PAUSE = 2.5 # 2.5 seconds pause
Mouse Control
Getting Screen Size and Position
width, height = pyautogui.size()
x, y = pyautogui.position()
print(f"Screen: {width}x{height}, Mouse: ({x}, {y})")
Moving the Mouse
# Move to absolute coordinates (x, y) over 'duration' seconds
pyautogui.moveTo(100, 100, duration=1)
# Move relative to current position
pyautogui.moveRel(0, 50, duration=1) # Move down 50 pixels
Clicking and Dragging
pyautogui.click() # Click at current position
pyautogui.click(200, 200) # Click at specific coordinates
pyautogui.doubleClick()
pyautogui.rightClick()
# Drag mouse to coordinates
pyautogui.dragTo(300, 300, duration=1)
Keyboard Control
Typing Strings
# Type with a delay between key presses
pyautogui.write('Hello world!', interval=0.25)
Pressing Keys
pyautogui.press('enter')
pyautogui.press(['left', 'left', 'left']) # Press left arrow 3 times
Hotkeys
# Simulates holding Ctrl, pressing c, then releasing both
pyautogui.hotkey('ctrl', 'c')
Message Boxes
PyAutoGUI provides simple message box functions.
pyautogui.alert('This is an alert box.')
response = pyautogui.confirm('Shall I proceed?')
print(response) # 'OK' or 'Cancel'
Screenshot Functions
PyAutoGUI can take screenshots and locate images on the screen.
Taking a Screenshot
im = pyautogui.screenshot()
im.save('screenshot.png')
Locating Images
You can find coordinates of an image on the screen. This is useful for finding buttons.
# Returns (left, top, width, height) of the first match
button_location = pyautogui.locateOnScreen('submit_button.png')
if button_location:
print(f"Button found at: {button_location}")
pyautogui.click(pyautogui.center(button_location))