{"id":980774,"date":"2024-12-27T06:57:09","date_gmt":"2024-12-26T22:57:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/980774.html"},"modified":"2024-12-27T06:57:12","modified_gmt":"2024-12-26T22:57:12","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8word%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/980774.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528word\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210027\/efd6be5b-4d06-45d9-a7ce-4a074e64dbe5.webp\" alt=\"python\u5982\u4f55\u8c03\u7528word\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8c03\u7528Word\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528<code>python-docx<\/code>\u5e93\u3001<code>pywin32<\/code>\u5e93\u3001\u4ee5\u53ca\u901a\u8fc7<code>comtypes<\/code>\u5e93\u8fdb\u884cCOM\u63a5\u53e3\u8c03\u7528\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528<code>python-docx<\/code>\u5e93\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f9d\u8d56\u4e8eWindows\u5e73\u53f0\uff0c\u8de8\u5e73\u53f0\u6027\u80fd\u8f83\u597d\u3001\u529f\u80fd\u4e30\u5bcc\u3001\u6613\u4e8e\u4f7f\u7528\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>python-docx<\/code>\u5e93\u6765\u64cd\u4f5cWord\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u4e0e\u5165\u95e8<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528<code>python-docx<\/code>\u5e93\uff0c\u9996\u5148\u9700\u8981\u8fdb\u884c\u5b89\u88c5\u3002\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f00\u59cb\u5904\u7406Word\u6587\u6863\u4e86\u3002<code>python-docx<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u8bfb\u53d6\u3001\u4fee\u6539\u548c\u521b\u5efaWord\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h4>1. \u52a0\u8f7d\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u8981\u52a0\u8f7d\u4e00\u4e2aWord\u6587\u6863\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165<code>Document<\/code>\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx import Document<\/p>\n<h2><strong>\u52a0\u8f7d\u73b0\u6709\u6587\u6863<\/strong><\/h2>\n<p>doc = Document(&#39;example.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u52a0\u8f7d\u4e00\u4e2a\u5df2\u5b58\u5728\u7684Word\u6587\u6863\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2. \u521b\u5efa\u65b0\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">doc = Document()<\/p>\n<h2><strong>\u6dfb\u52a0\u6bb5\u843d<\/strong><\/h2>\n<p>doc.add_paragraph(&#39;This is a new document.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bfb\u53d6Word\u6587\u6863\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u8bfb\u53d6Word\u6587\u6863\u7684\u5185\u5bb9\u662f<code>python-docx<\/code>\u7684\u57fa\u672c\u529f\u80fd\u4e4b\u4e00\u3002\u53ef\u4ee5\u8bfb\u53d6\u6bb5\u843d\u3001\u8868\u683c\u7b49\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6\u6bb5\u843d<\/h4>\n<\/p>\n<p><p>\u6bcf\u4e2aWord\u6587\u6863\u7531\u591a\u4e2a\u6bb5\u843d\u7ec4\u6210\uff0c\u4f60\u53ef\u4ee5\u904d\u5386\u6587\u6863\u4e2d\u7684\u6bb5\u843d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for paragraph in doc.paragraphs:<\/p>\n<p>    print(paragraph.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6\u8868\u683c<\/h4>\n<\/p>\n<p><p>Word\u6587\u6863\u4e2d\u4e5f\u53ef\u80fd\u5305\u542b\u8868\u683c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8bfb\u53d6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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>\u4e09\u3001\u4fee\u6539Word\u6587\u6863\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728\u52a0\u8f7d\u6216\u521b\u5efaWord\u6587\u6863\u540e\uff0c\u53ef\u4ee5\u5bf9\u5176\u5185\u5bb9\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h4>1. \u4fee\u6539\u6bb5\u843d<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u4fee\u6539\u6bb5\u843d\u7684\u6587\u672c\u6765\u66f4\u6539\u6587\u6863\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">doc.paragraphs[0].text = &quot;This is the modified first paragraph.&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6dfb\u52a0\u6bb5\u843d<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728\u6587\u6863\u672b\u5c3e\u6dfb\u52a0\u65b0\u7684\u6bb5\u843d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">doc.add_paragraph(&quot;This is a new paragraph.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4fee\u6539\u8868\u683c<\/h4>\n<\/p>\n<p><p>\u4fee\u6539\u8868\u683c\u4e2d\u7684\u5185\u5bb9\u4e0e\u8bfb\u53d6\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u5355\u5143\u683c\u6765\u4fee\u6539\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">table = doc.tables[0]<\/p>\n<p>cell = table.cell(0, 0)<\/p>\n<p>cell.text = &#39;New text&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4fdd\u5b58Word\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u5728\u5bf9Word\u6587\u6863\u8fdb\u884c\u4fee\u6539\u540e\uff0c\u9700\u8981\u4fdd\u5b58\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">doc.save(&#39;modified_document.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8fdb\u9636\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p><code>python-docx<\/code>\u5e93\u8fd8\u652f\u6301\u5176\u4ed6\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u4f8b\u5982\u6dfb\u52a0\u6807\u9898\u3001\u8bbe\u7f6e\u6837\u5f0f\u3001\u63d2\u5165\u56fe\u7247\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u6dfb\u52a0\u6807\u9898<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>add_heading<\/code>\u65b9\u6cd5\u6dfb\u52a0\u6807\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">doc.add_heading(&#39;Document Title&#39;, level=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bbe\u7f6e\u6837\u5f0f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4e3a\u6bb5\u843d\u8bbe\u7f6e\u4e0d\u540c\u7684\u6837\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">paragraph = doc.add_paragraph(&#39;Styled text.&#39;)<\/p>\n<p>paragraph.style = &#39;Title&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u63d2\u5165\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728\u6587\u6863\u4e2d\u63d2\u5165\u56fe\u7247\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">doc.add_picture(&#39;image.jpg&#39;, width=Inches(1.25))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528<code>pywin32<\/code>\u64cd\u4f5cWord<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>python-docx<\/code>\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>pywin32<\/code>\u5e93\u6765\u64cd\u4f5cWord\uff0c\u8fd9\u79cd\u65b9\u5f0f\u9700\u8981\u5728Windows\u7cfb\u7edf\u4e0a\u8fd0\u884c\uff0c\u56e0\u4e3a\u5b83\u4f9d\u8d56\u4e8eCOM\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5<code>pywin32<\/code><\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\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. \u521b\u5efaWord\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61<\/h4>\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<p>word.Visible = 1  # \u8bbe\u7f6eWord\u5e94\u7528\u7a0b\u5e8f\u53ef\u89c1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6253\u5f00Word\u6587\u6863<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">doc = word.Documents.Open(&#39;example.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u64cd\u4f5cWord\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7COM\u63a5\u53e3\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u8bfb\u53d6\u548c\u4fee\u6539\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6587\u6863\u4e2d\u7684\u5185\u5bb9<\/p>\n<p>content = doc.Content.Text<\/p>\n<p>print(content)<\/p>\n<h2><strong>\u4fee\u6539\u6587\u6863\u5185\u5bb9<\/strong><\/h2>\n<p>doc.Content.Text = &quot;This is the modified content.&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u6863<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">doc.SaveAs(&#39;modified_document.docx&#39;)<\/p>\n<p>doc.Close()<\/p>\n<p>word.Quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8c03\u7528\u548c\u64cd\u4f5cWord\u6587\u4ef6\uff0c<code>python-docx<\/code>\u5e93\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u7528\u4e14\u8de8\u5e73\u53f0\u3002\u800c<code>pywin32<\/code>\u5e93\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u4f46\u4ec5\u9650\u4e8eWindows\u5e73\u53f0\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u4f7f\u7528\u73af\u5883\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5904\u7406Word\u6587\u6863\u3002\u65e0\u8bba\u662f\u8bfb\u53d6\u3001\u4fee\u6539\u8fd8\u662f\u521b\u5efaWord\u6587\u6863\uff0cPython\u90fd\u80fd\u4e3a\u4f60\u63d0\u4f9b\u7075\u6d3b\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6Word\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u901a\u8fc7<code>python-docx<\/code>\u5e93\u8f7b\u677e\u8bfb\u53d6Word\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install python-docx<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u4f7f\u7528\u65f6\uff0c\u53ea\u9700\u5bfc\u5165\u5e93\u5e76\u6253\u5f00\u6307\u5b9a\u7684Word\u6587\u4ef6\uff0c\u4e4b\u540e\u53ef\u4ee5\u8bbf\u95ee\u6587\u6863\u7684\u6bb5\u843d\u3001\u8868\u683c\u7b49\u5185\u5bb9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from docx import Document\n\ndoc = Document(&#39;example.docx&#39;)\nfor para in doc.paragraphs:\n    print(para.text)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u521b\u5efa\u65b0\u7684Word\u6587\u4ef6\uff1f<\/strong><br \/>\u4f7f\u7528<code>python-docx<\/code>\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u521b\u5efa\u65b0\u7684Word\u6587\u6863\u3002\u521b\u5efa\u65b0\u6587\u6863\u540e\uff0c\u53ef\u4ee5\u6dfb\u52a0\u6807\u9898\u3001\u6bb5\u843d\u548c\u683c\u5f0f\u8bbe\u7f6e\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from docx import Document\n\ndoc = Document()\ndoc.add_heading(&#39;\u6587\u6863\u6807\u9898&#39;, level=1)\ndoc.add_paragraph(&#39;\u8fd9\u662f\u4e00\u4e2a\u65b0\u7684\u6bb5\u843d\u3002&#39;)\ndoc.save(&#39;new_document.docx&#39;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406Word\u6587\u4ef6\u7684\u56fe\u50cf\u548c\u8868\u683c\uff1f<\/strong><br \/><code>python-docx<\/code>\u5e93\u652f\u6301\u5728Word\u6587\u6863\u4e2d\u63d2\u5165\u56fe\u50cf\u548c\u8868\u683c\u3002\u53ef\u4ee5\u4f7f\u7528<code>add_picture<\/code>\u65b9\u6cd5\u6dfb\u52a0\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528<code>add_table<\/code>\u65b9\u6cd5\u521b\u5efa\u8868\u683c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u63d2\u5165\u56fe\u50cf\u548c\u8868\u683c\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from docx import Document\nfrom docx.shared import Inches\n\ndoc = Document()\ndoc.add_heading(&#39;\u5305\u542b\u56fe\u50cf\u548c\u8868\u683c\u7684\u6587\u6863&#39;, level=1)\n\n# \u6dfb\u52a0\u56fe\u50cf\ndoc.add_picture(&#39;image.png&#39;, width=Inches(2))\n\n# \u521b\u5efa\u8868\u683c\ntable = doc.add_table(rows=2, cols=2)\ncell = table.cell(0, 0)\ncell.text = &#39;\u5355\u5143\u683c1&#39;\ncell = table.cell(0, 1)\ncell.text = &#39;\u5355\u5143\u683c2&#39;\ndoc.save(&#39;document_with_image_and_table.docx&#39;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8c03\u7528Word\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528python-docx\u5e93\u3001pywin32\u5e93\u3001\u4ee5\u53ca [&hellip;]","protected":false},"author":3,"featured_media":980781,"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\/980774"}],"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=980774"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980774\/revisions"}],"predecessor-version":[{"id":980787,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980774\/revisions\/980787"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/980781"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=980774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=980774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=980774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}