12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import time
- import pythoncom
- import win32com
- import win32com.client
- from win10toast import ToastNotifier
- from tools.check import is_file_open_in_wps
- toaster = ToastNotifier()
- def bring_wps_window_to_front(file_path: str) -> bool:
- '''
- 找到文件被打开的窗口置顶,目前实现方式会被windows安全程序拦截只会在控制栏闪烁窗口图标
- :param file_path:
- :return:
- '''
- return True
- def get_active_wps_text():
- pythoncom.CoInitialize() # 初始化 COM
- try:
- wps = win32com.client.Dispatch("Kwps.Application") # WPS Word
- doc = wps.ActiveDocument
- content = doc.Content.Text
- print("[WPS] 当前文档内容:", content[:100]) # 打印前100个字符
- return content
- except Exception as e:
- print("[!] 无法访问 WPS 文档:", e)
- return None
- def check_for_changes():
- global current_text
- while True:
- time.sleep(2) # 每2秒检查一次文档内容
- new_text = get_active_wps_text()
- if new_text and new_text != current_text:
- current_text = new_text
- update_recommendation(new_text)
- toaster.show_toast("新推荐内容", recommendation, duration=10)
- def update_recommendation(content):
- global recommendation
- recommendation = f"基于你输入的内容,推荐:{content[:50]}..."
- if __name__ == '__main__':
- if is_file_open_in_wps('storage/接口文档.txt'):
- bring_wps_window_to_front('storage/接口文档.txt')
- bring_wps_window_to_front('storage/接口文档.txt')
|