wps_handle.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import time
  2. import pythoncom
  3. import win32com
  4. import win32com.client
  5. from win10toast import ToastNotifier
  6. from tools.check import is_file_open_in_wps
  7. toaster = ToastNotifier()
  8. def bring_wps_window_to_front(file_path: str) -> bool:
  9. '''
  10. 找到文件被打开的窗口置顶,目前实现方式会被windows安全程序拦截只会在控制栏闪烁窗口图标
  11. :param file_path:
  12. :return:
  13. '''
  14. return True
  15. def get_active_wps_text():
  16. pythoncom.CoInitialize() # 初始化 COM
  17. try:
  18. wps = win32com.client.Dispatch("Kwps.Application") # WPS Word
  19. doc = wps.ActiveDocument
  20. content = doc.Content.Text
  21. print("[WPS] 当前文档内容:", content[:100]) # 打印前100个字符
  22. return content
  23. except Exception as e:
  24. print("[!] 无法访问 WPS 文档:", e)
  25. return None
  26. def check_for_changes():
  27. global current_text
  28. while True:
  29. time.sleep(2) # 每2秒检查一次文档内容
  30. new_text = get_active_wps_text()
  31. if new_text and new_text != current_text:
  32. current_text = new_text
  33. update_recommendation(new_text)
  34. toaster.show_toast("新推荐内容", recommendation, duration=10)
  35. def update_recommendation(content):
  36. global recommendation
  37. recommendation = f"基于你输入的内容,推荐:{content[:50]}..."
  38. if __name__ == '__main__':
  39. if is_file_open_in_wps('storage/接口文档.txt'):
  40. bring_wps_window_to_front('storage/接口文档.txt')
  41. bring_wps_window_to_front('storage/接口文档.txt')