{"id":989015,"date":"2024-12-27T08:07:34","date_gmt":"2024-12-27T00:07:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/989015.html"},"modified":"2024-12-27T08:07:37","modified_gmt":"2024-12-27T00:07:37","slug":"doc%e4%b8%8b%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/989015.html","title":{"rendered":"doc\u4e0b\u5982\u4f55\u8c03\u7528python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064419\/5a9ca13a-e061-433d-aaa3-6bd5e258a591.webp\" alt=\"doc\u4e0b\u5982\u4f55\u8c03\u7528python\" \/><\/p>\n<p><p> \u5728Microsoft Word\u6587\u6863\u4e2d\u8c03\u7528Python\u811a\u672c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528VBA\u5b8f\u3001\u901a\u8fc7Python\u7684COM\u63a5\u53e3\u6216\u8005\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u81ea\u52a8\u5316\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u5173\u4e8e\u5982\u4f55\u5728Word\u6587\u6863\u4e2d\u8c03\u7528Python\u7684\u4e00\u4e9b\u65b9\u6cd5\u548c\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p><strong>\u76f4\u63a5\u4f7f\u7528Python COM\u63a5\u53e3<\/strong><\/p>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0cPython\u53ef\u4ee5\u901a\u8fc7COM\uff08Component Object Model\uff09\u63a5\u53e3\u4e0eMicrosoft Word\u8fdb\u884c\u4ea4\u4e92\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u81ea\u52a8\u5316\u4efb\u52a1\uff0c\u5982\u751f\u6210\u62a5\u544a\u3001\u4fee\u6539\u6587\u6863\u5185\u5bb9\u7b49\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Python\u548c\u76f8\u5173\u5e93<\/strong><br \/>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5Python\u3002\u5982\u679c\u6ca1\u6709\uff0c\u8bf7\u4ecePython\u5b98\u7f51\u4e0b\u8f7d\u5b89\u88c5\u3002\u6b64\u5916\uff0c\u9700\u8981\u5b89\u88c5<code>pywin32<\/code>\u5e93\uff0c\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u4e0eWindows\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\u7684\u63a5\u53e3\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199Python\u811a\u672c<\/strong><br \/>\u7f16\u5199\u4e00\u4e2aPython\u811a\u672c\uff0c\u901a\u8fc7COM\u63a5\u53e3\u4e0eWord\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u7528\u4e8e\u6253\u5f00Word\u6587\u6863\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import win32com.client<\/p>\n<h2><strong>\u521b\u5efaWord\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61<\/strong><\/h2>\n<p>word = win32com.client.Dispatch(&quot;Word.Application&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6eWord\u4e3a\u53ef\u89c1\u72b6\u6001<\/strong><\/h2>\n<p>word.Visible = True<\/p>\n<h2><strong>\u6253\u5f00\u6307\u5b9a\u8def\u5f84\u7684Word\u6587\u6863<\/strong><\/h2>\n<p>doc = word.Documents.Open(&#39;C:\\\\path\\\\to\\\\your\\\\document.docx&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u6863\u5185\u5bb9<\/strong><\/h2>\n<p>content = doc.Content.Text<\/p>\n<p>print(content)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u6863<\/strong><\/h2>\n<p>doc.Close()<\/p>\n<h2><strong>\u9000\u51faWord\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>word.Quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6267\u884cPython\u811a\u672c<\/strong><br \/>\u901a\u8fc7\u547d\u4ee4\u884c\u6216\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u8fd0\u884c\u4e0a\u8ff0Python\u811a\u672c\uff0c\u67e5\u770b\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u4f7f\u7528VBA\u5b8f\u8c03\u7528Python\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u4eceWord\u5185\u90e8\u76f4\u63a5\u8c03\u7528Python\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528VBA\uff08Visual Basic for Applications\uff09\u5b8f\u3002\u4ee5\u4e0b\u662f\u8bbe\u7f6e\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u5199VBA\u5b8f<\/strong><br \/>\u5728Word\u4e2d\uff0c\u6253\u5f00\u201c\u5f00\u53d1\u5de5\u5177\u201d\u9009\u9879\u5361\uff0c\u70b9\u51fb\u201c\u5b8f\u201d\u6309\u94ae\uff0c\u7136\u540e\u9009\u62e9\u201cVisual Basic\u201d\u4ee5\u6253\u5f00VBA\u7f16\u8f91\u5668\u3002\u7f16\u5199\u5982\u4e0b\u5b8f\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vba\">Sub RunPythonScript()<\/p>\n<p>    Dim objShell As Object<\/p>\n<p>    Set objShell = VBA.CreateObject(&quot;WScript.Shell&quot;)<\/p>\n<p>    &#39; \u8fd0\u884cPython\u811a\u672c\u7684\u547d\u4ee4<\/p>\n<p>    objShell.Run &quot;python C:\\path\\to\\your\\script.py&quot;<\/p>\n<p>    Set objShell = Nothing<\/p>\n<p>End Sub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884cVBA\u5b8f<\/strong><br \/>\u8fd4\u56deWord\u6587\u6863\uff0c\u70b9\u51fb\u201c\u5b8f\u201d\u6309\u94ae\uff0c\u9009\u62e9\u521a\u521a\u521b\u5efa\u7684\u5b8f<code>RunPythonScript<\/code>\uff0c\u70b9\u51fb\u201c\u8fd0\u884c\u201d\u4ee5\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u81ea\u52a8\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u65b9\u6cd5\u662f\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>python-docx<\/code>\uff0c\u76f4\u63a5\u5728Python\u4e2d\u64cd\u4f5cWord\u6587\u6863\uff0c\u800c\u4e0d\u662f\u5728Word\u4e2d\u8c03\u7528Python\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728Python\u4e2d\u5904\u7406Word\u6587\u6863\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5python-docx\u5e93<\/strong><br \/>\u901a\u8fc7pip\u5b89\u88c5python-docx\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install python-docx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528python-docx\u5904\u7406Word\u6587\u6863<\/strong><br \/>\u7f16\u5199Python\u811a\u672c\uff0c\u4f7f\u7528<code>python-docx<\/code>\u5e93\u521b\u5efa\u6216\u4fee\u6539Word\u6587\u6863\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\u5e76\u6dfb\u52a0\u4e00\u4e9b\u6587\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx import Document<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\u5bf9\u8c61<\/strong><\/h2>\n<p>doc = Document()<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898<\/strong><\/h2>\n<p>doc.add_heading(&#39;Document Title&#39;, 0)<\/p>\n<h2><strong>\u6dfb\u52a0\u6bb5\u843d<\/strong><\/h2>\n<p>doc.add_paragraph(&#39;This is a paragraph in the document.&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u6863<\/strong><\/h2>\n<p>doc.save(&#39;C:\\\\path\\\\to\\\\new_document.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Word\u6587\u6863\u4e2d\u8c03\u7528Python\u811a\u672c\u6216\u8005\u901a\u8fc7Python\u76f4\u63a5\u64cd\u4f5cWord\u6587\u6863\u3002\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u53d6\u51b3\u4e8e\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u3002\u4f8b\u5982\uff0c\u901a\u8fc7COM\u63a5\u53e3\u548cVBA\u5b8f\u9002\u5408\u9700\u8981\u76f4\u63a5\u4eceWord\u8c03\u7528Python\u7684\u60c5\u51b5\uff0c\u800c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5219\u66f4\u9002\u5408\u5728Python\u4e2d\u6279\u91cf\u5904\u7406Word\u6587\u6863\u7684\u60c5\u51b5\u3002\u5728\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u8def\u5f84\u7684\u6b63\u786e\u6027\u548c\u6743\u9650\u8bbe\u7f6e\uff0c\u786e\u4fddPython\u811a\u672c\u80fd\u591f\u6b63\u5e38\u6267\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728doc\u6587\u4ef6\u4e2d\u8fd0\u884cPython\u4ee3\u7801\uff1f<\/strong><br \/>\u5728doc\u6587\u4ef6\u4e2d\u76f4\u63a5\u8fd0\u884cPython\u4ee3\u7801\u5e76\u4e0d\u652f\u6301\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u63d2\u5165\u4ee3\u7801\u7247\u6bb5\u7684\u65b9\u5f0f\u6765\u5c55\u793a\u4ee3\u7801\u3002\u4f7f\u7528Python\u811a\u672c\u751f\u6210\u7684\u7ed3\u679c\uff0c\u53ef\u4ee5\u5c06\u5176\u590d\u5236\u5230doc\u6587\u4ef6\u4e2d\u3002\u6b64\u5916\uff0c\u4f7f\u7528Jupyter Notebook\u6216\u5176\u4ed6\u7f16\u8f91\u5de5\u5177\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fd0\u884c\u4ee3\u7801\u5e76\u5c06\u7ed3\u679c\u5bfc\u51fa\u4e3a\u6587\u6863\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5728Word\u6587\u6863\u4e2d\u5982\u4f55\u5d4c\u5165Python\u811a\u672c\u7684\u8f93\u51fa\u7ed3\u679c\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5c06Python\u811a\u672c\u7684\u8f93\u51fa\u7ed3\u679c\u590d\u5236\u5e76\u7c98\u8d34\u5230Word\u6587\u6863\u4e2d\uff0c\u6216\u8005\u5229\u7528\u751f\u6210\u7684\u56fe\u8868\u3001\u8868\u683c\u7b49\u53ef\u89c6\u5316\u7ed3\u679c\uff0c\u5c06\u5b83\u4eec\u63d2\u5165\u5230Word\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u80fd\u6709\u6548\u5730\u5c55\u793a\u5206\u6790\u7ed3\u679c\uff0c\u540c\u65f6\u4fdd\u6301\u6587\u6863\u7684\u6574\u6d01\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u5de5\u5177\u53ef\u4ee5\u5728doc\u6587\u4ef6\u4e2d\u8f7b\u677e\u96c6\u6210Python\u4ee3\u7801\u548c\u7ed3\u679c\uff1f<\/strong><br \/>\u6709\u4e00\u4e9b\u5de5\u5177\u548c\u63d2\u4ef6\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u5728Word\u6587\u6863\u4e2d\u96c6\u6210Python\u4ee3\u7801\u548c\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Python-docx\u5e93\u53ef\u4ee5\u521b\u5efa\u548c\u4fee\u6539Word\u6587\u6863\uff0c\u6216\u8005\u5229\u7528Jupyter Notebook\u7684\u5bfc\u51fa\u529f\u80fd\uff0c\u5c06Notebook\u8f6c\u6362\u4e3aWord\u6587\u6863\uff0c\u5305\u542b\u4ee3\u7801\u548c\u7ed3\u679c\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Microsoft Word\u6587\u6863\u4e2d\u8c03\u7528Python\u811a\u672c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528VBA\u5b8f\u3001\u901a\u8fc7Python [&hellip;]","protected":false},"author":3,"featured_media":989026,"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\/989015"}],"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=989015"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/989015\/revisions"}],"predecessor-version":[{"id":989029,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/989015\/revisions\/989029"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/989026"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=989015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=989015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=989015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}