PyAutoGUI 自动化操作
功能概览
| 功能类别 |
支持的操作 |
| 截图 |
全屏截图、区域截图、截图到剪贴板 |
| 鼠标控制 |
点击、双击、移动、相对移动、拖拽、滚动、按下/释放 |
| 颜色操作 |
获取像素颜色、查找颜色位置 |
| 键盘操作 |
输入文本、按键、组合键、快捷操作(复制/粘贴/全选等) |
| 图像识别 |
在屏幕上查找图片位置、等待图片出现/消失 |
| 对话框 |
警告、确认、输入对话框 |
| 系统信息 |
屏幕分辨率、鼠标位置、窗口信息 |
| 工具 |
等待、暂停 |
快速开始
基本使用模式
python scripts/automation.py <action> [参数...]
所有操作返回 JSON 格式结果。
脚本位置: scripts/automation.py
操作详解
截图
python scripts/automation.py screenshot
python scripts/automation.py screenshot --output my_screenshot.png
python scripts/automation.py screenshot --output region.png --region 100,100,400,300
python scripts/automation.py screenshot_to_clipboard
python scripts/automation.py screenshot_to_clipboard --region 100,100,400,300
鼠标点击
python scripts/automation.py click --x 100 --y 200
python scripts/automation.py click --x 100 --y 200 --button right --clicks 2
python scripts/automation.py double_click --x 100 --y 200
颜色操作
python scripts/automation.py get_pixel_color --x 100 --y 200
python scripts/automation.py find_color --rgb 255,255,255
python scripts/automation.py find_color --rgb 255,255,255 --tolerance 10
python scripts/automation.py find_color --rgb 255,0,0 --region 0,0,800,600
鼠标控制
python scripts/automation.py get_mouse_position
python scripts/automation.py move_mouse --x 500 --y 300
python scripts/automation.py move_mouse --x 500 --y 300 --duration 0.5
python scripts/automation.py move_mouse_rel --x 100 --y -50
python scripts/automation.py move_mouse_rel --x 100 --y -50 --duration 0.5
python scripts/automation.py drag_mouse --x 800 --y 600 --duration 1.0
python scripts/automation.py mouse_down --button left
python scripts/automation.py mouse_up --button left
python scripts/automation.py scroll --amount 500
python scripts/automation.py scroll --amount -500 --x 500 --y 300
屏幕信息
python scripts/automation.py get_screen_size
python scripts/automation.py get_active_window
python scripts/automation.py get_all_windows
等待
python scripts/automation.py sleep --seconds 2
键盘操作
python scripts/automation.py type_text --text "Hello World"
python scripts/automation.py type_text --text "Hello" --interval 0.1
python scripts/automation.py press_key --key enter
python scripts/automation.py press_key --key esc
python scripts/automation.py hotkey --keys ctrl,c
python scripts/automation.py hotkey --keys ctrl,shift,esc
python scripts/automation.py copy
python scripts/automation.py paste
python scripts/automation.py cut
python scripts/automation.py select_all
python scripts/automation.py undo
python scripts/automation.py redo
python scripts/automation.py save
常用按键名称: enter, esc, tab, space, backspace, delete, up, down, left, right, f1-f12, ctrl, alt, shift, win
图像识别
需要安装 opencv-python 以使用 confidence 参数:
python scripts/automation.py locate_on_screen --image button.png
python scripts/automation.py locate_on_screen --image button.png --confidence 0.9
python scripts/automation.py locate_on_screen --image button.png --region 0,0,800,600
python scripts/automation.py locate_all_on_screen --image icon.png
python scripts/automation.py wait_for_image --image button.png --timeout 10
python scripts/automation.py wait_for_image --image loading.png --timeout 30 --wait_interval 1
python scripts/automation.py wait_for_image_to_vanish --image loading.png --timeout 30
对话框
python scripts/automation.py alert --title "提示" --text "操作完成"
python scripts/automation.py confirm --title "确认" --text "是否继续?"
python scripts/automation.py confirm --title "选择" --text "请选择操作" --buttons "保存,不保存,取消"
python scripts/automation.py prompt --title "输入" --text "请输入名称:" --default "默认值"
安全设置
脚本已启用以下安全保护:
- FAILSAFE: 将鼠标快速移动到屏幕左上角会触发异常停止
- PAUSE: 每个操作后有 0.1 秒默认暂停
完整示例
自动化登录流程
python scripts/automation.py screenshot --output login_start.png
python scripts/automation.py click --x 500 --y 300
python scripts/automation.py type_text --text "myusername"
python scripts/automation.py press_key --key tab
python scripts/automation.py type_text --text "mypassword"
python scripts/automation.py click --x 500 --y 400
python scripts/automation.py sleep --seconds 3
python scripts/automation.py screenshot --output login_end.png
颜色检测自动化
color=$(python scripts/automation.py get_pixel_color --x 100 --y 100)
图像定位点击
result=$(python scripts/automation.py locate_on_screen --image submit_button.png --confidence 0.9)
依赖安装
脚本会自动安装必需的依赖:
pyautogui: 核心自动化库
pillow: 图像处理
可选依赖(用于图像识别置信度):
pip install opencv-python
资源索引
注意事项
- 坐标系: 屏幕左上角为原点 (0, 0),向右为 X 增加,向下为 Y 增加
- 权限: Windows 上可能需要以管理员权限运行某些操作
- 分辨率: 多显示器环境下,坐标可能跨越多个屏幕
- 图像识别: 受屏幕分辨率和缩放比例影响,建议使用 confidence 参数提高鲁棒性
快速参考表
| 用户输入 |
AI 行动 |
| "xxx" |
直接执行 xxx |