{"id":958937,"date":"2024-12-27T03:36:19","date_gmt":"2024-12-26T19:36:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/958937.html"},"modified":"2024-12-27T03:36:21","modified_gmt":"2024-12-26T19:36:21","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python-%e5%86%99word","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/958937.html","title":{"rendered":"\u5982\u4f55\u7528python \u5199word"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101903\/382cacd4-fd4d-4137-9832-88895a8775e8.webp\" alt=\"\u5982\u4f55\u7528python \u5199word\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5199\u5165Word\u6587\u6863\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528python-docx\u5e93\u3001\u81ea\u52a8\u5316Word\u5e94\u7528\u3001\u4ee5\u53ca\u751f\u6210HTML\u6587\u4ef6\u540e\u8f6c\u6362\u4e3aWord\u6587\u6863\u3002python-docx\u5e93\u662f\u6700\u4e3a\u5e38\u89c1\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u76f4\u63a5\u64cd\u4f5cWord\u6587\u6863\u7684API\u63a5\u53e3<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528python-docx\u5e93\u6765\u7f16\u5199\u548c\u4fee\u6539Word\u6587\u6863\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u548c\u57fa\u7840\u8bbe\u7f6e<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528python-docx\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5728\u547d\u4ee4\u884c\u4e2d\u5b89\u88c5\u5b83\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\u4f60\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\u5e93\u5e76\u5f00\u59cb\u521b\u5efa\u6216\u7f16\u8f91Word\u6587\u6863\u3002<\/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<\/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;, level=1)<\/p>\n<h2><strong>\u6dfb\u52a0\u6bb5\u843d<\/strong><\/h2>\n<p>doc.add_paragraph(&#39;This is a paragraph in the Word document.&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u6863<\/strong><\/h2>\n<p>doc.save(&#39;example.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u4e0a\u8ff0\u7b80\u5355\u7684\u4ee3\u7801\uff0c\u4f60\u5df2\u7ecf\u6210\u529f\u5730\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u6807\u9898\u548c\u6bb5\u843d\u7684Word\u6587\u6863<\/strong>\u3002\u8fd9\u662f\u4f7f\u7528python-docx\u8fdb\u884cWord\u6587\u6863\u64cd\u4f5c\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u6dfb\u52a0\u548c\u683c\u5f0f\u5316\u6587\u672c<\/p>\n<\/p>\n<p><p>\u5728\u6587\u6863\u4e2d\u6dfb\u52a0\u548c\u683c\u5f0f\u5316\u6587\u672c\u662f\u521b\u5efaWord\u6587\u6863\u7684\u57fa\u672c\u64cd\u4f5c\u3002\u4f7f\u7528python-docx\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6dfb\u52a0\u6bb5\u843d\u548c\u6587\u672c<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>add_paragraph()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5411\u6587\u6863\u4e2d\u6dfb\u52a0\u6bb5\u843d\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a<code>Paragraph<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7528\u6765\u64cd\u4f5c\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">doc.add_paragraph(&#39;This is another paragraph with more text.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u683c\u5f0f\u5316\u6587\u672c<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>add_run()<\/code>\u65b9\u6cd5\u5bf9\u6bb5\u843d\u4e2d\u7684\u90e8\u5206\u6587\u672c\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u5982\u52a0\u7c97\u3001\u659c\u4f53\u3001\u4e0b\u5212\u7ebf\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">paragraph = doc.add_paragraph(&#39;This is a paragraph with &#39;)<\/p>\n<p>run = paragraph.add_run(&#39;bold text&#39;)<\/p>\n<p>run.bold = True<\/p>\n<p>run = paragraph.add_run(&#39; and &#39;)<\/p>\n<p>run = paragraph.add_run(&#39;italic text.&#39;)<\/p>\n<p>run.italic = True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u6dfb\u52a0\u5176\u4ed6\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u6587\u672c\uff0cWord\u6587\u6863\u4e2d\u901a\u5e38\u8fd8\u5305\u542b\u5176\u4ed6\u5143\u7d20\uff0c\u5982\u8868\u683c\u3001\u56fe\u7247\u3001\u5217\u8868\u7b49\u3002python-docx\u540c\u6837\u652f\u6301\u8fd9\u4e9b\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6dfb\u52a0\u8868\u683c<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>add_table()<\/code>\u65b9\u6cd5\u521b\u5efa\u548c\u64cd\u4f5c\u8868\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">table = doc.add_table(rows=2, cols=2)<\/p>\n<p>table.cell(0, 0).text = &#39;Cell 1,1&#39;<\/p>\n<p>table.cell(0, 1).text = &#39;Cell 1,2&#39;<\/p>\n<p>table.cell(1, 0).text = &#39;Cell 2,1&#39;<\/p>\n<p>table.cell(1, 1).text = &#39;Cell 2,2&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6dfb\u52a0\u56fe\u7247<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>add_picture()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5411\u6587\u6863\u4e2d\u63d2\u5165\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">doc.add_picture(&#39;image.png&#39;, width=Inches(1.25))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u521b\u5efa\u5217\u8868<\/strong><\/li>\n<\/ol>\n<p><p>\u6709\u5e8f\u548c\u65e0\u5e8f\u5217\u8868\u90fd\u53ef\u4ee5\u901a\u8fc7<code>add_paragraph()<\/code>\u65b9\u6cd5\u5e76\u914d\u5408\u6bb5\u843d\u6837\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">doc.add_paragraph(&#39;First item in the list&#39;, style=&#39;ListBullet&#39;)<\/p>\n<p>doc.add_paragraph(&#39;Second item in the list&#39;, style=&#39;ListNumber&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4fee\u6539\u548c\u4fdd\u5b58\u6587\u6863<\/p>\n<\/p>\n<p><p>\u7f16\u8f91\u5df2\u6709\u7684Word\u6587\u6863\u540c\u6837\u662fpython-docx\u7684\u5f3a\u9879\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Document\u5bf9\u8c61\u52a0\u8f7d\u5df2\u6709\u7684\u6587\u6863\u5e76\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">doc = Document(&#39;existing_document.docx&#39;)<\/p>\n<h2><strong>\u4fee\u6539\u6587\u6863\u5185\u5bb9<\/strong><\/h2>\n<p>for paragraph in doc.paragraphs:<\/p>\n<p>    if &#39;old text&#39; in paragraph.text:<\/p>\n<p>        paragraph.text = paragraph.text.replace(&#39;old text&#39;, &#39;new text&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u4fee\u6539\u540e\u7684\u6587\u6863<\/strong><\/h2>\n<p>doc.save(&#39;modified_document.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u8fdb\u9636\u529f\u80fd\u548c\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li><strong>\u6837\u5f0f\u548c\u6a21\u677f<\/strong><\/li>\n<\/ol>\n<p><p>python-docx\u652f\u6301\u5e94\u7528\u81ea\u5b9a\u4e49\u6837\u5f0f\u548c\u6a21\u677f\uff0c\u4f7f\u5f97\u751f\u6210\u7684\u6587\u6863\u66f4\u5177\u4e2a\u6027\u5316\u548c\u4e13\u4e1a\u5316\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u590d\u6742\u6587\u6863\u7ed3\u6784<\/strong><\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u590d\u6742\u7ed3\u6784\u7684\u6587\u6863\uff0c\u5982\u591a\u7ea7\u6807\u9898\u3001\u590d\u6742\u8868\u683c\u7b49\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u6df1\u5165\u5730\u5b66\u4e60python-docx\u7684API\u5e76\u7ed3\u5408Word\u6587\u6863\u7684\u683c\u5f0f\u8981\u6c42\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6027\u80fd\u4e0e\u9650\u5236<\/strong><\/li>\n<\/ol>\n<p><p>\u7531\u4e8epython-docx\u64cd\u4f5c\u7684\u662f\u6587\u6863\u5bf9\u8c61\u6a21\u578b\uff0c\u5904\u7406\u5927\u578b\u6587\u6863\u65f6\u6027\u80fd\u53ef\u80fd\u4f1a\u53d7\u9650\u3002\u53e6\u5916\uff0cpython-docx\u4e0d\u652f\u6301\u67d0\u4e9b\u9ad8\u7ea7Word\u529f\u80fd\uff0c\u5982\u5b8f\u548c\u4e00\u4e9b\u590d\u6742\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528python-docx\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728Python\u4e2d\u521b\u5efa\u548c\u7f16\u8f91Word\u6587\u6863<\/strong>\u3002\u4ece\u57fa\u7840\u7684\u6587\u672c\u6dfb\u52a0\u5230\u590d\u6742\u7684\u8868\u683c\u548c\u56fe\u7247\u63d2\u5165\uff0cpython-docx\u90fd\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u529f\u80fd\u3002\u901a\u8fc7\u7ed3\u5408Python\u7684\u5f3a\u5927\u7f16\u7a0b\u80fd\u529b\uff0c\u4f60\u53ef\u4ee5\u81ea\u52a8\u5316\u751f\u6210\u4e13\u4e1a\u7684Word\u6587\u6863\uff0c\u6781\u5927\u5730\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4f7f\u7528Python\u751f\u6210Word\u6587\u6863\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u521b\u5efaWord\u6587\u6863\uff0c\u901a\u5e38\u4f7f\u7528<code>python-docx<\/code>\u5e93\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install python-docx<\/code>\u5b8c\u6210\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\uff0c\u6dfb\u52a0\u6587\u672c\u3001\u6807\u9898\u3001\u6bb5\u843d\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">from docx import Document\n\n# \u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\ndoc = Document()\n\n# \u6dfb\u52a0\u6807\u9898\ndoc.add_heading(&#39;\u6211\u7684\u6587\u6863\u6807\u9898&#39;, level=1)\n\n# \u6dfb\u52a0\u6bb5\u843d\ndoc.add_paragraph(&#39;\u8fd9\u662f\u6211\u7684\u7b2c\u4e00\u6bb5\u6587\u5b57\u3002&#39;)\n\n# \u4fdd\u5b58\u6587\u6863\ndoc.save(&#39;\u6211\u7684\u6587\u6863.docx&#39;)\n<\/code><\/pre>\n<p><strong>Python\u5982\u4f55\u5728Word\u4e2d\u6dfb\u52a0\u8868\u683c\u548c\u56fe\u50cf\uff1f<\/strong><br \/>\u4f7f\u7528<code>python-docx<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Word\u6587\u6863\u4e2d\u63d2\u5165\u8868\u683c\u548c\u56fe\u50cf\u3002\u5bf9\u4e8e\u8868\u683c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>add_table<\/code>\u65b9\u6cd5\uff0c\u6307\u5b9a\u884c\u6570\u548c\u5217\u6570\uff1b\u5bf9\u4e8e\u56fe\u50cf\uff0c\u4f7f\u7528<code>add_picture<\/code>\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u76f8\u5173\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\"># \u6dfb\u52a0\u8868\u683c\ntable = doc.add_table(rows=2, cols=2)\ncell = table.cell(0, 0)\ncell.text = &#39;\u5355\u5143\u683c1&#39;\n\n# \u6dfb\u52a0\u56fe\u50cf\ndoc.add_picture(&#39;\u8def\u5f84\/\u5230\/\u56fe\u50cf.png&#39;, width=docx.shared.Inches(2))\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u548c\u4fee\u6539\u73b0\u6709\u7684Word\u6587\u6863\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>Document<\/code>\u7c7b\u6253\u5f00\u73b0\u6709\u7684Word\u6587\u6863\uff0c\u5e76\u5bf9\u5176\u5185\u5bb9\u8fdb\u884c\u8bfb\u53d6\u548c\u4fee\u6539\u3002\u901a\u8fc7\u904d\u5386\u6587\u6863\u7684\u6bb5\u843d\u548c\u8868\u683c\uff0c\u60a8\u53ef\u4ee5\u63d0\u53d6\u6587\u672c\u5e76\u8fdb\u884c\u66f4\u6539\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u8bfb\u53d6\u548c\u4fee\u6539\u6bb5\u843d\uff1a<\/p>\n<pre><code class=\"language-python\">doc = Document(&#39;\u73b0\u6709\u6587\u6863.docx&#39;)\n\n# \u8bfb\u53d6\u6bb5\u843d\nfor para in doc.paragraphs:\n    print(para.text)\n\n# \u4fee\u6539\u6bb5\u843d\ndoc.paragraphs[0].text = &#39;\u4fee\u6539\u540e\u7684\u6587\u672c&#39;\n\n# \u4fdd\u5b58\u66f4\u6539\ndoc.save(&#39;\u4fee\u6539\u540e\u7684\u6587\u6863.docx&#39;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5199\u5165Word\u6587\u6863\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528python-docx\u5e93\u3001\u81ea\u52a8\u5316Wor [&hellip;]","protected":false},"author":3,"featured_media":958941,"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\/958937"}],"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=958937"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958937\/revisions"}],"predecessor-version":[{"id":958942,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958937\/revisions\/958942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/958941"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=958937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=958937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=958937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}