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

FeatureCode Snippet
Move mousepyautogui.moveTo(x, y, duration=1)
Clickpyautogui.click()
Type textpyautogui.write("text")
Press keypyautogui.press("enter")
Hotkey combopyautogui.hotkey("ctrl", "c")
Take screenshotpyautogui.screenshot()
Find image on screenpyautogui.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

0
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