# desktopauto —— 桌面自动化库 面向「拿不到元素树」的桌面程序:识图点击、文字定位、键鼠操作、窗口与进程管理,多插件、工作流、脚本共用。 只依赖 `opencv-python + numpy + pillow`(OCR 另需 `rapidocr` + `onnxruntime`),Windows 下用 ctypes 调系统 API,**不依赖 pyautogui / pywin32 / 任何底座代码**。 > 配套技能:`.trae/skills/37du-Desktop-Auto/SKILL.md`(AI 使用总纲 + `references/api-reference.md` 完整签名)。 > 本库原名 `screenvision`,已整体改名为 `desktopauto`(未发布、无历史引用)。 ## 作为 SDK 下发 本库按 SDK 发布(`sdk.json`,id `desktopauto`),执行机按插件 `plugin.json.requires.sdks` 下载并挂载到 `sys.path`; 版本号须同时更新 `sdk.json` / `__version__` / `pyproject.toml`。打包与发版命令见 `.skills/37du-SDK-Deploy/SKILL.md`。 - 包内 `desktopauto/_selftest.py`:安装冒烟(纯离线,不碰真实桌面 / 窗口 / 进程 / OCR); 库根 `_selftest.py`:开发态全量自测。 - **禁止预装进 python-env,也禁止 vendored 复制进插件包**:同名包会触发 `SDK_PACKAGE_COLLISION`, 挂载时也会因「SDK 包被环境包遮蔽」fail-fast。 ## 能力 | 能力 | 接口 | |---|---| | 识图是否存在 | `exists(tpl)` | | 找最佳命中(含坐标/相似度) | `find(tpl)` | | 找全部命中 | `find_all(tpl)` | | 等图出现(超时返回 None) | `wait(tpl, timeout=30)` | | 识图点击(找到即点中心) | `click(tpl)` | | 点坐标 | `click_at(x, y)` | | 截图(全屏/区域/窗口) | `screenshot(path)` | | **离线图片标记**(不碰屏幕,框出命中位置) | `mark(image, [tpl1, tpl2], out_path)` | | **文字识别取坐标** | `read_text()` → `[TextItem(text, center, score)]` | | **身份证 / 银行卡照片识别** | `ocr_card(path)` / `ocr_cards(folder)` → 号码、姓名、发卡行、校验结果 | | **按文字判断/等待/点击** | `text_exists("导出")` / `wait_text` / `click_text` | | **多状态按钮** | `find_any([tpl可用, tpl禁用])` → `(命中, 模板名)` | | **输入文本(中文)** | `paste("内容")`(写剪贴板+Ctrl+V)/ `type_text` | | **按键 / 组合键** | `press("enter")` / `hotkey("ctrl","s")` | | **按键串(影刀写法)** | `send_keys("^a{ENTER}")` / `send_keys("%{F4}")` | | **移动鼠标** | `move_mouse(x, y, relative_to="window")`(支持 instant/fast/middle/slow) | | **等待(含随机)** | `sleep(1.5)` / `sleep(random_range=(1, 3))` | | **文件 / 文件夹** | `fs.exists / write_text / read_text / copy / move / list_files / newest_file / wait_file` | | **等所有图都出现** | `wait_all([a, b], timeout=20)` | | **滚轮 / 拖拽** | `scroll(notches, at=tpl)` / `drag(起点, 终点)`(起点终点可传模板) | | **找色** | `find_color("#FF3B30", tolerance=12)` → `[ColorMatch]` | | **界面差异** | `diff(基准图)` → 变化比例 + 变化区域框 | | 窗口多条件查找 | `find_windows(process="DingTalk.exe")`、`wait_window`、`child_windows` | | 限定窗口 / 区域 | `Screen(window="钉钉")`、`Screen(region=(l,t,r,b))` | 特性:多尺度匹配(跨 DPI 复用同一套模板)、多目标 + NMS 去重、低纹理模板自动降级为平方差匹配(防纯色误命中)、透明 PNG 白底合成、命中/未命中调试图落盘、DPI 感知(125%/150% 缩放坐标不偏移)。 ## 安装 ```powershell # 开发机 / .venv d:\work\code\37duAutomation\.venv\Scripts\python.exe -m pip install -e libs\desktopauto # 执行机便携环境包(推荐:装一次,所有插件都能 import) d:\work\code\37duAutomation\desktop\electron-shell\python-env\<版本>\python.exe -m pip install d:\work\code\37duAutomation\libs\desktopauto # 文字识别(OCR)依赖:RapidOCR + onnxruntime,模型随包内置,离线可用 pip install rapidocr onnxruntime ``` 装好后任何脚本/插件直接 `import desktopauto`。 > 只用「识图」(模板匹配)时不需要装 OCR;`ocr_available()` 可探测。 ### 插件里怎么用(三种接入方式) | 方式 | 做法 | 适用 | |---|---|---| | **A. 环境包预装**(推荐) | 执行机环境包 `pip install desktopauto`,插件 `requirements.txt` 里写 `desktopauto`(仅声明意图,不重复装) | 多插件共用,升级只动一处 | | **B. 打包进插件** | 把 `desktopauto/` 目录复制到插件包内,插件内相对导入 | 需要插件包完全自包含、离线拷走即用 | | **C. 开发期 sys.path** | `sys.path.insert(0, r"d:\work\code\37duAutomation\libs\desktopauto")` | 本地调试图省事,**不要进发布包** | > 注意:项目规范要求「插件包内不得 import 插件之外的代码」。选 A 时要在插件 README/规范里明确 `desktopauto` 属于**执行机预装依赖**(与 requests/pandas 同级),不是跨目录 import 业务代码;选 B 则天然合规。 ## 快速上手 ```python from desktopauto import Screen s = Screen() # 全屏 if s.exists("img/btn_save.png"): # 识图:是否存在 print("按钮在") s.click("img/btn_save.png") # 识图点击:找到才点,找不到返回 False # 限定窗口(标题模糊匹配,自动排除最小化窗口)+ 等图出现 ding = Screen(window="钉钉", threshold=0.9, debug_dir="debug") ding.activate() hit = ding.wait("img/chat_input.png", timeout=30) if not hit: raise RuntimeError("钉钉窗口没准备好") ding.click("img/chat_input.png") print(hit.center, hit.score) # (x, y) 屏幕物理像素、相似度 # 一个窗口里同款图标有多个时 for m in ding.find_all("img/row_more.png"): ding.click_at(*m.center) ``` 临时覆盖参数(不改实例配置): ```python s.find("a.png", threshold=0.95, window="WPS", region=(0, 0, 800, 600)) s.find_all("a.png", scales=(0.9, 1.0, 1.1), max_results=5) # 跨 DPI 找图 ``` ## RPA 指令对照(按常见 RPA 工具的分类) | 指令 | 本库 | 返回 | |---|---|---| | 条件判断 · IF 图像存在 | `image_exists(tpl)` | `bool` | | 条件判断 · IF 文字存在 | `text_exists("导出")` | `bool` | | 条件判断 · IF 颜色存在 | `s.color_exists("#FF3B30")` | `bool` | | 等待 · 等待图像 | `wait_image(tpl, timeout=10)` | `Match` / `None` | | 等待 · 等待图像消失 | `wait_gone(tpl, timeout=10)` | `bool` | | 等待 · 等待文字 | `wait_text_exists("成功", timeout=10)` | `TextItem` / `None` | | 等待 · 等待任一(图或文字) | `wait_any(templates=[a, b], texts=["成功","失败"], timeout=30)` | `{kind, target, match}` / `None` | | AI/OCR · 通用文字识别 | `ocr_text()` | `str`(纯文本,适合喂 LLM) | | AI/OCR · 通用文字识别(含位置) | `ocr_text_with_position()` = `read_text()` | `[TextItem]`(含坐标/置信度) | | 鼠标键盘 · 点击图像 | `click_image(tpl)` | `bool`(找到才点) | | 鼠标键盘 · 鼠标悬停在图像上 | `hover_image(tpl)` | `bool`(不点击) | | 鼠标键盘 · 悬停到文字 | `hover_text("导出")` | `bool` | | 鼠标键盘 · 输入文本 | `paste(text)` / `type_text(text)` | `None` | | 鼠标键盘 · 按键 / 组合键 | `press("enter")` / `hotkey("ctrl","s")` | `None` | | 鼠标键盘 · 滚轮 / 拖拽 | `scroll(-5, at=tpl)` / `drag(tpl, (x,y))` | `None` | 一次典型串联: ```python from desktopauto import (Screen, click_image, paste, hotkey, wait_any, ocr_text) s = Screen(window="钉钉", threshold=0.9, debug_dir="debug") s.activate() if not s.exists("img/dialog_closed.png"): # 条件判断 s.click("img/btn_close.png") s.click("img/chat_input.png") paste("排班识别,水庄大鹅……") # 中文输入 hotkey("ctrl", "enter") r = wait_any(texts=["发送成功", "发送失败"], timeout=20) # 等任一结果 print(r["kind"], r["target"], r["match"].center if r else None) print(ocr_text()) # 整屏文字(含坐标用 ocr_text_with_position) ``` ## 输入:文本 / 按键 / 滚轮 / 拖拽 ```python from desktopauto import Screen, keys, clipboard s = Screen(window="钉钉") s.activate() s.click("img/chat_input.png") # 先点中输入框 s.paste("排班识别,水庄大鹅……") # 中文走剪贴板 + Ctrl+V,最稳 s.hotkey("ctrl", "enter") # 组合键发送 s.send_keys("^a{DELETE}") # 影刀写法:全选后删除(^ = Ctrl,{KEY} = 单键) s.press("tab", times=2) # 单键/连按 s.move_mouse(320, 180, relative_to="window") # 相对窗口坐标移动鼠标 # 影刀「点击图像」的参数也能直接对上:timeout 等图、delay_after 点后延时、anchor 九宫格 s.click("images/提交申报按钮.png", timeout=5, delay_after=1, anchor="center") s.type_text("A001", method="unicode") # 纯英文可逐字符注入 s.scroll(-5, at="img/list_area.png") # 在指定位置向下滚 5 格 s.drag("img/slider.png", (600, 400)) # 拖拽:起点可传模板,终点传坐标 print(clipboard.get_text()) # 读剪贴板(比 OCR 准:先 Ctrl+A/Ctrl+C) ``` 要点: - **中文一律 `paste`**(写剪贴板 + Ctrl+V)。`type_text` 默认 `method="auto"`,含非 ASCII 时自动走剪贴板; - `paste(restore=True)` 会在粘贴后**恢复你原来的剪贴板内容**; - `press`/`hotkey` 只支持命名键 + 字母/数字/F1~F12;**符号请用 `type_text`**(避免键盘布局差异); - 目标窗口以管理员权限运行时,普通权限进程的输入会被 UIPI 拦截(`SendInput` 返回 0,库会抛错说明原因)。 ## 窗口对象(RPA 的「获取窗口对象」「激活软件窗口」) ```python from desktopauto import get_window, foreground_window, activate_window # 获取窗口对象:按进程/标题/PID/类名,找不到直接抛错(提示写明条件) w = get_window(process="DingTalk.exe") w = get_window(title="银行", exact=True) # 标题精确匹配 w = get_window(process="DingTalk", index=1) # 同进程第二个窗口 w = get_window(title="导出", timeout=15) # 等它出现再返回 print(w.title, w.pid, w.process, w.rect, w.center) w.activate() # 激活软件窗口(还原+置前) w.is_foreground() # 是否在前台 w.maximize().move(0, 0) # 最大化/移动(链式) w.wait_ready(timeout=10) # 等可见且非最小化 s = w.screen(threshold=0.9, debug_dir="debug") # 直接拿到绑定该窗口的 Screen s.click("img/btn_send.png") w.screenshot("w.png") # 只截这个窗口 w.children() # 子窗口列表 w.close(); w.wait_closed(timeout=10) # 关闭并等它消失 w.exists() # 窗口是否还在(不抛错) foreground_window() # 当前前台窗口对象 activate_window("钉钉") # 也可直接用函数式激活 ``` `Window` 的属性都是实时读取的:窗口被手动关掉后,访问 `title` 会抛错,用 `exists()` 先判断。 ### 关闭窗口的三种力度 ```python from desktopauto import close_window close_window("钉钉") # ① 函数式:发 WM_CLOSE(等同点 ×) w.close() # ② 对象式,同上 w.close(); w.wait_closed(timeout=10) # ③ 关完等它真的消失(返回 bool) w.kill() # ④ 强杀:TerminateProcess 整个进程(应用无响应/有确认弹框时用) ``` - `close()` 只是**发消息**,应用若弹「是否保存」不会自动处理,需要你再点一下; - `kill()` 会**杀掉整个进程**(该应用所有窗口一起没,未保存数据会丢),仅在卡死时用; - 判断关没关掉一律用 `w.exists()` / `w.wait_closed()`,别用 sleep 猜。 ## 运行或打开 / 终止程序 ```python from desktopauto import (run_or_open, launch, open_path, open_url, is_running, wait_process, list_processes, kill_process, get_window) run_or_open(r"D:\Program\DingTalk\DingTalk.exe") # 启动 exe run_or_open(r"D:\资料\工资卡核对结果.xlsx") # 用默认程序打开(WPS/Excel) open_url("https://hr.37du.co") # 打开网址 # 启动 → 等进程 → 拿窗口 → 操作 run_or_open("notepad.exe", wait=1) wait_process("notepad", timeout=10) # 等进程出现 w = get_window(title="记事本", timeout=10).activate() # 终止 kill_process("notepad") # 同名全部终止 kill_process(w) # 按窗口对象(终止其进程) kill_process(12345) # 按 pid ``` | 函数 | 说明 | |---|---| | `run_or_open(target, args=(), wait=0)` | `.exe/.bat/.cmd/.com` → 启动返回 Popen;文档/文件夹/网址 → 默认程序打开 | | `launch(exe, args=(), cwd=None, wait=0)` | 只启动程序,返回 Popen(可 `.poll()` / `.terminate()`) | | `open_path(path)` / `open_url(url)` | 用系统默认程序打开 | | `list_processes(name="")` | 进程列表 `[{pid, name, path}]` | | `is_running(name)` | 是否在运行(名称包含匹配) | | `wait_process(name, timeout, until="start")` | 等启动;`until="exit"` 等退出 | | `kill_process(pid / 名称 / Window)` | 终止程序(同名全部杀;找不到抛错) | 注意: - `kill_process` 走 `TerminateProcess`,**不给应用保存机会**; - 只想关一个窗口用 `w.close()`,别为了关窗口把整个进程杀了; - 终止失败多为权限问题:目标以管理员运行、本进程不是 → 会抛错说明。 ## 窗口定位:别只靠标题 ```python from desktopauto import find_windows, wait_window, child_windows, window_info, activate_window find_windows(process="DingTalk.exe") # 按进程名(不区分大小写、包含匹配) find_windows(title="工资卡核对结果", exact=True) # 标题精确匹配(Excel 标题带文件名时用) find_windows(pid=27600) # 按 PID find_windows(class_name="Chrome_WidgetWin_1") # 按窗口类名 find_windows(title="钉钉", include_minimized=True) # 连最小化的一起列 w = wait_window(title="导出", timeout=15) # 等弹窗出现,返回窗口信息或 None print(w["title"], w["pid"], w["process"], w["rect"]) for c in child_windows(w["hwnd"]): # 子窗口(内嵌控件/弹层) print(c["class_name"], c["title"], c["rect"]) ``` > 实测钉钉客户端:`DingTalk.exe` / 类名 `Qt51511QWindowIcon` / 76 个子窗口 HWND, > 但没有可用的 UIA 元素树 —— 这正是「图 + 文字」路线要解决的问题。 ## 找色与界面差异 ```python from desktopauto import find_color, diff # 找红色状态点(RGB,容差 20),只要面积最大的 3 块 for c in find_color("#FF3B30", tolerance=20, min_area=8, max_results=3): print(c.center, c.area) # 判断界面有没有变化(比「找具体元素」更鲁棒;也能判断卡死/无响应) base = "img/state_before.png" r = diff(base, tolerance=12, min_area=50) print(r["changed"], r["changed_ratio"], r["boxes"]) ``` `Screen` 上还有 `s.find_color(...)` / `s.color_exists(...)` / `s.diff(baseline)`,会按实例的窗口/区域截图。 ## 命令行(不写代码先验证) ```powershell # 截图(用来裁模板) python -m desktopauto --mode shot --path img/btn.png --window "钉钉" # 判断是否存在(退出码 0=存在,1=不存在,2=出错) python -m desktopauto -t img/btn.png --mode exists # 识图点击 + 打调试图 python -m desktopauto -t img/btn.png --mode click --window "钉钉" --debug-dir debug # 离线标记:在一张截图里找多个模板,框出命中位置(不动真实鼠标) python -m desktopauto --mode mark -i shot.png -t "img/a.png,img/b.png" -o marked.png ``` ## 离线标记(先用标记验证,再上真实点击) 上线前不想真点鼠标时,用 `mark()` 拿一张截图先预演: ```python from desktopauto import mark, summarize result = mark(r"C:\Users\admin\Downloads\1.png", [r"C:\Users\admin\Downloads\2.png", r"C:\Users\admin\Downloads\3.png"], out_path="marked.png", threshold=0.85) print(summarize(result)) # 图片尺寸:1920x1032,命中合计 2 处 # #1 2.png:1 处 score=1.000 框=(148,395) 57x46 点击点=(176, 418) # #2 3.png:1 处 score=1.000 框=(2,372) 102x30 点击点=(53, 387) ``` 标注图上:绿/蓝/橙框 = 不同模板的命中框,框中心十字 = 真实 `click()` 会落下的位置。 确认标记位置都对,再把同样的模板交给 `Screen(...).click(...)` 真跑。 输出统一 JSON:`{"ok": true, "match": {"left":..,"top":..,"score":..,"center":[x,y]}}`。 ## 文字识别取坐标(OCR) ```python from desktopauto import Screen, match_text s = Screen() # 建议 Screen(window="钉钉") 限定窗口,又快又准 items = s.read_text() # 一次 OCR 拿全量:每项有 text / left / top / width / height / score / center for it in items[:5]: print(it.text, it.center) hit = s.find_text("导出") # 包含匹配 + 忽略大小写与空白,返回最可信的一条 print(hit.text, hit.center, hit.score) s.text_exists("提交") # 文字是否存在(不点击) s.wait_text("导出成功", timeout=30) # 等文字出现 s.click_text("导出") # 按文字点击其中心 # 一屏要查多个词:先 read_text() 拿一次再本地过滤,别反复 OCR items = s.read_text() for kw in ("导出", "刷新", "设置"): print(kw, [t.center for t in match_text(items, kw)]) ``` 匹配规则:默认「包含 + 忽略大小写 + 忽略空白」(OCR 常在字间插空格,`AI 表格` 能命中 `AI表格`);`exact=True` 精确相等,`regex=True` 正则。 CLI: ```powershell # 列出识别到的文字(带坐标),limit 控制输出条数 python -m desktopauto --mode ocr --limit 20 # 在一张截图上找文字并高亮标注(目标绿框,其余淡框) python -m desktopauto --mode ocr -i 1.png --text "导出,提交" -o hit.png # 实时截屏 + 限定窗口 + 只找某个词 python -m desktopauto --mode ocr --window "钉钉" --text "工作台" ``` 性能与坑: | 项 | 说明 | |---|---| | 速度 | 1920×1080 整屏约 2~4s(CPU);限定 `window/region` 后通常 <1s。引擎首次加载约 0.5s,之后复用 | | 精度 | 小字(<10px)、深色底白字、艺术字容易错;关键操作用「包含 + 短关键词」,别整句精确匹配 | | 坐标 | 实时截图返回**屏幕物理像素**,可直接 `click_text`;离线图(`mark` / `--image`)返回图内坐标 | | 稳定性 | OCR 是「看图」不是无障碍接口,界面改版就可能失效;重要流程建议「模板匹配 + OCR」互为兜底 | ## 证件照识别(身份证 / 银行卡) 拍照上传的身份证人像面、银行卡卡号面 → 结构化字段。纯离线(RapidOCR + onnxruntime), 模型随包,不联网;识别结果用**身份证校验码(模 11-2)/ 银行卡 Luhn** 把 OCR 认错的数字纠回来。 ```python from desktopauto import ocr_card, ocr_cards res = ocr_card(r"D:\照片\张三\银行卡.jpg", expected_name="张三") print(res["bank_card"]["number"], # 6214 8338 3144 5443 res["bank_card"]["luhn_ok"], # True res["bank_card"]["bank"], # 招商银行(BIN + 卡面文字双通道) res["flags"]) # ['duplicate_photo'] 等 rows = ocr_cards(r"D:\照片") # 按「姓名/图片」目录批量,同人多图合并、同图去重 ``` 输出(每个人的一条记录): | 字段 | 说明 | |---|---| | `id_card.number / checksum_ok / name / gender / birth / address / issue / valid_to` | 身份证:号码、校验是否通过、姓名、性别、出生、住址、签发机关、有效期 | | `bank_card.number / luhn_ok / length / bank / expiry / card_type` | 银行卡:卡号、Luhn、位数、发卡行、有效期、卡类型 | | `confidence` | `high / medium / low`(身份证、卡号各一份) | | `flags` | `id_not_found` `card_not_found` `id_checksum_fail` `card_luhn_fail` `duplicate_photo` `low_resolution` `rotated_90` … | | `digit_runs` | 照片里所有数字片段(和已知号码比对时找证据) | 识别策略(为什么不是「一步 OCR 全图」): 1. **粗识别**:缩到 1440 长边定位文字块(快); 2. **精修**:字段缺失时回原图按原生分辨率切块重识别(准); 3. **多路兜底**:灰度/CLAHE 整图、短边<800px 的缩略图 4 倍放大、数字带黑帽/Otsu/锐化(窄条图跳过检测网络,单路 0.1~0.3s); 4. **按行抓号码**:同一行合并、不同行切开,避免把卡号后面的有效期连成假号码; 5. **校验位裁判**:易混字符(`b→6/8`、`O→0`、`l→1`…)枚举候选,只有通过校验的才接受;多位可纠时**只认唯一解**,歧义宁可标人工。 实测(10 人真实样本,人工填真值):身份证号 **100%**、姓名 **80%**、银行卡号 **55.6%** 逐位正确; **认出来的 100% 正确**,错在「读不出」(缩略图/浮雕/反光),不读错。读不出的会带 `card_not_found` 交人工。 批量核对闭环(Excel 留「待填正确答案」列,填完回读算准确率): ```python from desktopauto import cardocr cardocr.write_review_workbook(rows, "核对表.xlsx") # 需要 openpyxl print(cardocr.verify_workbook("核对表.xlsx")) # 准确率统计 ``` 命令行: ```powershell python -m desktopauto.cardocr scan D:\照片 --json result.json --xlsx 核对表.xlsx python -m desktopauto.cardocr image D:\照片\张三\银行卡.jpg --name 张三 ``` 依赖:OCR 需 `rapidocr` + `onnxruntime`(可选依赖 `desktopauto[ocr]` / `desktopauto[card]`), 导出/回读 Excel 另需 `openpyxl`(`desktopauto[report]`);没装 openpyxl 时导入本库不受影响。 ## 模板图怎么准备 1. 用 `--mode shot` 截一张,再用画图工具裁出**目标区域**(比目标稍大一点、带完整边框/图标); 2. 模板要**有可辨识特征**:纯色/近似纯色的图块会被自动降级为像素匹配,容易在别处命中; 3. 避免把**会变的内容**裁进去(时间、数字、用户名、红点角标); 4. 同一元素在不同缩放机器上复用时,用 `scales=(0.8, 0.9, 1.0, 1.1, 1.25)`,或按机器各截一份; 5. 命中不稳定时先看 `debug_dir` 里的 `hit_*/miss_*.png`(带匹配框和分数),比盲调阈值快。 ## 常见坑 | 现象 | 原因 / 处理 | |---|---| | 图在屏幕上但点偏了 | 进程 DPI 未感知;本库首次调用已自动 `SetProcessDpiAwareness(2)`。若在别的进程里混用(如先被别的库改了 DPI 感知),需重启进程 | | 截出来是黑屏 | 远程桌面断开 / 锁屏 / 会话未交互;RPA 执行机要保持在已登录且解锁的会话 | | 窗口被别的窗口挡住还能找到? | 不能——本库截的是**屏幕实际显示内容**,不是窗口离屏渲染。点击前用 `activate()` | | 多显示器坐标是负的 | 正常,Windows 虚拟屏幕坐标;`grab()` 全屏包含所有屏幕 | | 相似度 0.85 命中了错的地方 | 提高阈值(0.9~0.95)、缩小 `window/region` 范围、或换更有特征的模板 | | 同款元素多个命中 | 用 `find_all()` 按坐标排序后取第 N 个,别用 `find()` 猜 | | 输入没反应 | 目标窗口以管理员运行 → UIPI 拦截;或窗口没焦点(先 `activate()` 再点目标) | | 粘贴出的是旧内容 | 写剪贴板后立即 Ctrl+V 有竞争,`paste(delay=0.15)` 加延迟;或 `restore=False` 别急着恢复 | | 窗口最小化/被挡时点不到 | 点击是物理坐标,窗口必须可见且在前台;最小化窗口会被 `find_windows` 默认排除 | ## 从影刀工程迁移 影刀工程导出的目录(含 `imagesV2.xml` + `resources/images/*.png` + `process*.py`)可以直接复用: 1. **模板图不用重截**:`scripts/import_yingdao_images.py` 解析 `imagesV2.xml`,把 45 个模板按中文名导出, 并把 `similarity=80` → `threshold=0.8`、`graying=False` → `gray=False`、`adaptive=True` → `scales=(0.9,1.0,1.1)` 一起写进 `manifest.json`: ```powershell .venv\Scripts\python.exe libs\desktopauto\scripts\import_yingdao_images.py # 输出:%LOCALAPPDATA%\37duAutomation\desktopauto_templates\<工程名>\images\*.png + manifest.json ``` 2. **指令对照**(影刀 → 本库): | 影刀 | 本库 | |---|---| | `image.click(timeout, delay_after, anchor_type, offset_x/y, clicks, button)` | `click(tpl, timeout=, delay_after=, anchor=, offset=, clicks=, button=)` | | `image.wait(wait_mode="appear"/"disappear", is_wait_all_images)` | `wait()` / `wait_gone()` / `wait_all()` | | `image.exist(exist_mode, is_find_all_images)` | `exists()` / `find_all()` | | `win32.send_keys(keys="{ENTER}")` | `send_keys("{ENTER}")` | | `win32.move_mouse(point_x, point_y, relative_to, move_speed)` | `move_mouse(x, y, relative_to="screen"/"window", speed=)` | | `programing.sleep(seconds, random_number, start/stop_number)` | `sleep(s)` / `sleep(random_range=(lo, hi))` | | `file.if_exist(path, expect_exist)` | `fs.exists(path)` | | `dir.makedir(parent, name)` | `fs.make_dir(parent, name)` | | `system.run_or_open` / `terminal_process` | `run_or_open()` / `kill_process()` | | `process.run` / `invoke_module`(流程/调 Python 模块) | 直接用 Python 函数调用,不需要迁 | 3. **view_coordinates.py**(影刀里查按钮坐标的辅助流程):用 `read_text()` + `click_text()` 或 `mark()` 标注替代。 ## 自测 ```powershell .\.venv\Scripts\python.exe libs\desktopauto\_selftest.py ``` 用合成图验证:精确匹配、坐标换算、区域偏移、多目标、低纹理防误命中、多尺度、透明 PNG、调试图输出(不需要真实桌面)。