123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- import os
- import time
- import pythoncom
- import win32com
- import win32com.client
- import win32con
- import win32gui
- from win10toast import ToastNotifier
- from tools.check import is_file_open_in_wps
- from tools.config import file_type_map
- toaster = ToastNotifier()
- def bring_wps_window_to_front(file_path: str) -> bool:
- '''
- 找到文件被打开的窗口置顶,目前实现方式会被windows安全程序拦截只会在控制栏闪烁窗口图标
- :param file_path:
- :return:
- '''
- # abs_path = os.path.abspath(file_path).lower()
- # suffix = file_path.split('.')[-1]
- #
- # try:
- # pythoncom.CoInitialize()
- # app = win32com.client.GetActiveObject(file_type_map[suffix][0])
- # for wb in getattr(app, file_type_map[suffix][1]):
- # if wb.FullName.lower() == abs_path:
- # hwnd = app.Hwnd # WPS 主窗口句柄
- # if hwnd:
- # win32gui.ShowWindow(hwnd, win32con.SW_RESTORE) # 恢复最小化窗口
- # win32gui.SetForegroundWindow(hwnd) # 置顶窗口
- # return True
- # except Exception as e:
- # print("置顶失败:", e)
- # return False
- # finally:
- # pythoncom.CoUninitialize()
- # return False
- 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')
|