import base64 import json import os import subprocess import winreg from typing import Optional, Literal import pythoncom import win32com from win32com.client import Dispatch from tools.logger_handle import logger WPSComponent = Literal['wps', 'et', 'wpp'] # wps=文字, et=表格, wpp=演示 def get_wps_exe_path(component: WPSComponent = 'wps') -> Optional[str]: """ 获取 WPS 各组件的 exe 路径 :param component: 'wps' 文字, 'et' 表格, 'wpp' 演示 :return: 组件的绝对路径,未找到则返回 None """ # 组件对应的可执行文件名 exe_map = { 'wps': 'wps.exe', 'et': 'et.exe', 'wpp': 'wpp.exe' } exe_name = exe_map.get(component) if not exe_name: return None # WPS 在注册表中的可能路径(与之前相同) wps_reg_paths = [ (winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Kingsoft\Office\6.0\common"), (winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WOW6432Node\Kingsoft\Office\6.0\common"), (winreg.HKEY_CURRENT_USER, r"SOFTWARE\Kingsoft\Office\6.0\common") ] for hkey, subkey in wps_reg_paths: try: key = winreg.OpenKey(hkey, subkey, 0, winreg.KEY_READ) install_root, _ = winreg.QueryValueEx(key, "InstallRoot") winreg.CloseKey(key) # 拼接 office6 目录下的组件 exe component_path = os.path.join(install_root, "office6", exe_name) if os.path.isfile(component_path): return component_path except (FileNotFoundError, OSError, WindowsError): continue return None def get_ms_office_exe_path(application: str) -> Optional[str]: """ 获取 Microsoft Office 组件路径 :param application: 'Word', 'Excel', 'PowerPoint' """ app_map = {'Word': 'WINWORD.EXE', 'Excel': 'EXCEL.EXE', 'PowerPoint': 'POWERPNT.EXE'} exe_name = app_map.get(application) if not exe_name: return None # 遍历常见的 Office 版本号 office_versions = ['16.0', '15.0', '14.0', '12.0'] for version in office_versions: reg_path = rf"SOFTWARE\Microsoft\Office\{version}\{application}\InstallRoot" for hkey in [winreg.HKEY_LOCAL_MACHINE, winreg.HKEY_CURRENT_USER]: try: key = winreg.OpenKey(hkey, reg_path, 0, winreg.KEY_READ) install_path, _ = winreg.QueryValueEx(key, "Path") winreg.CloseKey(key) exe_path = os.path.join(install_path, exe_name) if os.path.isfile(exe_path): return exe_path except (FileNotFoundError, OSError, WindowsError): continue return None def find_wps_via_registry(): """通过Windows注册表查找WPS的安装路径""" possible_paths = [ (winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Kingsoft\Office\6.0\common"), (winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WOW6432Node\Kingsoft\Office\6.0\common"), (winreg.HKEY_CURRENT_USER, r"SOFTWARE\Kingsoft\Office\6.0\common") ] for hkey, subkey in possible_paths: try: key = winreg.OpenKey(hkey, subkey, 0, winreg.KEY_READ | winreg.KEY_WOW64_64KEY) install_path, _ = winreg.QueryValueEx(key, "InstallRoot") winreg.CloseKey(key) # 尝试找到主要的可执行文件 for exe in ["wps.exe", "WPSOffice.exe", "ksolaunch.exe"]: full_path = os.path.join(install_path, "office6", exe) if os.path.isfile(full_path): return full_path except FileNotFoundError: continue except Exception: continue return None def encode_payload(payload): payload['data'] = base64.b64encode(json.dumps(payload['data']).encode('utf-8')).decode('utf-8') return json.dumps(payload) def is_wps_available(): """检测当前系统是否安装了可用的 WPS Office (COM 接口)""" progids_to_try = [ 'KWPS.Application', # WPS Office 2019+ 常用 'WPS.Application', # 旧版本可能用 'Kingsoft.Application' ] for progid in progids_to_try: try: pythoncom.CoInitialize() app = win32com.client.Dispatch(progid) app.Quit() del app pythoncom.CoUninitialize() logger.info(f"检测到可用的 WPS: {progid}") return True except Exception: continue finally: try: pythoncom.CoUninitialize() except: pass logger.warning("未检测到可用的 WPS Office,请确认已安装并注册 COM 接口") return False if __name__ == '__main__': print(get_wps_exe_path('wps')) print(get_wps_exe_path('et')) print(get_wps_exe_path('wpp')) print(subprocess.Popen([get_wps_exe_path('wps'), r"C:\Users\Lenovo\Desktop\dify本地部署.txt"])) # payload = {'data': {"cmd": "enable", "name": "wpsPlugin", "url": "http://172.10.3.110:3000/", "addonType": "wps", # "online": "true"}, # 'serverId': '524ef80e-7ccd-43af-9fcb-69f3b0269730'} # # payload['data'] = base64.b64encode(json.dumps(payload['data']).encode('utf-8')).decode('utf-8') # # print(payload)