diff --git a/README.md b/README.md index f248903..c1b341f 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ ### 安装依赖 ```bash -pip install flask +pip install flask pyautogui pyperclip ``` ### 启动服务 @@ -55,6 +55,133 @@ python app.py | DELETE | /api/contacts/:id | 删除联系人 | | GET | /api/stats | 获取统计数据 | +## PC 微信自动化操作 + +### 环境要求 + +- Python 3.x +- pyautogui: 屏幕截图和鼠标键盘操作 +- pyperclip: 剪贴板操作(用于中文输入) +- ollama + glm-ocr 模型: OCR 文字识别 + +### 安装依赖 + +```bash +pip install pyautogui pyperclip +``` + +安装 ollama 和 OCR 模型: +```bash +# 安装 ollama (https://ollama.ai) +ollama pull glm-ocr +``` + +### 微信定位和搜索流程 + +#### 1. 检查微信是否运行 + +```python +import pyautogui + +# 检查微信进程 +import subprocess +result = subprocess.run(['tasklist'], capture_output=True, text=True) +if 'WeChat' in result.stdout: + print('微信已在运行') +``` + +#### 2. 关键坐标位置 + +| 功能 | 坐标位置 | 说明 | +|------|----------|------| +| 搜索框 | (118, 40) | 左上角区域,点击后可输入搜索内容 | +| 搜索区域检测 | (96,31)-(132,48) | 用于 OCR 检测"搜索"字样 | +| 搜索结果区域 | (125,96)-(463,149) | 搜索后显示联系人列表的区域 | +| 聊天输入框 | (600, 895) | 底部消息输入区域 | + +#### 3. 搜索联系人示例 + +```python +import pyautogui +import pyperclip +import time + +def search_contact(name): + # 点击搜索框 + pyautogui.click(118, 40) + time.sleep(0.5) + + # 清空并输入中文(使用剪贴板) + pyautogui.hotkey('ctrl', 'a') + time.sleep(0.3) + pyperclip.copy(name) + pyautogui.hotkey('ctrl', 'v') + time.sleep(1) + + # 按回车确认搜索 + pyautogui.press('enter') + time.sleep(1) + +# 使用示例 +search_contact('夏骥') +``` + +#### 4. OCR 检测搜索框 + +```python +import pyautogui +import subprocess + +def ocr_region(left, top, width, height, save_path='region.png'): + # 截图指定区域 + region = (left, top, width, height) + screenshot = pyautogui.screenshot(region=region) + screenshot.save(save_path) + + # 使用 ollama 进行 OCR + result = subprocess.run( + ['ollama', 'run', 'glm-ocr', f'Text Recognition: ./{save_path}'], + capture_output=True, text=True + ) + return result.stdout.strip() + +# 检测搜索框是否有"搜索"字样 +text = ocr_region(96, 31, 36, 17) # (132-96, 48-31) +if '搜索' in text: + print('搜索框已就绪') +``` + +#### 5. 发送消息示例 + +```python +def send_message(message): + # 点击聊天输入框 + pyautogui.click(600, 895) + time.sleep(0.3) + + # 输入消息 + pyperclip.copy(message) + pyautogui.hotkey('ctrl', 'v') + time.sleep(0.5) + + # 发送 + pyautogui.press('enter') + +# 使用示例 +send_message('元宵节快乐!') +``` + +### 自动化脚本参数说明 + +| 参数 | 说明 | +|------|------| +| 搜索框位置 (118, 40) | 微信窗口左上角搜索区域 | +| 搜索区域 OCR (96,31)-(132,48) | 检测搜索框状态 | +| 结果区域 OCR (125,96)-(463,149) | 检测搜索结果 | +| 输入框位置 (600, 895) | 聊天窗口底部输入区域 | + +> **注意**: 以上坐标基于特定分辨率和微信窗口大小,实际使用时可能需要根据环境调整。 + ## 许可证 MIT License