wps_handle.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import time
  2. import pythoncom
  3. import win32com
  4. import win32com.client
  5. from win10toast import ToastNotifier
  6. toaster = ToastNotifier()
  7. def get_active_wps_text():
  8. pythoncom.CoInitialize() # 初始化 COM
  9. try:
  10. wps = win32com.client.Dispatch("Kwps.Application") # WPS Word
  11. doc = wps.ActiveDocument
  12. content = doc.Content.Text
  13. print("[WPS] 当前文档内容:", content[:100]) # 打印前100个字符
  14. return content
  15. except Exception as e:
  16. print("[!] 无法访问 WPS 文档:", e)
  17. return None
  18. def check_for_changes():
  19. global current_text
  20. while True:
  21. time.sleep(2) # 每2秒检查一次文档内容
  22. new_text = get_active_wps_text()
  23. if new_text and new_text != current_text:
  24. current_text = new_text
  25. update_recommendation(new_text)
  26. toaster.show_toast("新推荐内容", recommendation, duration=10)
  27. def update_recommendation(content):
  28. global recommendation
  29. recommendation = f"基于你输入的内容,推荐:{content[:50]}..."