用ai写了一个能固定时间读取桌面文本文档内容,并发送给文件传输的脚本。遇到一些问题请老师们帮忙看一下。
我在vscode的资源管理器里运行时会弹未能自动识别输入框,请确认截图文件input_box.png存在的错误。但是在运行和调试里面却可以顺利运行。我已经安装了pip install pyautogui,pip install pyperclip ,pip install schedule这几个运行库。下面是ai生成的代码,求各位大佬帮忙诊断下问题所在。
import pyautogui
import pyperclip
import schedule
import time
import os
import threading
import tkinter as tk
from tkinter import messagebox, filedialog
import ctypes
from ctypes import wintypes
=============== 配置区 ===============
WECHAT_HOTKEY = [‘alt’, ‘ctrl’, ‘w’] # 唤醒微信的快捷键
INPUT_IMG = “input_box.png” # 输入框截图
DESKTOP = os.path.join(os.path.expanduser(“~”), “Desktop”)
FILE_PATH = os.path.join(DESKTOP, “工作汇报.txt”)
=====================================
Windows API 常量
WM_LBUTTONDOWN = 0x0201
WM_LBUTTONUP = 0x0202
def get_window_handle():
“””获取当前活动窗口的句柄”””
return ctypes.windll.user32.GetForegroundWindow()
def send_mouse_click(hwnd, x, y):
“””向指定窗口发送后台鼠标点击事件”””
将坐标转换为lParam格式 (y << 16 | x)
lparam = wintypes.LPARAM(y << 16 | x)
发送鼠标按下事件
ctypes.windll.user32.PostMessageW(hwnd, WM_LBUTTONDOWN, 1, lparam)
time.sleep(0.05)
发送鼠标释放事件
ctypes.windll.user32.PostMessageW(hwnd, WM_LBUTTONUP, 0, lparam)
def locate_input_box():
“””自动识别微信输入框位置”””
try:
location = pyautogui.locateOnScreen(INPUT_IMG, confidence=0.7)
if location:
x = location.left + location.width // 2
y = location.top + location.height // 2
return (x, y)
else:
return None
except Exception:
return None
def send_report():
“””读取文件并发送到微信文件传输助手”””
try:
if not os.path.exists(FILE_PATH):
messagebox.showerror(“错误”, f”未找到文件:{FILE_PATH}”)
return
with open(FILE_PATH, “r”, encoding=”utf-8”) as f:
content = f.read().strip()
if not content:
messagebox.showwarning(“提示”, “工作汇报文件是空的,未发送。”)
return
唤醒微信
pyautogui.hotkey(*WECHAT_HOTKEY)
time.sleep(1.5)
搜索文件传输助手
pyautogui.hotkey(‘ctrl’, ‘f’)
time.sleep(0.8)
pyautogui.typewrite(‘wenjian’)
time.sleep(1)
pyautogui.press(‘enter’)
time.sleep(1.5)
pyautogui.press(‘enter’)
time.sleep(1.5)
自动识别输入框并使用后台点击
pos = locate_input_box()
if pos:
获取微信窗口句柄
hwnd = get_window_handle()
if hwnd:
转换为相对于窗口的坐标
rect = wintypes.RECT()
ctypes.windll.user32.GetWindowRect(hwnd, ctypes.byref(rect))
relative_x = pos[0] - rect.left
relative_y = pos[1] - rect.top
后台点击输入框
send_mouse_click(hwnd, relative_x, relative_y)
time.sleep(0.5)
else:
messagebox.showwarning(“提示”, “无法获取微信窗口句柄。”)
return
else:
messagebox.showwarning(“提示”, “未能自动识别输入框,请确认截图文件 input_box.png 存在。”)
return
粘贴 & 发送
pyperclip.copy(content)
pyautogui.hotkey(‘ctrl’, ‘v’)
time.sleep(0.8)
pyautogui.press(‘enter’)
messagebox.showinfo(“成功”, “✅ 工作汇报已成功发送!”)
except Exception as e:
messagebox.showerror(“错误”, f”发送失败:{e}”)
def start_schedule(run_time):
“””启动定时任务”””
schedule.clear()
schedule.every().day.at(run_time).do(send_report)
messagebox.showinfo(“定时设置成功”, f”将在每天 {run_time} 自动发送。”)
def loop_schedule():
while True:
schedule.run_pending()
time.sleep(30)
threading.Thread(target=loop_schedule, daemon=True).start()
=============== 图形界面部分 ===============
def run_gui():
root = tk.Tk()
root.title(“微信自动发送工作汇报(后台点击版)”)
root.geometry(“420x260”)
tk.Label(root, text=”请选择功能模式:”, font=(“微软雅黑”, 12)).pack(pady=10)
tk.Label(root, text=”每天发送时间(格式 17:30):”, font=(“微软雅黑”, 10)).pack()
time_entry = tk.Entry(root, font=(“微软雅黑”, 10))
time_entry.insert(0, “17:30”)
time_entry.pack(pady=5)
def test_now():
answer = messagebox.askyesno(“确认”, “确定要立即测试发送吗?”)
if answer:
send_report()
tk.Button(root, text=”🧪 立即测试”, command=test_now,
font=(“微软雅黑”, 11), bg=”#4CAF50”, fg=”white”).pack(pady=8)
def set_time():
t = time_entry.get().strip()
if len(t) != 5 or “:” not in t:
messagebox.showerror(“格式错误”, “请输入正确的时间格式,例如 17:30”)
return
start_schedule(t)
tk.Button(root, text=”⏰ 设置每日定时发送”, command=set_time,
font=(“微软雅黑”, 11), bg=”#2196F3”, fg=”white”).pack(pady=5)
tk.Button(root, text=”❌ 退出程序”, command=root.quit,
font=(“微软雅黑”, 10), bg=”#f44336”, fg=”white”).pack(pady=10)
root.mainloop()
if name == “main“:
run_gui()
关于 LearnKu
推荐文章: