使用Python自动监听聊天软件并回复
下载图片识别文字模型
https://cdn-lfs-cn-1.modelscope.cn/prod/lfs-objects/0f/88/46b1d4bba223a2a2f9d9b44022fbc22cc019051a602b41a7fda9667e4cad?filename=ch_PP-OCRv5_server_det.onnx&namespace=RapidAI&repository=RapidOCR&revision=master&tag=model&auth_key=1765964813-7020b2b7b2c14a7aa1360936fbe21f60-0-03344d0a07798c28c3e9260cecb803e2
插入代码
import pygetwindow as gw import pyautogui import time import pyperclip from cnocr import CnOcr def get_all_window_titles(): # 获取所有窗口标题 windows = gw.getAllWindows() return [window.title for window in windows] def get_window_by_title(title): # 通过标题获取窗口对象 windows = gw.getWindowsWithTitle(title) return windows if windows else None def capture_region(x1, y1, x2, y2, output_path): # 截取指定区域 region = (x1, y1, x2-x1, y2-y1) screenshot = pyautogui.screenshot(region=region) # 保存截图 screenshot.save(output_path) print(f"已保存截图: {output_path}") def send_msg(text): pyperclip.copy(text) # 复制文本到剪贴板 pyautogui.hotkey('ctrl', 'v') # 粘贴 pyautogui.press('enter') # 发送消息 def find_and_click(window_title, button_coords): # 获取目标窗口 windows = gw.getWindowsWithTitle(window_title) if not windows: print(f"未找到窗口: {window_title}") return window = windows[0] # 激活窗口 window.activate() time.sleep(0.5) # 等待窗口激活 # 计算绝对坐标 left, top = window.left, window.top x, y =window.left + button_coords[0],window.top + button_coords[1] time.sleep(0.2) # 等待输入结束 # 截取聊天区域 chat_screenshot = pyautogui.screenshot(region=(left + 320, top + 60, 200, 300)) chat_screenshot.save('./chat_area.png') # OCR提取文字 ocr = CnOcr() out = ocr.ocr('./chat_area.png') last_message = out[-1]['text'] print(f"收到消息: {last_message}") pyautogui.moveTo(x-50, y-50, duration=0.5) # 模拟点击 pyautogui.click(x-50, y-50) # 模拟键盘输入 # 假设微信输入框已聚焦 send_msg(last_message) time.sleep(1) # 等待输入结束 # 模拟点击发送 pyautogui.click(x, y) capture_region(x,y,x+50,y+50, "C://Users//admin//Pictures//test.png") print(f"已点击坐标: ({x}, {y})") return last_message if __name__ == "__main__": # 示例:点击微信中的“发送”按钮 window_title = "微信" # 替换为实际窗口标题 button_coords = (710, 600) # 按钮在窗口中的相对坐标 preMsg = '' message = '1' while True: if (preMsg != message): preMsg = message message = find_and_click(window_title, button_coords) time.sleep(5) # 等待输入结束

浙公网安备 33010602011771号