{"id":224339,"date":"2024-05-11T19:38:09","date_gmt":"2024-05-11T11:38:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/224339.html"},"modified":"2024-05-11T19:38:18","modified_gmt":"2024-05-11T11:38:18","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%86%99%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84word%e5%a4%84%e7%90%86%e9%a1%b5%e9%9d%a2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/224339.html","title":{"rendered":"\u5982\u4f55\u7528python\u5199\u4e00\u4e2a\u7b80\u5355\u7684word\u5904\u7406\u9875\u9762"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25143723\/178eb1aa-389b-462c-aaba-ae72e82f3cfc.webp\" alt=\"\u5982\u4f55\u7528python\u5199\u4e00\u4e2a\u7b80\u5355\u7684word\u5904\u7406\u9875\u9762\" \/><\/p>\n<p><p>\u8981\u7528Python\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Word\u5904\u7406\u9875\u9762\uff0c\u60a8\u4e3b\u8981\u9700\u8981\u638c\u63e1\u5982\u4e0b\u51e0\u4e2a\u6838\u5fc3\u65b9\u9762\uff1a<strong>\u5229\u7528Python\u7684\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u6587\u6863\u5904\u7406\u3001\u4e86\u89e3Word\u6587\u4ef6\u7684\u57fa\u672c\u7ed3\u6784\u3001\u7f16\u5199\u57fa\u7840\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u3001\u5b9e\u73b0\u6587\u6863\u7684\u57fa\u7840\u64cd\u4f5c<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u5229\u7528Python\u7684\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u6587\u6863\u5904\u7406<\/strong>\u662f\u6700\u57fa\u7840\u4e5f\u662f\u6700\u91cd\u8981\u7684\u6b65\u9aa4\u3002Python\u62e5\u6709\u8bf8\u5982<code>python-docx<\/code>\u7684\u5f3a\u5927\u5e93\uff0c\u80fd\u591f\u8ba9\u6211\u4eec\u65b9\u4fbf\u5730\u521b\u5efa\u3001\u4fee\u6539\u548c\u67e5\u8be2Word\u6587\u6863\u3002\u901a\u8fc7<code>python-docx<\/code>\u5e93\uff0c\u60a8\u80fd\u591f\u64cd\u4f5c\u6587\u6863\u7684\u5404\u4e2a\u5c42\u9762\uff0c\u5305\u62ec\u6587\u672c\u3001\u56fe\u7247\u3001\u8868\u683c\u3001\u6bb5\u843d\u683c\u5f0f\u7b49\uff0c\u4e3a\u6784\u5efaWord\u5904\u7406\u9875\u9762\u6253\u4e0b\u575a\u5b9e\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3Word\u6587\u4ef6\u57fa\u7840<\/h3>\n<\/p>\n<p><p>Word\u6587\u6863\uff08\u901a\u5e38\u4e3a.docx\u683c\u5f0f\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6587\u6863\u6587\u4ef6\u7c7b\u578b\uff0c\u7528\u4e8e\u4fdd\u5b58\u6587\u5b57\u3001\u56fe\u7247\u3001\u8868\u683c\u7b49\u4fe1\u606f\u3002\u5728\u5f00\u59cb\u7f16\u5199\u5904\u7406\u9875\u9762\u4e4b\u524d\uff0c\u5bf9Word\u6587\u4ef6\u57fa\u7840\u6709\u4e00\u4e2a\u521d\u6b65\u4e86\u89e3\u662f\u5fc5\u8981\u7684\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6587\u4ef6\u7ed3\u6784<\/strong>\uff1aWord\u6587\u6863\u7684\u5185\u90e8\u7ed3\u6784\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u538b\u7f29\u5305\uff0c\u5305\u542b\u4e86\u6587\u6863\u7684\u6240\u6709\u5143\u7d20\u548c\u5c5e\u6027\u3002\u4e86\u89e3\u8fd9\u4e00\u7ed3\u6784\u6709\u52a9\u4e8e\u7406\u89e3\u5229\u7528\u4ee3\u7801\u5982\u4f55\u64cd\u4f5c\u8fd9\u4e9b\u5143\u7d20\u3002<\/li>\n<li><strong>\u6587\u6863\u5143\u7d20<\/strong>\uff1a\u5e38\u89c1\u7684\u6587\u6863\u5143\u7d20\u6709\u6587\u672c\u3001\u56fe\u7247\u3001\u8868\u683c\u7b49\u3002\u638c\u63e1\u5982\u4f55\u7528\u4ee3\u7801\u521b\u5efa\u548c\u4fee\u6539\u8fd9\u4e9b\u5143\u7d20\u662f\u7f16\u5199\u5904\u7406\u9875\u9762\u7684\u5173\u952e\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Python\u5904\u7406Word\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u5229\u7528<code>python-docx<\/code>\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9Word\u6587\u6863\u8fdb\u884c\u589e\u5220\u6539\u67e5\u7b49\u64cd\u4f5c\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install python-docx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u521b\u5efa\u6587\u6863<\/strong>\uff1a\u4ece\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\u5f00\u59cb\uff0c\u719f\u6089\u5982\u4f55\u5411\u6587\u6863\u4e2d\u6dfb\u52a0\u5185\u5bb9\u662f\u57fa\u7840\u3002<\/li>\n<li><strong>\u7f16\u8f91\u5185\u5bb9<\/strong>\uff1a\u63a5\u7740\u5b66\u4e60\u5982\u4f55\u5bf9\u5df2\u6709\u6587\u6863\u4e2d\u7684\u5185\u5bb9\u8fdb\u884c\u7f16\u8f91\uff0c\u5305\u62ec\u6dfb\u52a0\u6216\u5220\u9664\u6587\u672c\u3001\u56fe\u7247\u548c\u8868\u683c\u7b49\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u7f16\u5199\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u540e\u7aef\u903b\u8f91\u975e\u5e38\u91cd\u8981\uff0c\u4f46\u597d\u7684\u7528\u6237\u4f53\u9a8c\u540c\u6837\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u6784\u5efaGUI\uff0c\u5982<code>Tkinter<\/code>\u3001<code>PyQt<\/code>\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u9009\u62e9\u6846\u67b6<\/strong>\uff1a\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u4e2a\u4eba\u559c\u597d\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684GUI\u6846\u67b6\u3002<code>Tkinter<\/code>\u662fPython\u81ea\u5e26\u7684\u6807\u51c6GUI\u5e93\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li><strong>\u8bbe\u8ba1\u5e03\u5c40<\/strong>\uff1a\u57fa\u4e8e\u9009\u5b9a\u7684\u6846\u67b6\u8bbe\u8ba1\u5408\u7406\u7684\u5e03\u5c40\u548c\u63a7\u4ef6\uff0c\u6bd4\u5982\u6309\u94ae\u3001\u6587\u672c\u6846\u3001\u83dc\u5355\u7b49\uff0c\u4f7f\u7528\u6237\u80fd\u591f\u65b9\u4fbf\u5730\u4e0e\u7a0b\u5e8f\u4ea4\u4e92\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5b9e\u73b0\u6587\u6863\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u6709\u4e86GUI\u4e4b\u540e\uff0c\u8fdb\u4e00\u6b65\u5c06\u540e\u7aef\u7684Word\u5904\u7406\u529f\u80fd\u6574\u5408\u5230\u524d\u7aef\u4e2d\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u5b9e\u9645\u7684\u6587\u6863\u64cd\u4f5c\u80fd\u529b\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6587\u4ef6\u64cd\u4f5c<\/strong>\uff1a\u5b9e\u73b0\u6253\u5f00\u3001\u4fdd\u5b58\u6587\u6863\u7b49\u57fa\u7840\u529f\u80fd\u3002\u8fd9\u9700\u8981GUI\u4e0e\u540e\u7aef\u903b\u8f91\u7d27\u5bc6\u534f\u4f5c\uff0c\u5b8c\u6210\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u7f16\u8f91\u529f\u80fd<\/strong>\uff1a\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u589e\u52a0\u6587\u5b57\u7f16\u8f91\u3001\u683c\u5f0f\u8bbe\u7f6e\u7b49\u9ad8\u7ea7\u529f\u80fd\u3002\u4e3a\u7528\u6237\u63d0\u4f9b\u4e30\u5bcc\u7684\u6587\u6863\u7f16\u8f91\u9009\u9879\uff0c\u5305\u62ec\u5b57\u4f53\u6837\u5f0f\u3001\u6bb5\u843d\u6392\u5217\u7b49\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u5c06\u80fd\u591f\u7528Python\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Word\u5904\u7406\u9875\u9762\u3002\u5c3d\u7ba1\u8fc7\u7a0b\u770b\u8d77\u6765\u590d\u6742\uff0c\u4f46\u6bcf\u4e2a\u6b65\u9aa4\u90fd\u662f\u4e3a\u4e86\u5efa\u7acb\u7a33\u56fa\u7684\u57fa\u7840\u548c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002\u5728\u719f\u7ec3\u8fd0\u7528\u5e93\u3001\u7406\u89e3Word\u6587\u4ef6\u7ed3\u6784\u548c\u7f16\u5199GUI\u7684\u57fa\u7840\u4e0a\uff0c\u4e0d\u65ad\u5b9e\u8df5\u548c\u6539\u8fdb\uff0c\u5c06\u4f7f\u60a8\u80fd\u591f\u521b\u5efa\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684Word\u5904\u7406\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u5904\u7406Word\u6587\u6863\uff1f<\/strong><\/p>\n<p>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u6765\u5904\u7406Word\u6587\u6863\uff0c\u5176\u4e2d\u6bd4\u8f83\u6d41\u884c\u7684\u6709python-docx\u548cpywin32\u3002\u8fd9\u4e24\u4e2a\u5e93\u90fd\u80fd\u5b9e\u73b0\u5bf9Word\u6587\u6863\u7684\u521b\u5efa\u3001\u4fee\u6539\u548c\u8bfb\u53d6\u64cd\u4f5c\u3002python-docx\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684API\uff0c\u9002\u5408\u7528\u4e8e\u5904\u7406\u7b80\u5355\u7684\u6587\u6863\u683c\u5f0f\uff1b\u800cpywin32\u662f\u4e00\u6b3e\u5f3a\u5927\u7684Windows API\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u5982\u64cd\u4f5cWord\u4e2d\u7684\u8868\u683c\u3001\u56fe\u8868\u7b49\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528python-docx\u5e93\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528python-docx\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\uff1a<\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u5bfc\u5165docx\u5e93\uff1a<code>import docx<\/code><\/li>\n<li>\u521b\u5efa\u4e00\u4e2aDocument\u5bf9\u8c61\uff1a<code>doc = docx.Document()<\/code><\/li>\n<li>\u4f7f\u7528add_paragraph()\u65b9\u6cd5\u6dfb\u52a0\u6bb5\u843d\uff1a<code>doc.add_paragraph(&quot;\u8fd9\u662f\u4e00\u4e2a\u6bb5\u843d&quot;)<\/code><\/li>\n<li>\u4f7f\u7528save()\u65b9\u6cd5\u4fdd\u5b58\u6587\u6863\uff1a<code>doc.save(&quot;new_document.docx&quot;)<\/code><\/li>\n<\/ul>\n<p>\u8fd9\u6837\u5c31\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e00\u4e2a\u6bb5\u843d\u7684\u65b0Word\u6587\u6863\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528pywin32\u5e93\u5b9e\u73b0\u5bf9Word\u6587\u6863\u4e2d\u7684\u8868\u683c\u8fdb\u884c\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>pywin32\u5e93\u63d0\u4f9b\u4e86\u5bf9Word\u6587\u6863\u7684\u66f4\u5e95\u5c42\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u8868\u683c\u7684\u63d2\u5165\u3001\u4fee\u6539\u548c\u5220\u9664\u7b49\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528pywin32\u5e93\u5728Word\u6587\u6863\u4e2d\u63d2\u5165\u4e00\u4e2a\u8868\u683c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code>import win32com.client as win32\n\n# \u521b\u5efaWord\u5b9e\u4f8b\nword = win32.Dispatch(&quot;Word.Application&quot;)\n# \u65b0\u5efa\u4e00\u4e2a\u6587\u6863\ndoc = word.Documents.Add()\n# \u5728\u6587\u6863\u4e2d\u63d2\u5165\u4e00\u4e2a\u8868\u683c\ntable = doc.Tables.Add(doc.Range(0, 0), 3, 3)\n# \u904d\u5386\u8868\u683c\u7684\u6bcf\u4e2a\u5355\u5143\u683c\uff0c\u5e76\u8bbe\u7f6e\u5185\u5bb9\nfor i in range(3):\n    for j in range(3):\n        cell = table.Cell(i+1, j+1)\n        cell.Range.Text = f&quot;\u7b2c{i+1}\u884c\u7b2c{j+1}\u5217&quot;\n# \u4fdd\u5b58\u6587\u6863\ndoc.SaveAs(&quot;table.docx&quot;)\n# \u5173\u95edWord\u5b9e\u4f8b\nword.Quit()\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b3\u884c3\u5217\u7684\u8868\u683c\uff0c\u5e76\u8bbe\u7f6e\u4e86\u6bcf\u4e2a\u5355\u5143\u683c\u7684\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Word\u5904\u7406\u9875\u9762\uff0c\u60a8\u4e3b\u8981\u9700\u8981\u638c\u63e1\u5982\u4e0b\u51e0\u4e2a\u6838\u5fc3\u65b9\u9762\uff1a\u5229\u7528Python\u7684\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c [&hellip;]","protected":false},"author":3,"featured_media":224356,"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\/224339"}],"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=224339"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/224339\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/224356"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=224339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=224339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=224339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}