Automate Your Desktop with Python and PyAutoGUI


Ever wanted your Python script to move your mouse, type text, click buttons, or take screenshots — just like a human?
With PyAutoGUI
, you can create your own desktop automation bot. Whether you’re filling out repetitive forms, auto-clicking buttons, or testing GUIs, this library saves time and effort.
In this post, we’ll build a simple bot that:
Moves the mouse
Clicks on screen elements
Types automatically
Takes screenshots
Adds delays and failsafes
🛠️ Step 1: Install PyAutoGUI
bashCopyEditpip install pyautogui
🧪 Step 2: Your First Automation Script
pythonCopyEditimport pyautogui
import time
# Wait 3 seconds so you can switch windows
time.sleep(3)
pyautogui.moveTo(500, 400, duration=1)
pyautogui.click()
pyautogui.typewrite("Hello, world!", interval=0.1)
pyautogui.press("enter")
This will move your mouse to a position, click, type some text, and press Enter.
📸 Step 3: Take a Screenshot
pythonCopyEditscreenshot = pyautogui.screenshot()
screenshot.save("screenshot.png")
You can even detect on-screen elements:
pythonCopyEditlocation = pyautogui.locateOnScreen("button.png", confidence=0.8)
if location:
pyautogui.click(location)
🧯 Safety First!
Always include this line to prevent runaway scripts:
pythonCopyEditpyautogui.FAILSAFE = True # Move your mouse to a corner to stop
🧰 Useful Features
Feature | Code Snippet |
Move mouse | pyautogui.moveTo(x, y, duration=1) |
Click | pyautogui.click () |
Type text | pyautogui.write("text") |
Press key | pyautogui.press ("enter") |
Hotkey combo | pyautogui.hotkey("ctrl", "c") |
Take screenshot | pyautogui.screenshot() |
Find image on screen | pyautogui.locateOnScreen("img.png") |
💡 Real Use Cases
Auto-fill forms from Excel/CSV
GUI testing and QA automation
Auto-login to websites
Creating keyboard macros
Repetitive software actions (data entry, exporting reports)
⚠️ Limitations
It’s pixel-based — screen resolution and DPI affect results
It doesn't "see" inside apps like Selenium does with browsers
Dynamic UI elements can break image recognition
Subscribe to my newsletter
Read articles from Ashraful Islam Leon directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ashraful Islam Leon
Ashraful Islam Leon
Passionate Software Developer | Crafting clean code and elegant solutions