1. Introduction to PyAutoGUI library
PyAutoGUI is the most comprehensive graphical interface automation library in the Python ecosystem. It realizes cross-platform (Windows/macOS/Linux) automation tasks by simulating mouse, keyboard operations and screen image recognition. Its core value lies in:
- Liberate repetitive labor: Automatic data entry, file organization, report generation and other time-consuming operations, and the efficiency is improved by more than 90%.
- Precise control capability: supports pixel-level coordinate positioning (Error ±0.5 pixels), sub-second operation delay (can be configured with 0.01 second accuracy).
- Intelligent image recognition: By matching dynamic positioning interface elements (such as buttons and text boxes) through screenshots, we solve the problem of script failure caused by changes in window position.
- Safety and reliable: Built-in anti-touch mechanism (such as failure safety points), operation logging and multi-threaded safety locks to ensure the stability of the automation process.
2. Installation and environment configuration
Installation command:
pip install pyautogui # Core Librarypip install Pillow # Image processing dependency(Screenshot function required)
Verify installation:
import pyautogui print(pyautogui.__version__) # Output version number(like 0.9.54)
Global security configuration (prevent scripts from getting out of control):
= True # Automatically terminate the program when the mouse moves to the upper left corner [2](@ref) = 0.5 # Pause after each operation 0.5 Second[2](@ref)
3. Core functions and common functions
1. Mouse control
Functions/Methods | Function description | Parameter description |
---|---|---|
(x, y, duration)
|
Move the mouse to absolute coordinates (x, y) |
duration : Movement takes time (seconds), set to 0 and complete instantly |
(dx, dy, duration) |
Offset relative to the current position (dx, dy) | Commonly used to dynamically adjust positions (such as dragging files) |
(x, y, clicks, button) |
Click operation (left button/right button/double click) |
button :'left' (default),'right' 、'middle'
|
(x, y, duration)
|
Drag to the target position | You need to hold down the mouse first, which is often used for file drag and drop or drawing software operations |
(units)
|
Scroll the mouse wheel |
units : Positive numbers scroll upwards, negative numbers go downwards |
Example: Automated file drag and drop
(100, 200, duration=1) # Move to file location(500, 300, duration=2) # Drag to the target folder
2. Keyboard operation
Functions/Methods | Function description | Parameter description |
---|---|---|
(text, interval)
|
Simulate the keyboard to enter text |
interval : Character input interval time (seconds) |
(keys)
|
Press and release a single key (such as'enter' 、'tab' ) |
Supports 200+ keys (seepyautogui.KEYBOARD_KEYS ) |
(*keys)
|
Combining shortcut keys (such as'ctrl+c' ) |
Automatically process the key sequence (pressctrl → Pressc → Releasec → Releasectrl ) |
(key) / keyUp(key)
|
Handle the pressing and release of keys separately | Used for long pressing (such as game character movement) |
Example: Automatically fill in the form
('Zhang San', interval=0.1) # Enter a name('tab') # Switch to the next input box('13812345678') # Enter your mobile phone number('ctrl', 's') # Save form[6](@ref)
3. Screen and image recognition
Functions/Methods | Function description | Parameter description |
---|---|---|
(region)
|
Screening image in the screen area |
region : Specify the area (x, y, width, height), default full screen |
(image, confidence)
|
Match the target image position on the screen |
confidence : Matching accuracy (0~1), recommended above 0.8 |
(image)
|
Returns the center coordinate of the matching image | Commonly used to click on buttons |
(x, y)
|
Get the RGB color value of the specified pixel point | Used to determine the interface status (such as whether the button is highlighted) |
Example: Smart click on dynamic buttons
button_pos = ('submit_button.png', confidence=0.9) if button_pos: x, y = (button_pos) (x, y) # Click the button center [1,4](@ref)else: raise Exception("Submit button not found")
4. Window management and advanced control
Functions/Methods | Function description | Parameter description |
---|---|---|
(title)
|
Get the window object with the specified title | Need to cooperatepygetwindow Library usage |
(text)
|
A confirmation dialog box pops up | Block the program until the user clicks |
()
|
Return to screen resolution (width, height) | Used to calculate relative coordinates |
Example: Multi-window collaborative operation
# Activate the Notepad window and enter contentnotepad = ('Notepad')[0] () ('Automatically enter text', interval=0.1)
4. Application scenarios and practical cases
-
Automated office
- Excel report generation: Automatically open files, fill data and save them through hotkey operations, which takes from 2 hours to 2 minutes.
- Mail sending: Automatically fill in recipients, topics and attachments, and combine Outlook to achieve unattended processing.
-
Data acquisition and testing
- Dynamic web page screenshots: Timely screenshot web page data and save it to monitor price or inventory changes.
- UI automation testing: Verify the software interface response, replacing manual click operation.
-
Games and entertainment
- Automatic combo script: Simulate key sequences in the game to achieve continuous skill release.
- Video automatic playback: Skip advertisements through image recognition and automatically switch episodes.
5. Precautions and optimization techniques
-
Compatibility optimization
- The coordinate systems of different operating systems may differ (such as the height of the menu bar in macOS), and the offset needs to be calculated dynamically.
- High-resolution screens need to adjust image recognition parameters (such as
confidence=0.95
)。
-
Performance improvement
- use
()
Batch identify multiple targets to reduce the number of screenshots. - Locking is required when multithreading is enabled (
with ()
) to prevent operational conflicts.
- use
-
Exception handling
- Capture
To handle image recognition failure.
- Record operation logs (
()
) Easy to debug.
- Capture
This is the end of this article about the use of the PyAutoGUI library in Python. For more related content of the Python PyAutoGUI library, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!