{"id":977280,"date":"2024-12-27T06:28:16","date_gmt":"2024-12-26T22:28:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/977280.html"},"modified":"2024-12-27T06:28:18","modified_gmt":"2024-12-26T22:28:18","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80word%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/977280.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00word\u6587\u6863"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202616\/3ab2369e-19fc-4525-a2df-bf0416145217.webp\" alt=\"python\u5982\u4f55\u6253\u5f00word\u6587\u6863\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6253\u5f00Word\u6587\u6863\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>python-docx<\/code>\u5e93\u3001<code>pywin32<\/code>\u5e93\u3001<code>comtypes<\/code>\u5e93\uff0c\u5176\u4e2d<code>python-docx<\/code>\u662f\u6700\u5e38\u7528\u7684\u9009\u9879\uff0c\u56e0\u4e3a\u5b83\u6613\u4e8e\u4f7f\u7528\u4e14\u8de8\u5e73\u53f0\u3002<code>pywin32<\/code>\u548c<code>comtypes<\/code>\u5219\u4f9d\u8d56\u4e8eWindows\u64cd\u4f5c\u7cfb\u7edf\u548c\u5df2\u5b89\u88c5\u7684Microsoft Word\u3002<\/strong>\u5176\u4e2d\uff0c<code>python-docx<\/code>\u5e93\u56e0\u5176\u8f7b\u4fbf\u548c\u8de8\u5e73\u53f0\u7684\u7279\u6027\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u8bfb\u53d6\u548c\u5904\u7406Word\u6587\u6863\u7684\u57fa\u672c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>python-docx<\/code>\u5e93\u6253\u5f00\u5e76\u8bfb\u53d6Word\u6587\u6863\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001PYTHON-DOCX\u5e93<\/h3>\n<\/p>\n<p><p><code>python-docx<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u548c\u66f4\u65b0Microsoft Word\uff08.docx\uff09\u6587\u4ef6\u7684Python\u5e93\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u6253\u5f00Word\u6587\u6863\uff0c\u5e76\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5python-docx<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5<code>python-docx<\/code>\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install python-docx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6253\u5f00Word\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6253\u5f00Word\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx import Document<\/p>\n<h2><strong>\u6253\u5f00Word\u6587\u6863<\/strong><\/h2>\n<p>doc = Document(&#39;example.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8bfb\u53d6\u6587\u6863\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>python-docx<\/code>\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u6587\u6863\u4e2d\u7684\u6bb5\u843d\u548c\u8868\u683c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6587\u6863\u4e2d\u7684\u6240\u6709\u6bb5\u843d<\/p>\n<p>for paragraph in doc.paragraphs:<\/p>\n<p>    print(paragraph.text)<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u6863\u4e2d\u7684\u6240\u6709\u8868\u683c<\/strong><\/h2>\n<p>for table in doc.tables:<\/p>\n<p>    for row in table.rows:<\/p>\n<p>        for cell in row.cells:<\/p>\n<p>            print(cell.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001PYWIN32\u5e93<\/h3>\n<\/p>\n<p><p><code>pywin32<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u7ec4Windows API\u7684Python\u63a5\u53e3\uff0c\u53ef\u4ee5\u7528\u4e8e\u4e0eMicrosoft Word\u8fdb\u884c\u4ea4\u4e92\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u4e2a\u5e93\u53ea\u80fd\u5728Windows\u7cfb\u7edf\u4e0a\u4f7f\u7528\uff0c\u5e76\u4e14\u9700\u8981\u5b89\u88c5Microsoft Word\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5pywin32<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bpip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6253\u5f00Word\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528<code>pywin32<\/code>\u6253\u5f00Word\u6587\u6863\u7684\u793a\u4f8b\u4ee3\u7801\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>\u6253\u5f00Word\u6587\u6863<\/strong><\/h2>\n<p>doc = word.Documents.Open(&#39;C:\\\\path\\\\to\\\\your\\\\document.docx&#39;)<\/p>\n<h2><strong>\u6253\u5370\u6587\u6863\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>print(doc.Content.Text)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u6863\u548cWord\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>doc.Close()<\/p>\n<p>word.Quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001COMTYPES\u5e93<\/h3>\n<\/p>\n<p><p><code>comtypes<\/code>\u5e93\u662f\u4e00\u4e2a\u7eafPython\u7684COM\u63a5\u53e3\u5e93\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u4e0eWord\u6587\u6863\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5comtypes<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install comtypes<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528comtypes\u6253\u5f00Word\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import comtypes.client<\/p>\n<h2><strong>\u521b\u5efaWord\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61<\/strong><\/h2>\n<p>word = comtypes.client.CreateObject(&#39;Word.Application&#39;)<\/p>\n<h2><strong>\u6253\u5f00Word\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\u548cWord\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>doc.Close(False)<\/p>\n<p>word.Quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6253\u5f00\u548c\u8bfb\u53d6Word\u6587\u6863\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u73af\u5883\u3002<strong><code>python-docx<\/code>\u5e93\u9002\u7528\u4e8e\u9700\u8981\u8de8\u5e73\u53f0\u7684\u89e3\u51b3\u65b9\u6848\u548c\u57fa\u672c\u7684\u6587\u6863\u8bfb\u53d6\uff1b\u800c<code>pywin32<\/code>\u548c<code>comtypes<\/code>\u5e93\u9002\u7528\u4e8e\u9700\u8981\u5728Windows\u5e73\u53f0\u4e0a\u8fdb\u884c\u66f4\u590d\u6742\u7684Word\u64cd\u4f5c\u3002<\/strong>\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u8bfb\u53d6\u3001\u4fee\u6539\u548c\u521b\u5efaWord\u6587\u6863\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6253\u5f00Word\u6587\u6863\u5e76\u8bfb\u53d6\u5185\u5bb9\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>python-docx<\/code>\u5e93\u6765\u6253\u5f00\u548c\u8bfb\u53d6Word\u6587\u6863\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>pip install python-docx<\/code>\u6765\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u6253\u5f00Word\u6587\u6863\u5e76\u8bfb\u53d6\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-python\">from docx import Document\n\ndoc = Document(&#39;your_document.docx&#39;)\nfor paragraph in doc.paragraphs:\n    print(paragraph.text)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5c06\u6587\u6863\u4e2d\u7684\u6240\u6709\u6bb5\u843d\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Python\u7f16\u8f91Word\u6587\u6863\u4e2d\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528<code>python-docx<\/code>\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u8bfb\u53d6Word\u6587\u6863\uff0c\u8fd8\u53ef\u4ee5\u7f16\u8f91\u5176\u5185\u5bb9\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u4fee\u6539\u6bb5\u843d\u6587\u672c\uff1a<\/p>\n<pre><code class=\"language-python\">doc = Document(&#39;your_document.docx&#39;)\ndoc.paragraphs[0].text = &quot;\u65b0\u7684\u6bb5\u843d\u5185\u5bb9&quot;\ndoc.save(&#39;your_document.docx&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u4f1a\u5c06\u7b2c\u4e00\u4e2a\u6bb5\u843d\u7684\u6587\u672c\u66ff\u6362\u4e3a\u65b0\u7684\u5185\u5bb9\u5e76\u4fdd\u5b58\u6587\u6863\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6253\u5f00Word\u6587\u6863\u65f6\uff0c\u5982\u4f55\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u6587\u4ef6\uff1f<\/strong><br \/><code>python-docx<\/code>\u5e93\u4e13\u95e8\u7528\u4e8e\u5904\u7406<code>.docx<\/code>\u683c\u5f0f\u7684Word\u6587\u6863\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u8f83\u65e7\u7684<code>.doc<\/code>\u683c\u5f0f\u6587\u4ef6\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u5176\u8f6c\u6362\u4e3a<code>.docx<\/code>\u683c\u5f0f\uff0c\u6216\u4f7f\u7528\u5176\u4ed6\u5e93\u5982<code>pywin32<\/code>\u6765\u4e0eMicrosoft Word\u8fdb\u884c\u4ea4\u4e92\u3002\u4f46\u8bf7\u6ce8\u610f\uff0c<code>pywin32<\/code>\u9700\u8981\u5728Windows\u73af\u5883\u4e0b\u8fd0\u884c\uff0c\u5e76\u4e14\u9700\u8981\u5b89\u88c5Microsoft Office\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6253\u5f00Word\u6587\u6863\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528python-docx\u5e93\u3001pywin32\u5e93\u3001comtypes\u5e93\uff0c\u5176 [&hellip;]","protected":false},"author":3,"featured_media":977285,"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\/977280"}],"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=977280"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977280\/revisions"}],"predecessor-version":[{"id":977289,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977280\/revisions\/977289"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/977285"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=977280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=977280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=977280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}