{"id":1027601,"date":"2024-12-31T10:52:20","date_gmt":"2024-12-31T02:52:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1027601.html"},"modified":"2024-12-31T10:52:23","modified_gmt":"2024-12-31T02:52:23","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","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1027601.html","title":{"rendered":"\u5982\u4f55\u7528python\u5408\u5e76word\u5355\u5143\u683c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/39c22f8e-3af4-426e-9e43-c718418fd6f1.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u5408\u5e76word\u5355\u5143\u683c\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5408\u5e76Word\u5355\u5143\u683c\u7684\u65b9\u6cd5\u6709\uff1a\u5229\u7528python-docx\u5e93\u3001\u6307\u5b9a\u5355\u5143\u683c\u8303\u56f4\u3001\u8c03\u7528\u5408\u5e76\u65b9\u6cd5\u3002<\/strong>  <\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\uff1a<strong>\u5229\u7528python-docx\u5e93<\/strong>\u3002python-docx\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u548c\u66f4\u65b0Microsoft Word (.docx) \u6587\u4ef6\u7684Python\u5e93\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u5904\u7406Word\u6587\u6863\u4e2d\u7684\u8868\u683c\uff0c\u5305\u62ec\u5408\u5e76\u5355\u5143\u683c\u3002\u901a\u8fc7\u5b89\u88c5\u548c\u4f7f\u7528\u8fd9\u4e2a\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u7f16\u5199\u811a\u672c\u6765\u5408\u5e76Word\u8868\u683c\u4e2d\u7684\u5355\u5143\u683c\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u5185\u5bb9\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5python-docx\u5e93<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528python-docx\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\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><h3>\u4e8c\u3001\u521b\u5efa\u6216\u6253\u5f00\u4e00\u4e2aWord\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u5408\u5e76\u5355\u5143\u683c\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\u6216\u6253\u5f00\u4e00\u4e2a\u73b0\u6709\u7684\u6587\u6863\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u6267\u884c\u8fd9\u4e9b\u64cd\u4f5c\u7684\u793a\u4f8b\u4ee3\u7801\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<\/strong><\/h2>\n<p>doc = Document()<\/p>\n<h2><strong>\u6216\u8005\u6253\u5f00\u4e00\u4e2a\u73b0\u6709\u7684Word\u6587\u6863<\/strong><\/h2>\n<h2><strong>doc = Document(&#39;path_to_existing_doc.docx&#39;)<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6dfb\u52a0\u8868\u683c\u5230\u6587\u6863\u4e2d<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6b63\u5728\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u6863\uff0c\u4f60\u9700\u8981\u9996\u5148\u6dfb\u52a0\u4e00\u4e2a\u8868\u683c\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u6dfb\u52a0\u4e00\u4e2a\u5177\u67092\u884c2\u5217\u7684\u8868\u683c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u4e00\u4e2a2\u884c2\u5217\u7684\u8868\u683c<\/p>\n<p>table = doc.add_table(rows=2, cols=2)<\/p>\n<h2><strong>\u586b\u5145\u8868\u683c\u4e2d\u7684\u5355\u5143\u683c<\/strong><\/h2>\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<p><h3>\u56db\u3001\u5408\u5e76\u5355\u5143\u683c<\/h3>\n<\/p>\n<p><p>\u5408\u5e76\u8868\u683c\u4e2d\u7684\u5355\u5143\u683c\u662f\u901a\u8fc7\u8c03\u7528\u5355\u5143\u683c\u7684<code>merge<\/code>\u65b9\u6cd5\u6765\u5b8c\u6210\u7684\u3002\u4f60\u9700\u8981\u6307\u5b9a\u8981\u5408\u5e76\u7684\u5355\u5143\u683c\u8303\u56f4\u7684\u8d77\u59cb\u548c\u7ed3\u675f\u5355\u5143\u683c\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5408\u5e76\u8868\u683c\u4e2d\u7b2c\u4e00\u884c\u7684\u4e24\u4e2a\u5355\u5143\u683c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5408\u5e76\u7b2c\u4e00\u884c\u7684\u4e24\u4e2a\u5355\u5143\u683c<\/p>\n<p>a = table.cell(0, 0)<\/p>\n<p>b = table.cell(0, 1)<\/p>\n<p>a.merge(b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4fdd\u5b58\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u5b8c\u6210\u6240\u6709\u7684\u64cd\u4f5c\u540e\uff0c\u4f60\u9700\u8981\u5c06\u6587\u6863\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u4ee5\u4e0b\u662f\u4fdd\u5b58\u6587\u6863\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u6587\u6863<\/p>\n<p>doc.save(&#39;path_to_save_doc.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u6574\u5408\u5728\u4e00\u8d77\u7684\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b\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<\/strong><\/h2>\n<p>doc = Document()<\/p>\n<h2><strong>\u6dfb\u52a0\u4e00\u4e2a2\u884c2\u5217\u7684\u8868\u683c<\/strong><\/h2>\n<p>table = doc.add_table(rows=2, cols=2)<\/p>\n<h2><strong>\u586b\u5145\u8868\u683c\u4e2d\u7684\u5355\u5143\u683c<\/strong><\/h2>\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<h2><strong>\u5408\u5e76\u7b2c\u4e00\u884c\u7684\u4e24\u4e2a\u5355\u5143\u683c<\/strong><\/h2>\n<p>a = table.cell(0, 0)<\/p>\n<p>b = table.cell(0, 1)<\/p>\n<p>a.merge(b)<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u6863<\/strong><\/h2>\n<p>doc.save(&#39;merged_cells.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u590d\u6742\u8868\u683c<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u66f4\u590d\u6742\u7684\u8868\u683c\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5408\u5e76\u591a\u4e2a\u5355\u5143\u683c\u6216\u8de8\u8d8a\u591a\u4e2a\u884c\u548c\u5217\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5408\u5e76\u591a\u884c\u591a\u5217\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u4e00\u4e2a3\u884c3\u5217\u7684\u8868\u683c<\/p>\n<p>table = doc.add_table(rows=3, cols=3)<\/p>\n<h2><strong>\u586b\u5145\u8868\u683c\u4e2d\u7684\u5355\u5143\u683c<\/strong><\/h2>\n<p>for i in range(3):<\/p>\n<p>    for j in range(3):<\/p>\n<p>        table.cell(i, j).text = f&#39;Cell {i+1},{j+1}&#39;<\/p>\n<h2><strong>\u5408\u5e76\u7b2c\u4e00\u4e2a\u5355\u5143\u683c\u548c\u6700\u540e\u4e00\u4e2a\u5355\u5143\u683c\u4e4b\u95f4\u7684\u6240\u6709\u5355\u5143\u683c<\/strong><\/h2>\n<p>a = table.cell(0, 0)<\/p>\n<p>b = table.cell(2, 2)<\/p>\n<p>a.merge(b)<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u6863<\/strong><\/h2>\n<p>doc.save(&#39;merged_cells_complex.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5176\u4ed6\u6709\u7528\u7684\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5408\u5e76\u5355\u5143\u683c\uff0cpython-docx\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u6709\u7528\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u8bbe\u7f6e\u5355\u5143\u683c\u7684\u6837\u5f0f\u3001\u8c03\u6574\u8868\u683c\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx.shared import Pt<\/p>\n<h2><strong>\u8bbe\u7f6e\u5355\u5143\u683c\u7684\u5b57\u4f53\u5927\u5c0f<\/strong><\/h2>\n<p>run = table.cell(0, 0).paragraphs[0].runs[0]<\/p>\n<p>run.font.size = Pt(14)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8868\u683c\u7684\u5bbd\u5ea6<\/strong><\/h2>\n<p>for i in range(3):<\/p>\n<p>    for j in range(3):<\/p>\n<p>        table.cell(i, j).width = Pt(100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528python-docx\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u521b\u5efa\u548c\u64cd\u4f5cWord\u6587\u6863\u4e2d\u7684\u8868\u683c\uff0c\u5305\u62ec\u5408\u5e76\u5355\u5143\u683c\u3002<strong>\u5b89\u88c5python-docx\u5e93\u3001\u521b\u5efa\u6216\u6253\u5f00Word\u6587\u6863\u3001\u6dfb\u52a0\u8868\u683c\u5230\u6587\u6863\u4e2d\u3001\u5408\u5e76\u5355\u5143\u683c\u3001\u4fdd\u5b58\u6587\u6863<\/strong>\u662f\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\u7684\u5173\u952e\u6b65\u9aa4\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u7528Python\u5408\u5e76Word\u5355\u5143\u683c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u5408\u5e76Word\u6587\u6863\u4e2d\u7684\u5355\u5143\u683c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>python-docx<\/code>\u5e93\u6765\u5904\u7406Word\u6587\u6863\uff0c\u5305\u62ec\u5408\u5e76\u8868\u683c\u4e2d\u7684\u5355\u5143\u683c\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86\u8be5\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>pip install python-docx<\/code>\u6765\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u52a0\u8f7d\u4f60\u7684Word\u6587\u6863\uff0c\u627e\u5230\u9700\u8981\u5408\u5e76\u7684\u8868\u683c\u548c\u5355\u5143\u683c\uff0c\u4f7f\u7528<code>merge()<\/code>\u65b9\u6cd5\u8fdb\u884c\u5408\u5e76\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from docx import Document\n\ndoc = Document(&#39;your_document.docx&#39;)\ntable = doc.tables[0]  # \u83b7\u53d6\u7b2c\u4e00\u4e2a\u8868\u683c\ncell_to_merge = table.cell(0, 0)  # \u9009\u62e9\u8981\u5408\u5e76\u7684\u5355\u5143\u683c\ncell_to_merge.merge(table.cell(0, 1))  # \u5408\u5e76\u4e0e\u76f8\u90bb\u5355\u5143\u683c\ndoc.save(&#39;merged_document.docx&#39;)  # \u4fdd\u5b58\u4fee\u6539\u540e\u7684\u6587\u6863\n<\/code><\/pre>\n<p><strong>\u5408\u5e76\u5355\u5143\u683c\u540e\uff0c\u5982\u4f55\u5904\u7406\u6587\u672c\u5185\u5bb9\uff1f<\/strong><br \/>\u5728\u5408\u5e76\u5355\u5143\u683c\u540e\uff0c\u901a\u5e38\u9700\u8981\u5904\u7406\u5408\u5e76\u540e\u5355\u5143\u683c\u7684\u6587\u672c\u5185\u5bb9\u3002\u53ef\u4ee5\u5c06\u539f\u59cb\u5355\u5143\u683c\u7684\u6587\u672c\u5185\u5bb9\u590d\u5236\u5230\u5408\u5e76\u540e\u7684\u5355\u5143\u683c\u4e2d\u3002\u5408\u5e76\u64cd\u4f5c\u540e\uff0c\u539f\u59cb\u5355\u5143\u683c\u7684\u5185\u5bb9\u5c06\u88ab\u6e05\u9664\uff0c\u56e0\u6b64\u5728\u5408\u5e76\u4e4b\u524d\u5e94\u5148\u5c06\u9700\u8981\u4fdd\u7559\u7684\u6587\u672c\u79fb\u52a8\u5230\u76ee\u6807\u5355\u5143\u683c\u3002<\/p>\n<pre><code class=\"language-python\">cell_to_merge.text = &#39;\u5408\u5e76\u540e\u7684\u6587\u672c&#39;  # \u8bbe\u7f6e\u5408\u5e76\u540e\u7684\u5355\u5143\u683c\u6587\u672c\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u5408\u5e76Word\u5355\u5143\u683c\u65f6\uff0c\u6709\u54ea\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u5408\u5e76Word\u5355\u5143\u683c\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a  <\/p>\n<ol>\n<li>\u5408\u5e76\u5355\u5143\u683c\u65f6\uff0c\u786e\u4fdd\u9009\u62e9\u7684\u5355\u5143\u683c\u5728\u540c\u4e00\u884c\u6216\u540c\u4e00\u5217\u5185\u3002  <\/li>\n<li>\u5408\u5e76\u540e\uff0c\u539f\u59cb\u5355\u5143\u683c\u7684\u5185\u5bb9\u5c06\u88ab\u6e05\u9664\uff0c\u56e0\u6b64\u52a1\u5fc5\u5728\u5408\u5e76\u524d\u5907\u4efd\u91cd\u8981\u4fe1\u606f\u3002  <\/li>\n<li>\u786e\u4fdd\u6587\u6863\u683c\u5f0f\u6b63\u786e\uff0c\u4ee5\u907f\u514d\u5408\u5e76\u64cd\u4f5c\u5bfc\u81f4\u683c\u5f0f\u9519\u4e71\u3002  <\/li>\n<li>\u4f7f\u7528\u5408\u5e76\u64cd\u4f5c\u540e\uff0c\u52a1\u5fc5\u4fdd\u5b58\u4fee\u6539\u540e\u7684\u6587\u6863\uff0c\u4ee5\u786e\u4fdd\u66f4\u6539\u751f\u6548\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5408\u5e76Word\u5355\u5143\u683c\u7684\u65b9\u6cd5\u6709\uff1a\u5229\u7528python-docx\u5e93\u3001\u6307\u5b9a\u5355\u5143\u683c\u8303\u56f4\u3001\u8c03\u7528\u5408\u5e76\u65b9\u6cd5\u3002 \u8be6\u7ec6 [&hellip;]","protected":false},"author":3,"featured_media":1027613,"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\/1027601"}],"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=1027601"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1027601\/revisions"}],"predecessor-version":[{"id":1027617,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1027601\/revisions\/1027617"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1027613"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1027601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1027601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1027601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}