commit bd8a119506a2c32b4e95860fb932d3d280f42b71 Author: woni <320753691@qq.com> Date: Tue Jun 18 20:42:45 2024 +0800 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..04bdca3 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# WPSExt + +## 简介 + +通过 wps 加载项操作 wps 表格, 实现自动化 + +## 用法 + + +## 注意 diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..79a71ce --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,14 @@ +[tool.poetry] +name = "wpsext" +version = "0.1.0" +description = "" +authors = ["wn0x00 "] +readme = "README.md" + +[tool.poetry.dependencies] +python = "3.7" + + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/test/test_invoke.py b/test/test_invoke.py new file mode 100644 index 0000000..76e1e83 --- /dev/null +++ b/test/test_invoke.py @@ -0,0 +1,8 @@ +def test_open_wps(): + pass + +def test_close_wps(): + pass + +def test_invoke_func(): + pass diff --git a/wpsext/__init__.py b/wpsext/__init__.py new file mode 100644 index 0000000..8890290 --- /dev/null +++ b/wpsext/__init__.py @@ -0,0 +1,45 @@ +import winreg +import os +import subprocess + + +def read_registry_value(key, subkey, value_name): + """通过注册表查询指定键值的值""" + try: + registry_key = winreg.OpenKey(key, subkey, 0, winreg.KEY_READ) + value, regtype = winreg.QueryValueEx(registry_key, value_name) + winreg.CloseKey(registry_key) + return value + except WindowsError as e: + return None + + +def get_wps_exe_path(): + """通过注册表定位 wps 的安装路径""" + key = winreg.HKEY_CURRENT_USER + subkey = r"Software\kingsoft\Office\6.0\Common" + value_name = "InstallRoot" + install_root = read_registry_value(key, subkey, value_name) + + if not install_root: + raise FileNotFoundError("未找到 WPS 安装路径,请检查是否安装 WPS。") + + wps_exe_path = os.path.join(install_root, "office6", "wps.exe") + + assert os.path.exists(wps_exe_path), f"未找到 WPS 安装路径,请检查是否安装 WPS。" + return wps_exe_path + + +def run_wps(): + """启动 WPS""" + wps_exe_path = get_wps_exe_path() + params = ["/prometheus", "/t"] + process = subprocess.Popen( + [wps_exe_path] + params, + creationflags=subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS, + ) + print("Process ID:", process.pid) + + +if __name__ == "__main__": + run_wps()