{"id":1008796,"date":"2024-12-27T11:05:56","date_gmt":"2024-12-27T03:05:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008796.html"},"modified":"2024-12-27T11:05:58","modified_gmt":"2024-12-27T03:05:58","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%89%93%e5%bc%80wps","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1008796.html","title":{"rendered":"\u5982\u4f55\u7528python\u6253\u5f00wps"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083954\/ef0ca962-dbf6-4096-affa-f1daf3e86c29.webp\" alt=\"\u5982\u4f55\u7528python\u6253\u5f00wps\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u6253\u5f00WPS\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u3001subprocess\u6a21\u5757\u3001pywinauto\u5e93\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528os\u6a21\u5757\u6216subprocess\u6a21\u5757\u76f4\u63a5\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u6765\u542f\u52a8WPS\u529e\u516c\u8f6f\u4ef6\uff0c\u800c\u66f4\u9ad8\u7ea7\u7684\u65b9\u6cd5\u5219\u662f\u4f7f\u7528pywinauto\u5e93\u6765\u8fdb\u884c\u66f4\u52a0\u7ec6\u81f4\u7684\u64cd\u4f5c\u548c\u63a7\u5236\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6253\u5f00WPS\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u529f\u80fd\u3002\u4f7f\u7528<code>os.system()<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u7cfb\u7edf\u547d\u4ee4\u6765\u6253\u5f00WPS\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6307\u5b9aWPS\u7a0b\u5e8f\u7684\u8def\u5f84<\/strong><\/h2>\n<p>wps_path = &quot;C:\\\\Program Files (x86)\\\\Kingsoft\\\\WPS Office\\\\ksolaunch.exe&quot;<\/p>\n<h2><strong>\u4f7f\u7528os.system()\u6253\u5f00WPS<\/strong><\/h2>\n<p>os.system(f&#39;&quot;{wps_path}&quot;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>os\u6a21\u5757\u7684\u4f18\u70b9\u5728\u4e8e\u7b80\u5355\u6613\u7528\uff0c\u4f46\u7f3a\u70b9\u662f\u65e0\u6cd5\u5bf9\u6253\u5f00\u7684\u7a0b\u5e8f\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u63a7\u5236<\/strong>\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u6a21\u62df\u7528\u6237\u8f93\u5165\u6216\u64cd\u4f5cWPS\u4e2d\u7684\u5143\u7d20\uff0c\u5219\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528subprocess\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>subprocess<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u542f\u52a8\u65b0\u8fdb\u7a0b\u3001\u8fde\u63a5\u5230\u5176\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\uff0c\u5e76\u83b7\u5f97\u8fd4\u56de\u7801\u3002\u76f8\u6bd4\u4e8eos\u6a21\u5757\uff0c<code>subprocess<\/code>\u66f4\u9002\u5408\u9700\u8981\u6355\u83b7\u8f93\u51fa\u6216\u8fdb\u884c\u590d\u6742\u8fdb\u7a0b\u7ba1\u7406\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u6307\u5b9aWPS\u7a0b\u5e8f\u7684\u8def\u5f84<\/strong><\/h2>\n<p>wps_path = &quot;C:\\\\Program Files (x86)\\\\Kingsoft\\\\WPS Office\\\\ksolaunch.exe&quot;<\/p>\n<h2><strong>\u4f7f\u7528subprocess.Popen()\u6253\u5f00WPS<\/strong><\/h2>\n<p>process = subprocess.Popen(wps_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>subprocess\u6a21\u5757\u5141\u8bb8\u4f60\u5728\u542f\u52a8WPS\u540e\u8fdb\u884c\u66f4\u591a\u7684\u4ea4\u4e92<\/strong>\uff0c\u6bd4\u5982\u7b49\u5f85\u8fdb\u7a0b\u7ed3\u675f\u3001\u83b7\u53d6\u8fdb\u7a0b\u7684\u8f93\u51fa\u7b49\u3002\u5728\u9700\u8981\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u548c\u63a7\u5236\u529b\u65f6\uff0csubprocess\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pywinauto\u5e93<\/h3>\n<\/p>\n<p><p><code>pywinauto<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316Windows GUI\u7a0b\u5e8f\u7684Python\u5e93\u3002\u4f7f\u7528pywinauto\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u64cd\u4f5c\uff0c\u81ea\u52a8\u5316WPS\u4e2d\u7684\u5404\u79cd\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5pywinauto<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86pywinauto\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywinauto<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528pywinauto\u6253\u5f00WPS<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from pywinauto.application import Application<\/p>\n<h2><strong>\u6307\u5b9aWPS\u7a0b\u5e8f\u7684\u8def\u5f84<\/strong><\/h2>\n<p>wps_path = &quot;C:\\\\Program Files (x86)\\\\Kingsoft\\\\WPS Office\\\\ksolaunch.exe&quot;<\/p>\n<h2><strong>\u542f\u52a8WPS<\/strong><\/h2>\n<p>app = Application().start(wps_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528pywinauto\u7684\u4e3b\u8981\u4f18\u52bf\u5728\u4e8e\u5b83\u53ef\u4ee5\u4e0eWPS\u7684GUI\u8fdb\u884c\u4ea4\u4e92<\/strong>\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u7f16\u5199\u811a\u672c\u6765\u81ea\u52a8\u6267\u884cWPS\u4e2d\u7684\u5404\u79cd\u4efb\u52a1\uff0c\u6bd4\u5982\u6253\u5f00\u6587\u6863\u3001\u7f16\u8f91\u5185\u5bb9\u3001\u4fdd\u5b58\u6587\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u63a7\u5236WPS\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528pywinauto\uff0c\u4f60\u8fd8\u53ef\u4ee5\u83b7\u53d6WPS\u7a97\u53e3\u7684\u53e5\u67c4\uff0c\u5e76\u6267\u884c\u5404\u79cd\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8fde\u63a5\u5230WPS\u7a97\u53e3<\/p>\n<p>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_dlg = app.window(title_re=&quot;.*WPS Office&quot;)<\/p>\n<h2><strong>\u6700\u5927\u5316\u7a97\u53e3<\/strong><\/h2>\n<p>main_dlg.maximize()<\/p>\n<h2><strong>\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\uff0c\u4f8b\u5982\u70b9\u51fb\u83dc\u5355\u7b49<\/strong><\/h2>\n<h2><strong>main_dlg.menu_select(&quot;File-&gt;Open&quot;)<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u6253\u5f00WPS\u65f6\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff1a<strong>os\u6a21\u5757\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u57fa\u672c\u9700\u6c42\uff1bsubprocess\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u548c\u63a7\u5236\u529b\uff1b\u800cpywinauto\u5219\u9002\u5408\u9700\u8981\u8fdb\u884cGUI\u81ea\u52a8\u5316\u7684\u590d\u6742\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u786e\u4fddWPS\u7684\u8def\u5f84\u6b63\u786e\u4e14\u5728\u4f60\u7684\u8ba1\u7b97\u673a\u4e0a\u5df2\u5b89\u88c5\u662f\u6210\u529f\u6253\u5f00WPS\u7684\u524d\u63d0\u3002\u6b64\u5916\uff0c\u4e86\u89e3\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u9879\u76ee\u4e2d\u66f4\u9ad8\u6548\u5730\u4f7f\u7528Python\u8fdb\u884c\u529e\u516c\u81ea\u52a8\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u811a\u672c\u81ea\u52a8\u6253\u5f00WPS Office\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684<code>subprocess<\/code>\u6a21\u5757\u6765\u6253\u5f00WPS Office\u3002\u4f60\u53ea\u9700\u6307\u5b9aWPS\u7684\u5b89\u88c5\u8def\u5f84\u548c\u6240\u9700\u6253\u5f00\u7684\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import subprocess\n\n# WPS\u7684\u5b89\u88c5\u8def\u5f84\nwps_path = r&quot;C:\\Program Files\\Kingsoft\\WPS Office\\ksolaunch.exe&quot;\n# \u8981\u6253\u5f00\u7684\u6587\u4ef6\u8def\u5f84\nfile_path = r&quot;C:\\path\\to\\your\\file.docx&quot;\n\n# \u6253\u5f00WPS\nsubprocess.Popen([wps_path, file_path])\n<\/code><\/pre>\n<p>\u786e\u4fdd\u8def\u5f84\u6b63\u786e\u5e76\u4e14\u6587\u4ef6\u5b58\u5728\u3002<\/p>\n<p><strong>Python\u80fd\u5426\u63a7\u5236WPS\u7684\u6587\u6863\u64cd\u4f5c\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528Python\u53ef\u4ee5\u901a\u8fc7<code>pywinauto<\/code>\u7b49\u5e93\u4e0eWPS\u8fdb\u884c\u4ea4\u4e92\uff0c\u63a7\u5236\u6587\u4ef6\u7684\u6253\u5f00\u3001\u7f16\u8f91\u548c\u4fdd\u5b58\u7b49\u64cd\u4f5c\u3002<code>pywinauto<\/code>\u53ef\u4ee5\u6a21\u62df\u9f20\u6807\u548c\u952e\u76d8\u8f93\u5165\uff0c\u4ece\u800c\u5b9e\u73b0\u81ea\u52a8\u5316\u64cd\u4f5c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from pywinauto import Application\n\n# \u542f\u52a8WPS\napp = Application().start(wps_path)\n\n# \u8fdb\u884c\u6587\u6863\u64cd\u4f5c\napp.WPS_Window.wait(&#39;visible&#39;)\napp.WPS_Window.menu_select(&quot;\u6587\u4ef6 -&gt; \u6253\u5f00&quot;)\n<\/code><\/pre>\n<p>\u66f4\u591a\u590d\u6742\u7684\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u67e5\u9605<code>pywinauto<\/code>\u7684\u6587\u6863\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7Python\u5224\u65adWPS\u662f\u5426\u5df2\u7ecf\u6253\u5f00\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>psutil<\/code>\u5e93\u6765\u68c0\u67e5WPS\u8fdb\u7a0b\u662f\u5426\u5728\u8fd0\u884c\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u6253\u5f00WPS\u3002\u4e0b\u9762\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import psutil\n\ndef is_wps_running():\n    for process in psutil.process_iter():\n        if process.name() == &quot;wps.exe&quot;:\n            return True\n    return False\n\nif not is_wps_running():\n    subprocess.Popen([wps_path])\nelse:\n    print(&quot;WPS\u5df2\u7ecf\u5728\u8fd0\u884c\u4e2d\u3002&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u4e3a\u667a\u80fd\u7684\u6587\u4ef6\u5904\u7406\u6d41\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u6253\u5f00WPS\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u3001subprocess\u6a21\u5757\u3001pywinauto\u5e93\u7b49\u65b9\u6cd5\u3002 \u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1008802,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008796"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1008796"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008796\/revisions"}],"predecessor-version":[{"id":1008806,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008796\/revisions\/1008806"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1008802"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1008796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1008796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1008796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}