|
| 1 | +import pyautogui |
| 2 | +import pytesseract |
| 3 | +from PIL import Image |
| 4 | +from docx import Document |
| 5 | +import re |
| 6 | +import os |
| 7 | +import time |
| 8 | +import schedule |
| 9 | +import logging |
| 10 | +import threading |
| 11 | +import queue |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +# 设置日志记录配置 |
| 15 | +logging.basicConfig(filename='app.log', level=logging.DEBUG, |
| 16 | + format='%(asctime)s - %(levelname)s - %(message)s') |
| 17 | + |
| 18 | +# 获取当前用户的桌面路径 |
| 19 | +def get_desktop_path(): |
| 20 | + return str(Path.home() / "Desktop") |
| 21 | + |
| 22 | +# 初始化或创建一个新的 Word 文档并保存到桌面 |
| 23 | +def init_doc_on_desktop(filename): |
| 24 | + desktop_path = get_desktop_path() |
| 25 | + full_path = os.path.join(desktop_path, filename) |
| 26 | + if not os.path.exists(full_path): |
| 27 | + doc = Document() |
| 28 | + doc.save(full_path) |
| 29 | + print(f"创建了新的文档: {full_path}") |
| 30 | + else: |
| 31 | + doc = Document(full_path) |
| 32 | + print(f"加载了已有的文档: {full_path}") |
| 33 | + return doc, full_path |
| 34 | + |
| 35 | +# 强制创建新的文档(可选)并保存到桌面 |
| 36 | +def ensure_new_doc_on_desktop(filename): |
| 37 | + doc, full_path = init_doc_on_desktop(filename) |
| 38 | + if os.path.exists(full_path): |
| 39 | + os.remove(full_path) # 删除旧文件 |
| 40 | + print(f"删除了旧的文档: {full_path}") |
| 41 | + doc = Document() |
| 42 | + doc.save(full_path) |
| 43 | + print(f"创建了新的文档: {full_path}") |
| 44 | + return doc, full_path |
| 45 | + |
| 46 | +# 保存招聘信息到 Word 文档 |
| 47 | +def save_to_doc(doc, message, filename): |
| 48 | + doc.add_paragraph(message) |
| 49 | + doc.save(filename) |
| 50 | + |
| 51 | +# 筛选词条并总结到新的 Word 文档 |
| 52 | +def filter_and_summarize(source_filename, target_filename, keywords): |
| 53 | + source_doc = Document(source_filename) |
| 54 | + target_doc = Document() |
| 55 | + |
| 56 | + summary = {} |
| 57 | + for para in source_doc.paragraphs: |
| 58 | + for keyword in keywords: |
| 59 | + if re.search(keyword, para.text, re.IGNORECASE): |
| 60 | + if keyword not in summary: |
| 61 | + summary[keyword] = [] |
| 62 | + summary[keyword].append(para.text) |
| 63 | + |
| 64 | + # 写入总结文档 |
| 65 | + for keyword, entries in summary.items(): |
| 66 | + target_doc.add_heading(keyword, level=1) |
| 67 | + for entry in entries: |
| 68 | + target_doc.add_paragraph(entry) |
| 69 | + |
| 70 | + target_doc.save(target_filename) |
| 71 | + print(f"已更新总结文档: {target_filename}") |
| 72 | + |
| 73 | +# 截图并识别文本 |
| 74 | +def capture_and_process_text(region=None): |
| 75 | + screenshot = pyautogui.screenshot(region=region) |
| 76 | + tessdata_dir_config = '--tessdata-dir "C:\\Program Files\\Tesseract-OCR\\tessdata"' |
| 77 | + text = pytesseract.image_to_string(screenshot, lang='chi_sim', config=tessdata_dir_config) # 使用简体中文 |
| 78 | + return text |
| 79 | + |
| 80 | +# 检查并保存包含“招聘”的信息 |
| 81 | +def check_and_save_recruitment_info(doc, raw_data_file): |
| 82 | + try: |
| 83 | + text = capture_and_process_text() |
| 84 | + lines = text.split('\n') |
| 85 | + for line in lines: |
| 86 | + if '招聘' in line: |
| 87 | + print(f"检测到招聘信息: {line}") |
| 88 | + save_to_doc(doc, line, raw_data_file) |
| 89 | + except Exception as ex: |
| 90 | + logging.error(f"捕获或处理文本时发生错误: {ex}", exc_info=True) |
| 91 | + print(f"捕获或处理文本时发生错误: {ex}") |
| 92 | + |
| 93 | +# 定时任务:每二十分钟筛选并总结信息 |
| 94 | +def scheduled_task(): |
| 95 | + print("开始筛选并总结信息...") |
| 96 | + filter_and_summarize(raw_data_full_path, summarized_full_path, keywords) |
| 97 | + print("信息筛选和总结完成。") |
| 98 | + |
| 99 | +# 监听键盘输入的线程函数 |
| 100 | +def listen_for_exit(q): |
| 101 | + print("你可以随时输入 'o' 来关闭脚本。") |
| 102 | + while True: |
| 103 | + user_input = input() |
| 104 | + if user_input.lower() == 'o': |
| 105 | + q.put('exit') |
| 106 | + break |
| 107 | + |
| 108 | +# 主函数 |
| 109 | +if __name__ == "__main__": |
| 110 | + raw_data_file = '原始数据.docx' |
| 111 | + summarized_file = '汇总后.docx' |
| 112 | + keywords = ['工程师', '设计师', '程序员'] # 根据需要添加更多关键词 |
| 113 | + |
| 114 | + # 创建或初始化文档并保存到桌面 |
| 115 | + doc, raw_data_full_path = init_doc_on_desktop(raw_data_file) |
| 116 | + # 如果希望每次运行都创建新文档,请取消下面这行的注释: |
| 117 | + # doc, raw_data_full_path = ensure_new_doc_on_desktop(raw_data_file) |
| 118 | + |
| 119 | + summarized_doc, summarized_full_path = init_doc_on_desktop(summarized_file) |
| 120 | + # 如果希望每次运行都创建新文档,请取消下面这行的注释: |
| 121 | + # summarized_doc, summarized_full_path = ensure_new_doc_on_desktop(summarized_file) |
| 122 | + |
| 123 | + # 设置定时任务 |
| 124 | + schedule.every(20).minutes.do(scheduled_task) |
| 125 | + |
| 126 | + # 创建队列和线程用于监听键盘输入 |
| 127 | + exit_queue = queue.Queue() |
| 128 | + input_thread = threading.Thread(target=listen_for_exit, args=(exit_queue,)) |
| 129 | + input_thread.daemon = True # 设置为守护线程,主线程结束时自动退出 |
| 130 | + input_thread.start() |
| 131 | + |
| 132 | + print("程序启动,开始监控包含‘招聘’字样的信息...") |
| 133 | + |
| 134 | + try: |
| 135 | + while True: |
| 136 | + if not exit_queue.empty(): |
| 137 | + if exit_queue.get() == 'exit': |
| 138 | + print("收到关闭指令,正在退出程序...") |
| 139 | + break |
| 140 | + |
| 141 | + check_and_save_recruitment_info(doc, raw_data_full_path) |
| 142 | + schedule.run_pending() |
| 143 | + time.sleep(60) # 每隔60秒检查一次 |
| 144 | + except KeyboardInterrupt: |
| 145 | + print("程序被用户中断。") |
| 146 | + except Exception as ex: |
| 147 | + logging.error(f"主循环中发生错误: {ex}", exc_info=True) |
| 148 | + print(f"主循环中发生错误: {ex}") |
| 149 | + finally: |
| 150 | + print("程序已停止。") |
0 commit comments