{"id":1071154,"date":"2025-01-08T11:04:45","date_gmt":"2025-01-08T03:04:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1071154.html"},"modified":"2025-01-08T11:04:47","modified_gmt":"2025-01-08T03:04:47","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%90%88%e5%b9%b6word%e5%8d%95%e5%85%83%e6%a0%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1071154.html","title":{"rendered":"\u5982\u4f55\u7528python\u5408\u5e76word\u5355\u5143\u683c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101718\/d0171525-8ce8-496e-898f-73fd074deff9.webp\" alt=\"\u5982\u4f55\u7528python\u5408\u5e76word\u5355\u5143\u683c\" \/><\/p>\n<p><p> <strong>\u7528python\u5408\u5e76word\u5355\u5143\u683c\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528python-docx\u5e93\u3001\u6307\u5b9a\u5355\u5143\u683c\u7684\u4f4d\u7f6e\u3001\u8c03\u7528\u5408\u5e76\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u5404\u79cd\u4efb\u52a1\uff0c\u5305\u62ec\u5904\u7406Microsoft Word\u6587\u6863\u3002\u5408\u5e76Word\u8868\u683c\u4e2d\u7684\u5355\u5143\u683c\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u751f\u6210\u52a8\u6001\u62a5\u544a\u6216\u6587\u6863\u65f6\u3002\u4f7f\u7528python-docx\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528python-docx\u5e93\u6765\u5408\u5e76Word\u4e2d\u7684\u5355\u5143\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165python-docx\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86python-docx\u5e93\u3002\u5982\u679c\u8fd8\u6ca1\u6709\u5b89\u88c5\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 python-docx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx import Document<\/p>\n<p>from docx.shared import Inches<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u548c\u6253\u5f00Word\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\u6216\u8005\u6253\u5f00\u4e00\u4e2a\u5df2\u6709\u7684\u6587\u6863\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u65b0\u6587\u6863\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863<\/p>\n<p>doc = Document()<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898<\/strong><\/h2>\n<p>doc.add_heading(&#39;\u793a\u4f8b\u6587\u6863&#39;, 0)<\/p>\n<h2><strong>\u6dfb\u52a0\u6bb5\u843d<\/strong><\/h2>\n<p>doc.add_paragraph(&#39;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6587\u6863\uff0c\u7528\u4e8e\u5c55\u793a\u5982\u4f55\u5408\u5e76Word\u8868\u683c\u4e2d\u7684\u5355\u5143\u683c\u3002&#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>\u5982\u679c\u8981\u6253\u5f00\u4e00\u4e2a\u5df2\u6709\u7684\u6587\u6863\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u5df2\u6709\u7684Word\u6587\u6863<\/p>\n<p>doc = Document(&#39;example.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6dfb\u52a0\u8868\u683c\u5e76\u5408\u5e76\u5355\u5143\u683c<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6dfb\u52a0\u4e00\u4e2a\u8868\u683c\uff0c\u5e76\u5408\u5e76\u67d0\u4e9b\u5355\u5143\u683c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u4e00\u4e2a\u8868\u683c\uff0c\u5305\u542b3\u884c3\u5217<\/p>\n<p>table = doc.add_table(rows=3, cols=3)<\/p>\n<h2><strong>\u5411\u8868\u683c\u4e2d\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>table.cell(0, 0).text = &#39;\u5355\u5143\u683c1&#39;<\/p>\n<p>table.cell(0, 1).text = &#39;\u5355\u5143\u683c2&#39;<\/p>\n<p>table.cell(0, 2).text = &#39;\u5355\u5143\u683c3&#39;<\/p>\n<p>table.cell(1, 0).text = &#39;\u5355\u5143\u683c4&#39;<\/p>\n<p>table.cell(1, 1).text = &#39;\u5355\u5143\u683c5&#39;<\/p>\n<p>table.cell(1, 2).text = &#39;\u5355\u5143\u683c6&#39;<\/p>\n<p>table.cell(2, 0).text = &#39;\u5355\u5143\u683c7&#39;<\/p>\n<p>table.cell(2, 1).text = &#39;\u5355\u5143\u683c8&#39;<\/p>\n<p>table.cell(2, 2).text = &#39;\u5355\u5143\u683c9&#39;<\/p>\n<h2><strong>\u5408\u5e76\u5355\u5143\u683c\uff0c\u5408\u5e76\u7b2c\u4e00\u884c\u7684\u7b2c\u4e8c\u548c\u7b2c\u4e09\u4e2a\u5355\u5143\u683c<\/strong><\/h2>\n<p>a = table.cell(0, 1)  # \u8d77\u59cb\u5355\u5143\u683c<\/p>\n<p>b = table.cell(0, 2)  # \u7ed3\u675f\u5355\u5143\u683c<\/p>\n<p>a.merge(b)<\/p>\n<h2><strong>\u5408\u5e76\u7b2c\u4e8c\u5217\u7684\u7b2c\u4e8c\u548c\u7b2c\u4e09\u4e2a\u5355\u5143\u683c<\/strong><\/h2>\n<p>c = table.cell(1, 1)  # \u8d77\u59cb\u5355\u5143\u683c<\/p>\n<p>d = table.cell(2, 1)  # \u7ed3\u675f\u5355\u5143\u683c<\/p>\n<p>c.merge(d)<\/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><h3>\u56db\u3001\u8be6\u7ec6\u8bf4\u660e\u5408\u5e76\u5355\u5143\u683c\u7684\u8fc7\u7a0b<\/h3>\n<\/p>\n<p><p>\u5408\u5e76\u5355\u5143\u683c\u7684\u6838\u5fc3\u5728\u4e8e<code>merge<\/code>\u65b9\u6cd5\u3002<code>merge<\/code>\u65b9\u6cd5\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u8d77\u59cb\u5355\u5143\u683c\u548c\u7ed3\u675f\u5355\u5143\u683c\u3002\u8fd9\u4e24\u4e2a\u5355\u5143\u683c\u4e4b\u95f4\u7684\u6240\u6709\u5355\u5143\u683c\u5c06\u88ab\u5408\u5e76\u6210\u4e00\u4e2a\u5355\u5143\u683c\u3002\u4ee5\u4e0b\u662f\u5408\u5e76\u5355\u5143\u683c\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6307\u5b9a\u8d77\u59cb\u5355\u5143\u683c\u548c\u7ed3\u675f\u5355\u5143\u683c<\/strong>\uff1a\u4f7f\u7528<code>table.cell(row, col)<\/code>\u65b9\u6cd5\u6765\u6307\u5b9a\u8981\u5408\u5e76\u7684\u8d77\u59cb\u5355\u5143\u683c\u548c\u7ed3\u675f\u5355\u5143\u683c\u3002\u884c\u548c\u5217\u7684\u7d22\u5f15\u90fd\u662f\u4ece0\u5f00\u59cb\u7684\u3002<\/li>\n<li><strong>\u8c03\u7528merge\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528\u8d77\u59cb\u5355\u5143\u683c\u7684<code>merge<\/code>\u65b9\u6cd5\uff0c\u5c06\u8d77\u59cb\u5355\u5143\u683c\u548c\u7ed3\u675f\u5355\u5143\u683c\u4e4b\u95f4\u7684\u6240\u6709\u5355\u5143\u683c\u5408\u5e76\u6210\u4e00\u4e2a\u5355\u5143\u683c\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5408\u5e76\u8868\u683c\u7684\u7b2c\u4e00\u884c\u7684\u7b2c\u4e8c\u548c\u7b2c\u4e09\u4e2a\u5355\u5143\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = table.cell(0, 1)  # \u8d77\u59cb\u5355\u5143\u683c<\/p>\n<p>b = table.cell(0, 2)  # \u7ed3\u675f\u5355\u5143\u683c<\/p>\n<p>a.merge(b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u590d\u6742\u7684\u8868\u683c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8868\u683c\u53ef\u80fd\u4f1a\u66f4\u52a0\u590d\u6742\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u793a\u4f8b\uff0c\u5176\u4e2d\u5305\u542b\u66f4\u591a\u7684\u884c\u548c\u5217\uff0c\u4ee5\u53ca\u66f4\u591a\u7684\u5408\u5e76\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863<\/p>\n<p>doc = Document()<\/p>\n<h2><strong>\u6dfb\u52a0\u4e00\u4e2a\u8868\u683c\uff0c\u5305\u542b5\u884c5\u5217<\/strong><\/h2>\n<p>table = doc.add_table(rows=5, cols=5)<\/p>\n<h2><strong>\u5411\u8868\u683c\u4e2d\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    for j in range(5):<\/p>\n<p>        table.cell(i, j).text = f&#39;\u5355\u5143\u683c({i+1},{j+1})&#39;<\/p>\n<h2><strong>\u5408\u5e76\u8868\u683c\u4e2d\u7684\u67d0\u4e9b\u5355\u5143\u683c<\/strong><\/h2>\n<p>table.cell(0, 0).merge(table.cell(0, 1))<\/p>\n<p>table.cell(0, 2).merge(table.cell(1, 2))<\/p>\n<p>table.cell(2, 0).merge(table.cell(2, 1))<\/p>\n<p>table.cell(3, 3).merge(table.cell(4, 4))<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u6863<\/strong><\/h2>\n<p>doc.save(&#39;complex_table.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b5\u884c5\u5217\u7684\u8868\u683c\uff0c\u5e76\u5411\u6bcf\u4e2a\u5355\u5143\u683c\u4e2d\u6dfb\u52a0\u4e86\u6570\u636e\u3002\u7136\u540e\uff0c\u6211\u4eec\u5408\u5e76\u4e86\u8868\u683c\u4e2d\u7684\u67d0\u4e9b\u5355\u5143\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528python-docx\u5e93\u5408\u5e76Word\u8868\u683c\u4e2d\u7684\u5355\u5143\u683c\u662f\u975e\u5e38\u7b80\u5355\u7684\u3002\u53ea\u9700\u8981\u6307\u5b9a\u8981\u5408\u5e76\u7684\u8d77\u59cb\u5355\u5143\u683c\u548c\u7ed3\u675f\u5355\u5143\u683c\uff0c\u7136\u540e\u8c03\u7528<code>merge<\/code>\u65b9\u6cd5\u5373\u53ef\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5728\u751f\u6210\u52a8\u6001\u62a5\u544a\u548c\u6587\u6863\u65f6\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528Python\u5408\u5e76Word\u8868\u683c\u4e2d\u7684\u5355\u5143\u683c\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u8bf7\u968f\u65f6\u7559\u8a00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u7279\u5b9a\u5e93\u6765\u5408\u5e76Word\u5355\u5143\u683c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5408\u5e76Word\u5355\u5143\u683c\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<code>pip install python-docx<\/code>\u547d\u4ee4\u5b8c\u6210\u3002\u5b89\u88c5\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u52a0\u8f7dWord\u6587\u6863\uff0c\u5b9a\u4f4d\u5230\u7279\u5b9a\u7684\u8868\u683c\u548c\u5355\u5143\u683c\uff0c\u5e76\u4f7f\u7528<code>cell.merge()<\/code>\u65b9\u6cd5\u6765\u5408\u5e76\u6240\u9700\u7684\u5355\u5143\u683c\u3002<\/p>\n<p><strong>\u5408\u5e76\u5355\u5143\u683c\u65f6\uff0c\u4fdd\u7559\u539f\u6709\u5185\u5bb9\u5417\uff1f<\/strong><br \/>\u5728\u5408\u5e76\u5355\u5143\u683c\u65f6\uff0c\u53ea\u6709\u5de6\u4e0a\u89d2\u7684\u5355\u5143\u683c\u5185\u5bb9\u4f1a\u88ab\u4fdd\u7559\uff0c\u5176\u4ed6\u88ab\u5408\u5e76\u7684\u5355\u5143\u683c\u5185\u5bb9\u5c06\u4f1a\u4e22\u5931\u3002\u56e0\u6b64\uff0c\u5728\u5408\u5e76\u4e4b\u524d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u624b\u52a8\u5c06\u9700\u8981\u4fdd\u7559\u7684\u4fe1\u606f\u590d\u5236\u5230\u5de6\u4e0a\u89d2\u7684\u5355\u5143\u683c\u4e2d\uff0c\u4ee5\u514d\u4e22\u5931\u91cd\u8981\u6570\u636e\u3002<\/p>\n<p><strong>\u5408\u5e76\u5355\u5143\u683c\u540e\u5982\u4f55\u4fdd\u5b58Word\u6587\u6863\uff1f<\/strong><br \/>\u5b8c\u6210\u5355\u5143\u683c\u5408\u5e76\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>document.save(&#39;\u6587\u4ef6\u540d.docx&#39;)<\/code>\u547d\u4ee4\u6765\u4fdd\u5b58\u4fee\u6539\u540e\u7684Word\u6587\u6863\u3002\u786e\u4fdd\u5728\u4fdd\u5b58\u65f6\u63d0\u4f9b\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u540d\uff0c\u4ee5\u9632\u8986\u76d6\u539f\u59cb\u6587\u4ef6\u3002\u5982\u679c\u60a8\u5e0c\u671b\u8986\u76d6\u539f\u6587\u4ef6\uff0c\u53ea\u9700\u4f7f\u7528\u76f8\u540c\u7684\u6587\u4ef6\u540d\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528python\u5408\u5e76word\u5355\u5143\u683c\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528python-docx\u5e93\u3001\u6307\u5b9a\u5355\u5143\u683c\u7684\u4f4d\u7f6e\u3001\u8c03\u7528\u5408\u5e76\u51fd\u6570 Pyt [&hellip;]","protected":false},"author":3,"featured_media":1071159,"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\/1071154"}],"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=1071154"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071154\/revisions"}],"predecessor-version":[{"id":1071163,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071154\/revisions\/1071163"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1071159"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1071154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1071154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1071154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}