{"id":944204,"date":"2024-12-26T23:01:56","date_gmt":"2024-12-26T15:01:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/944204.html"},"modified":"2024-12-26T23:01:57","modified_gmt":"2024-12-26T15:01:57","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96word","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/944204.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6word"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081307\/b4e4e447-9421-4a33-b1ca-9d035eafbc5c.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6word\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6Word\u6587\u6863\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>python-docx<\/code>\u5e93\u3001<code>pywin32<\/code>\u5e93\u3001\u4ee5\u53ca<code>pandas<\/code>\u7ed3\u5408\u5176\u4ed6\u5de5\u5177\u8fdb\u884c\u8bfb\u53d6\u3002<\/strong>\u5176\u4e2d\uff0c<code>python-docx<\/code>\u5e93\u662f\u4e13\u95e8\u7528\u4e8e\u5904\u7406Word\u6587\u6863\u7684\u5e93\uff0c\u9002\u5408\u8bfb\u53d6\u548c\u521b\u5efa\u8f83\u4e3a\u7b80\u5355\u7684.docx\u6587\u4ef6\uff1b<code>pywin32<\/code>\u5e93\u901a\u8fc7\u8c03\u7528Windows\u7684COM\u63a5\u53e3\u6765\u64cd\u4f5cWord\uff0c\u529f\u80fd\u5f3a\u5927\u4f46\u4ec5\u9650\u4e8eWindows\u7cfb\u7edf\uff1b\u800c<code>pandas<\/code>\u7ed3\u5408\u5176\u4ed6\u5de5\u5177\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u8868\u683c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>python-docx<\/code>\u5e93\u6765\u8bfb\u53d6Word\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>python-docx<\/code>\u5e93\u8bfb\u53d6Word\u6587\u6863<\/h3>\n<\/p>\n<p><p><code>python-docx<\/code>\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u5904\u7406Word\u6587\u6863\u7684Python\u5e93\uff0c\u652f\u6301\u521b\u5efa\u3001\u8bfb\u53d6\u548c\u4fee\u6539.docx\u6587\u4ef6\u3002\u5b83\u975e\u5e38\u9002\u5408\u5904\u7406\u4e0d\u5305\u542b\u590d\u6742\u683c\u5f0f\u7684Word\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5<code>python-docx<\/code>\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>python-docx<\/code>\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u901a\u8fc7pip\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><h4>2. \u8bfb\u53d6Word\u6587\u6863\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>python-docx<\/code>\u8bfb\u53d6Word\u6587\u6863\u7684\u57fa\u672c\u6d41\u7a0b\u662f\uff1a\u6253\u5f00\u6587\u6863\u3001\u904d\u5386\u6bb5\u843d\u6216\u8868\u683c\u3001\u63d0\u53d6\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx import Document<\/p>\n<h2><strong>\u6253\u5f00Word\u6587\u6863<\/strong><\/h2>\n<p>doc = Document(&#39;example.docx&#39;)<\/p>\n<h2><strong>\u904d\u5386\u6587\u6863\u4e2d\u7684\u6bb5\u843d<\/strong><\/h2>\n<p>for para in doc.paragraphs:<\/p>\n<p>    print(para.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165<code>Document<\/code>\u7c7b\uff0c\u7136\u540e\u4f7f\u7528\u5b83\u6253\u5f00\u4e00\u4e2aWord\u6587\u6863\u3002\u63a5\u7740\uff0c\u6211\u4eec\u901a\u8fc7\u904d\u5386<code>doc.paragraphs<\/code>\u6765\u8bbf\u95ee\u6bcf\u4e2a\u6bb5\u843d\uff0c\u5e76\u8f93\u51fa\u5176\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>3. \u8bfb\u53d6\u8868\u683c\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u5982\u679cWord\u6587\u6863\u4e2d\u5305\u542b\u8868\u683c\uff0c<code>python-docx<\/code>\u4e5f\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u6587\u6863\u4e2d\u7684\u8868\u683c<\/p>\n<p>for table in doc.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>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u904d\u5386\u6587\u6863\u4e2d\u7684\u6240\u6709\u8868\u683c\uff0c\u7136\u540e\u9010\u884c\u3001\u9010\u5355\u5143\u683c\u5730\u8bfb\u53d6\u8868\u683c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>pywin32<\/code>\u5e93\u8bfb\u53d6Word\u6587\u6863<\/h3>\n<\/p>\n<p><p><code>pywin32<\/code>\u5e93\u901a\u8fc7Windows COM\u63a5\u53e3\u4e0eMicrosoft Word\u8fdb\u884c\u4ea4\u4e92\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6267\u884c\u590d\u6742\u64cd\u4f5c\u7684\u573a\u666f\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u53ea\u80fd\u5728Windows\u5e73\u53f0\u4e0a\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5<code>pywin32<\/code>\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528<code>pywin32<\/code>\u8bfb\u53d6Word\u6587\u6863<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import win32com.client<\/p>\n<h2><strong>\u521b\u5efaWord\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61<\/strong><\/h2>\n<p>word = win32com.client.Dispatch(&quot;Word.Application&quot;)<\/p>\n<h2><strong>\u6253\u5f00Word\u6587\u6863<\/strong><\/h2>\n<p>doc = word.Documents.Open(&#39;example.docx&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u6863\u4e2d\u7684\u6587\u672c<\/strong><\/h2>\n<p>text = doc.Content.Text<\/p>\n<p>print(text)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u6863<\/strong><\/h2>\n<p>doc.Close()<\/p>\n<h2><strong>\u9000\u51faWord\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>word.Quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2aWord\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u5b83\u6253\u5f00\u4e00\u4e2aWord\u6587\u6863\u3002\u901a\u8fc7\u8bbf\u95ee<code>doc.Content.Text<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u6587\u6863\u7684\u6240\u6709\u6587\u672c\u5185\u5bb9\u3002\u6700\u540e\uff0c\u5173\u95ed\u6587\u6863\u5e76\u9000\u51faWord\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>pandas<\/code>\u7ed3\u5408\u5176\u4ed6\u5de5\u5177\u8bfb\u53d6Word\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cWord\u6587\u6863\u53ef\u80fd\u5305\u542b\u590d\u6742\u7684\u8868\u683c\u6570\u636e\uff0c\u8fd9\u65f6\u53ef\u4ee5\u7ed3\u5408<code>pandas<\/code>\u5e93\u548c\u5176\u4ed6\u5de5\u5177\uff08\u4f8b\u5982<code>tabula-py<\/code>\u6216<code>camelot-py<\/code>\uff09\u6765\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1. \u7ed3\u5408<code>pandas<\/code>\u8bfb\u53d6\u8868\u683c<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5b89\u88c5\u6240\u9700\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas tabula-py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6Word\u6587\u6863\u4e2d\u7684\u8868\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import tabula<\/p>\n<h2><strong>\u4f7f\u7528tabula\u8bfb\u53d6PDF\u4e2d\u7684\u8868\u683c<\/strong><\/h2>\n<p>tables = tabula.read_pdf(&#39;example.pdf&#39;, pages=&#39;all&#39;, multiple_tables=True)<\/p>\n<h2><strong>\u5c06\u8868\u683c\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>for table in tables:<\/p>\n<p>    df = pd.DataFrame(table)<\/p>\n<p>    print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136<code>tabula-py<\/code>\u4e3b\u8981\u7528\u4e8e\u8bfb\u53d6PDF\u6587\u4ef6\uff0c\u4f46\u7ed3\u5408<code>pandas<\/code>\u5e93\uff0c\u5b83\u4e5f\u53ef\u4ee5\u5904\u7406\u4eceWord\u8f6c\u6362\u6210PDF\u683c\u5f0f\u7684\u590d\u6742\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u8bfb\u53d6Word\u6587\u6863\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u975e\u5e38\u91cd\u8981\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6587\u672c\u548c\u8868\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>python-docx<\/code>\u5e93\uff1b\u5bf9\u4e8e\u9700\u8981\u590d\u6742\u64cd\u4f5c\u6216Windows\u7279\u5b9a\u7684\u529f\u80fd\uff0c<code>pywin32<\/code>\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u800c\u5bf9\u4e8e\u590d\u6742\u7684\u8868\u683c\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u5c06Word\u6587\u6863\u8f6c\u6362\u4e3aPDF\u683c\u5f0f\uff0c\u7136\u540e\u4f7f\u7528<code>pandas<\/code>\u7ed3\u5408\u5176\u4ed6\u5de5\u5177\u8fdb\u884c\u5904\u7406\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u8c03\u6574\uff0c\u4ee5\u4fbf\u66f4\u6709\u6548\u5730\u5904\u7406Word\u6587\u6863\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6Word\u6587\u6863\u4e2d\u7684\u6587\u672c\u5185\u5bb9\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u8bfb\u53d6Word\u6587\u6863\uff0c\u53ef\u4ee5\u5229\u7528<code>python-docx<\/code>\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u6b64\u5e93\uff0c\u4f7f\u7528\u547d\u4ee4<code>pip install python-docx<\/code>\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8bfb\u53d6\u6587\u672c\u5185\u5bb9\uff1a  <\/p>\n<pre><code class=\"language-python\">from docx import Document\n\ndoc = Document(&#39;your_file.docx&#39;)\nfor para in doc.paragraphs:\n    print(para.text)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u9010\u6bb5\u6253\u5370\u6587\u6863\u4e2d\u7684\u6240\u6709\u6587\u672c\u3002<\/p>\n<p><strong>\u5728\u8bfb\u53d6Word\u6587\u6863\u65f6\uff0c\u5982\u4f55\u63d0\u53d6\u7279\u5b9a\u683c\u5f0f\u7684\u6587\u672c\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7<code>python-docx<\/code>\u5e93\u63d0\u53d6\u7279\u5b9a\u683c\u5f0f\u7684\u6587\u672c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u904d\u5386\u6587\u6863\u4e2d\u7684\u8868\u683c\u548c\u6bb5\u843d\uff0c\u68c0\u67e5\u5176\u6837\u5f0f\u5c5e\u6027\u3002\u4ee5\u4e0b\u662f\u63d0\u53d6\u7c97\u4f53\u6587\u672c\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">for para in doc.paragraphs:\n    for run in para.runs:\n        if run.bold:\n            print(run.text)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u4ec5\u6253\u5370\u6240\u6709\u7c97\u4f53\u6587\u672c\u3002<\/p>\n<p><strong>Python\u8bfb\u53d6Word\u6587\u6863\u65f6\uff0c\u5982\u4f55\u5904\u7406\u56fe\u7247\u548c\u5176\u4ed6\u975e\u6587\u672c\u5185\u5bb9\uff1f<\/strong><br \/>\u4f7f\u7528<code>python-docx<\/code>\u5e93\uff0c\u60a8\u53ef\u4ee5\u63d0\u53d6Word\u6587\u6863\u4e2d\u7684\u56fe\u7247\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8bbf\u95ee\u56fe\u50cf\uff1a  <\/p>\n<pre><code class=\"language-python\">for rel in doc.part.rels.values():\n    if &quot;image&quot; in rel.reltype:\n        img = rel.target_part\n        with open(img.partname[1:], &#39;wb&#39;) as f:\n            f.write(img.blob)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4ece\u6587\u6863\u4e2d\u63d0\u53d6\u5e76\u4fdd\u5b58\u6240\u6709\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6Word\u6587\u6863\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528python-docx\u5e93\u3001pywin32\u5e93\u3001\u4ee5\u53cap [&hellip;]","protected":false},"author":3,"featured_media":944205,"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\/944204"}],"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=944204"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/944204\/revisions"}],"predecessor-version":[{"id":944207,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/944204\/revisions\/944207"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/944205"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=944204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=944204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=944204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}