{"id":1075185,"date":"2025-01-08T11:41:42","date_gmt":"2025-01-08T03:41:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1075185.html"},"modified":"2025-01-08T11:41:45","modified_gmt":"2025-01-08T03:41:45","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86word%e4%b8%ad%e8%a1%a8%e6%a0%bc%e6%95%b0%e6%8d%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1075185.html","title":{"rendered":"python\u5982\u4f55\u5c06word\u4e2d\u8868\u683c\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104628\/4deea874-66b1-49ab-aeb1-f9ac8c015d9a.webp\" alt=\"python\u5982\u4f55\u5c06word\u4e2d\u8868\u683c\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u5c06Word\u6587\u6863\u4e2d\u7684\u8868\u683c\u6570\u636e\u5bfc\u51fa\u5230Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>python-docx<\/code>\u5e93\u3001\u89e3\u6790\u8868\u683c\u5185\u5bb9\u3001\u5904\u7406\u6570\u636e\u3002<\/strong> \u5176\u4e2d\uff0c<code>python-docx<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8bfb\u53d6\u548c\u5199\u5165Word\u6587\u6863\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u5e93\u6765\u6253\u5f00Word\u6587\u6863\uff0c\u904d\u5386\u6240\u6709\u8868\u683c\uff0c\u5e76\u5c06\u8868\u683c\u4e2d\u7684\u6570\u636e\u63d0\u53d6\u51fa\u6765\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5e76\u5bfc\u5165\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528<code>python-docx<\/code>\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6253\u5f00Word\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u6253\u5f00\u5305\u542b\u8868\u683c\u7684Word\u6587\u6863\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6253\u5f00\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">doc = Document(&#39;path_to_your_word_document.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u904d\u5386\u6587\u6863\u4e2d\u7684\u8868\u683c<\/h3>\n<\/p>\n<p><p>\u6253\u5f00\u6587\u6863\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u6587\u6863\u4e2d\u7684\u6240\u6709\u8868\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tables = doc.tables<\/p>\n<p>for table in 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><p>\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u6253\u5370\u51fa\u6587\u6863\u4e2d\u6240\u6709\u8868\u683c\u7684\u5185\u5bb9\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u7ba1\u7406\u548c\u5904\u7406\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5728Python\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u4f8b\u5982\u5217\u8868\u6216\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5c06\u8868\u683c\u6570\u636e\u5b58\u50a8\u5728\u5217\u8868\u4e2d<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u8868\u683c\u6570\u636e\u5b58\u50a8\u5728\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tables_data = []<\/p>\n<p>for table in tables:<\/p>\n<p>    table_data = []<\/p>\n<p>    for row in table.rows:<\/p>\n<p>        row_data = []<\/p>\n<p>        for cell in row.cells:<\/p>\n<p>            row_data.append(cell.text)<\/p>\n<p>        table_data.append(row_data)<\/p>\n<p>    tables_data.append(table_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u8868\u683c\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5c06\u6570\u636e\u63d0\u53d6\u5230\u5217\u8868\u540e\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u6211\u4eec\u60f3\u628a\u6570\u636e\u5bfc\u51fa\u5230CSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>csv<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    for table in tables_data:<\/p>\n<p>        for row in table:<\/p>\n<p>            writer.writerow(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c06Word\u6587\u6863\u4e2d\u7684\u8868\u683c\u6570\u636e\u63d0\u53d6\u5e76\u5bfc\u51fa\u5230CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>from docx import Document<\/p>\n<h2><strong>\u6253\u5f00Word\u6587\u6863<\/strong><\/h2>\n<p>doc = Document(&#39;path_to_your_word_document.docx&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u8868\u683c\u6570\u636e<\/strong><\/h2>\n<p>tables_data = []<\/p>\n<p>for table in doc.tables:<\/p>\n<p>    table_data = []<\/p>\n<p>    for row in table.rows:<\/p>\n<p>        row_data = []<\/p>\n<p>        for cell in row.cells:<\/p>\n<p>            row_data.append(cell.text)<\/p>\n<p>        table_data.append(row_data)<\/p>\n<p>    tables_data.append(table_data)<\/p>\n<h2><strong>\u5bfc\u51fa\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    for table in tables_data:<\/p>\n<p>        for row in table:<\/p>\n<p>            writer.writerow(row)<\/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\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cWord\u6587\u6863\u4e2d\u7684\u8868\u683c\u53ef\u80fd\u4f1a\u66f4\u52a0\u590d\u6742\uff0c\u6bd4\u5982\u5305\u542b\u5408\u5e76\u5355\u5143\u683c\u3001\u5d4c\u5957\u8868\u683c\u7b49\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u66f4\u590d\u6742\u7684\u5904\u7406\u903b\u8f91\u6765\u6b63\u786e\u89e3\u6790\u8868\u683c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u5408\u5e76\u5355\u5143\u683c<\/h4>\n<\/p>\n<p><p>\u5408\u5e76\u5355\u5143\u683c\u9700\u8981\u7279\u522b\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u6b63\u786e\u89e3\u6790\u6570\u636e\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u5355\u5143\u683c\u7684<code>_element<\/code>\u5c5e\u6027\u6765\u68c0\u6d4b\u5355\u5143\u683c\u662f\u5426\u662f\u5408\u5e76\u5355\u5143\u683c\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>            if cell._element.get(&#39;w:vMerge&#39;) is not None:<\/p>\n<p>                # \u5904\u7406\u5408\u5e76\u5355\u5143\u683c\u903b\u8f91<\/p>\n<p>                pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u5d4c\u5957\u8868\u683c<\/h4>\n<\/p>\n<p><p>\u5d4c\u5957\u8868\u683c\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u65b9\u6cd5\u6765\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def extract_table_data(table):<\/p>\n<p>    table_data = []<\/p>\n<p>    for row in table.rows:<\/p>\n<p>        row_data = []<\/p>\n<p>        for cell in row.cells:<\/p>\n<p>            if len(cell.tables) &gt; 0:<\/p>\n<p>                # \u9012\u5f52\u5904\u7406\u5d4c\u5957\u8868\u683c<\/p>\n<p>                nested_table_data = [extract_table_data(nested_table) for nested_table in cell.tables]<\/p>\n<p>                row_data.append(nested_table_data)<\/p>\n<p>            else:<\/p>\n<p>                row_data.append(cell.text)<\/p>\n<p>        table_data.append(row_data)<\/p>\n<p>    return table_data<\/p>\n<p>tables_data = [extract_table_data(table) for table in doc.tables]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u6b63\u786e\u5904\u7406\u5d4c\u5957\u8868\u683c\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>python-docx<\/code>\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06Word\u6587\u6863\u4e2d\u7684\u8868\u683c\u6570\u636e\u63d0\u53d6\u5230Python\u4e2d\u8fdb\u884c\u5904\u7406\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u8868\u683c\u8fd8\u662f\u590d\u6742\u7684\u5408\u5e76\u5355\u5143\u683c\u548c\u5d4c\u5957\u8868\u683c\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u9002\u5f53\u7684\u5904\u7406\u903b\u8f91\u6765\u6b63\u786e\u89e3\u6790\u6570\u636e\u3002\u5e0c\u671b\u672c\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u6765\u5904\u7406Word\u6587\u6863\u4e2d\u7684\u8868\u683c\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6Word\u6587\u6863\u4e2d\u7684\u8868\u683c\u6570\u636e\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8bfb\u53d6Word\u6587\u6863\u4e2d\u7684\u8868\u683c\u6570\u636e\uff0c\u53ef\u4ee5\u5229\u7528<code>python-docx<\/code>\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u8be5\u5e93\u3002\u63a5\u7740\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a  <\/p>\n<ol>\n<li>\u4f7f\u7528<code>Document<\/code>\u7c7b\u6253\u5f00Word\u6587\u6863\u3002  <\/li>\n<li>\u8bbf\u95ee\u6587\u6863\u4e2d\u7684\u8868\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tables<\/code>\u5c5e\u6027\u83b7\u53d6\u6240\u6709\u8868\u683c\u3002  <\/li>\n<li>\u904d\u5386\u8868\u683c\u4e2d\u7684\u884c\u548c\u5355\u5143\u683c\uff0c\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u5e76\u5b58\u50a8\u5728\u5217\u8868\u6216\u5b57\u5178\u4e2d\u3002<\/li>\n<\/ol>\n<p><strong>\u6709\u4ec0\u4e48\u5e93\u53ef\u4ee5\u5e2e\u52a9\u63d0\u53d6Word\u8868\u683c\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c<code>python-docx<\/code>\u662f\u6700\u5e38\u7528\u7684\u5e93\u4e4b\u4e00\uff0c\u4e13\u95e8\u7528\u4e8e\u64cd\u4f5cWord\u6587\u6863\u3002\u9664\u4e86\u5b83\uff0c\u8fd8\u6709\u5176\u4ed6\u5e93\u5982<code>pandas<\/code>\u7ed3\u5408<code>python-docx<\/code>\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u4f7f\u7528<code>pandas<\/code>\u53ef\u4ee5\u8f7b\u677e\u5c06\u8868\u683c\u8f6c\u6362\u4e3aDataFrame\uff0c\u4fbf\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<p><strong>\u63d0\u53d6\u8868\u683c\u6570\u636e\u540e\uff0c\u5982\u4f55\u5c06\u5176\u4fdd\u5b58\u4e3a\u5176\u4ed6\u683c\u5f0f\uff1f<\/strong><br \/>\u63d0\u53d6\u5230\u7684\u8868\u683c\u6570\u636e\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u4fdd\u5b58\u4e3a\u5176\u4ed6\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u53ef\u4ee5\u5c06DataFrame\u4fdd\u5b58\u4e3aCSV\u6216Excel\u683c\u5f0f\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\ndata = {&#39;Column1&#39;: [...], &#39;Column2&#39;: [...]}\ndf = pd.DataFrame(data)\ndf.to_csv(&#39;output.csv&#39;, index=False)  # \u4fdd\u5b58\u4e3aCSV\ndf.to_excel(&#39;output.xlsx&#39;, index=False)  # \u4fdd\u5b58\u4e3aExcel\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u6570\u636e\u7684\u540e\u7eed\u5904\u7406\u53d8\u5f97\u66f4\u52a0\u7075\u6d3b\u4e0e\u4fbf\u6377\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06Word\u6587\u6863\u4e2d\u7684\u8868\u683c\u6570\u636e\u5bfc\u51fa\u5230Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528python-docx\u5e93\u3001\u89e3\u6790\u8868\u683c\u5185\u5bb9\u3001\u5904\u7406\u6570\u636e\u3002 \u5176 [&hellip;]","protected":false},"author":3,"featured_media":1075193,"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\/1075185"}],"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=1075185"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075185\/revisions"}],"predecessor-version":[{"id":1075196,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075185\/revisions\/1075196"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1075193"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1075185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1075185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1075185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}