{"id":967619,"date":"2024-12-27T04:58:53","date_gmt":"2024-12-26T20:58:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/967619.html"},"modified":"2024-12-27T04:58:55","modified_gmt":"2024-12-26T20:58:55","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%86%99word%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/967619.html","title":{"rendered":"Python\u5982\u4f55\u8bfb\u5199word\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182819\/354a48c5-e5e5-47c6-88fe-a84cd40c731b.webp\" alt=\"Python\u5982\u4f55\u8bfb\u5199word\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u5199Word\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528<code>python-docx<\/code>\u5e93\u3001<code>pywin32<\/code>\u5e93\u3001<code>pypandoc<\/code>\u5e93\u7b49\u3002\u5176\u4e2d\uff0c<code>python-docx<\/code>\u5e93\u662f\u6700\u5e38\u7528\u3001\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4e13\u6ce8\u4e8e\u5904\u7406Word\u6587\u4ef6\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\u63a5\u53e3\u3001\u6613\u4e8e\u5b89\u88c5\u548c\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>python-docx<\/code>\u5e93<\/h3>\n<\/p>\n<p><p><code>python-docx<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u548c\u66f4\u65b0Microsoft Word\uff08.docx\uff09\u6587\u4ef6\u7684Python\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u8bfb\u53d6\u548c\u5199\u5165Word\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u5173\u4e8e\u5982\u4f55\u4f7f\u7528\u8be5\u5e93\u7684\u8be6\u7ec6\u8bf4\u660e\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>python-docx<\/code>\u5e93\u3002\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\u53ef\u4ee5\u5f00\u59cb\u8bfb\u53d6\u548c\u5199\u5165Word\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u57fa\u672c\u64cd\u4f5c\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><p><strong>\u8bfb\u53d6Word\u6587\u4ef6\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx import Document<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2aWord\u6587\u6863<\/strong><\/h2>\n<p>doc = Document(&#39;example.docx&#39;)<\/p>\n<h2><strong>\u904d\u5386\u6587\u6863\u4e2d\u7684\u6bcf\u4e00\u6bb5<\/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><strong>\u5199\u5165Word\u6587\u4ef6\uff1a<\/strong><\/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\u4e2a\u6bb5\u843d<\/strong><\/h2>\n<p>doc.add_paragraph(&quot;\u8fd9\u662f\u4e00\u4e2a\u65b0\u7684\u6bb5\u843d\u3002&quot;)<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u6863<\/strong><\/h2>\n<p>doc.save(&#39;new_document.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8be6\u7ec6\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p><code>python-docx<\/code>\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u8bfb\u53d6\u548c\u5199\u5165\u6587\u672c\uff0c\u8fd8\u53ef\u4ee5\u64cd\u4f5c\u8868\u683c\u3001\u56fe\u7247\u3001\u6837\u5f0f\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8be6\u7ec6\u7684\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p><strong>\u64cd\u4f5c\u6bb5\u843d\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u6bb5\u843d\u662fWord\u6587\u6863\u4e2d\u7684\u57fa\u672c\u6587\u672c\u5757\uff0c\u53ef\u4ee5\u901a\u8fc7<code>add_paragraph<\/code>\u65b9\u6cd5\u6dfb\u52a0\u6bb5\u843d\uff0c\u901a\u8fc7<code>paragraphs<\/code>\u5c5e\u6027\u904d\u5386\u6587\u6863\u4e2d\u7684\u6bb5\u843d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u5e26\u6709\u6837\u5f0f\u7684\u6bb5\u843d<\/p>\n<p>doc.add_paragraph(&#39;\u8fd9\u662f\u4e00\u4e2a\u6807\u9898\u6bb5\u843d&#39;, style=&#39;Heading1&#39;)<\/p>\n<h2><strong>\u4fee\u6539\u6bb5\u843d\u6837\u5f0f<\/strong><\/h2>\n<p>para = doc.add_paragraph(&#39;\u8fd9\u662f\u4e00\u4e2a\u666e\u901a\u6bb5\u843d&#39;)<\/p>\n<p>para.style = &#39;BodyText&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u64cd\u4f5c\u8868\u683c\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>add_table<\/code>\u65b9\u6cd5\u521b\u5efa\u8868\u683c\uff0c\u5e76\u8bbe\u7f6e\u8868\u683c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u4e00\u4e2a2x2\u7684\u8868\u683c<\/p>\n<p>table = doc.add_table(rows=2, cols=2)<\/p>\n<h2><strong>\u586b\u5145\u8868\u683c<\/strong><\/h2>\n<p>table.cell(0, 0).text = &#39;\u7b2c\u4e00\u884c\uff0c\u7b2c\u4e00\u5217&#39;<\/p>\n<p>table.cell(0, 1).text = &#39;\u7b2c\u4e00\u884c\uff0c\u7b2c\u4e8c\u5217&#39;<\/p>\n<p>table.cell(1, 0).text = &#39;\u7b2c\u4e8c\u884c\uff0c\u7b2c\u4e00\u5217&#39;<\/p>\n<p>table.cell(1, 1).text = &#39;\u7b2c\u4e8c\u884c\uff0c\u7b2c\u4e8c\u5217&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u63d2\u5165\u56fe\u7247\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>add_picture<\/code>\u65b9\u6cd5\u63d2\u5165\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d2\u5165\u56fe\u7247<\/p>\n<p>doc.add_picture(&#39;image.png&#39;, width=Inches(1.25))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f18\u52bf\u4e0e\u5c40\u9650<\/h4>\n<\/p>\n<p><p><code>python-docx<\/code>\u7684\u4e3b\u8981\u4f18\u52bf\u5728\u4e8e\u5176\u7b80\u5355\u6027\u548c\u4e13\u6ce8\u6027\uff0c\u9002\u5408\u5904\u7406\u4ee5\u6587\u672c\u4e3a\u4e3b\u7684Word\u6587\u6863\u3002\u7136\u800c\uff0c\u5b83\u4e5f\u6709\u4e00\u4e9b\u5c40\u9650\u6027\uff0c\u6bd4\u5982\u4e0d\u652f\u6301\u5b8f\u3001\u590d\u6742\u7684\u8868\u683c\u6837\u5f0f\u3001\u4ee5\u53ca\u5bf9Word\u6587\u6863\u683c\u5f0f\u7684\u6df1\u5ea6\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>pywin32<\/code>\u5e93<\/h3>\n<\/p>\n<p><p><code>pywin32<\/code>\u5e93\u63d0\u4f9b\u4e86\u5bf9Windows COM\u63a5\u53e3\u7684\u8bbf\u95ee\uff0c\u53ef\u4ee5\u7528\u6765\u64cd\u4f5cWord\u3001Excel\u7b49Microsoft Office\u5e94\u7528\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528<code>pywin32<\/code>\u5e93\u8bfb\u53d6\u548c\u5199\u5165Word\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>pywin32<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8bfb\u53d6Word\u6587\u4ef6\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import win32com.client<\/p>\n<h2><strong>\u6253\u5f00Word\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>word = win32com.client.Dispatch(&quot;Word.Application&quot;)<\/p>\n<h2><strong>\u6253\u5f00\u6587\u6863<\/strong><\/h2>\n<p>doc = word.Documents.Open(&#39;example.docx&#39;)<\/p>\n<h2><strong>\u904d\u5386\u6587\u6863\u4e2d\u7684\u6bcf\u4e00\u6bb5<\/strong><\/h2>\n<p>for para in doc.Paragraphs:<\/p>\n<p>    print(para.Range.Text)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u6863<\/strong><\/h2>\n<p>doc.Close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5199\u5165Word\u6587\u4ef6\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import win32com.client<\/p>\n<h2><strong>\u6253\u5f00Word\u5e94\u7528\u7a0b\u5e8f<\/strong><\/h2>\n<p>word = win32com.client.Dispatch(&quot;Word.Application&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u6863<\/strong><\/h2>\n<p>doc = word.Documents.Add()<\/p>\n<h2><strong>\u6dfb\u52a0\u6bb5\u843d<\/strong><\/h2>\n<p>doc.Content.Text = &quot;\u8fd9\u662f\u4f7f\u7528pywin32\u5e93\u521b\u5efa\u7684\u6587\u6863\u3002&quot;<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u6863<\/strong><\/h2>\n<p>doc.SaveAs(&#39;new_document.docx&#39;)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u6863<\/strong><\/h2>\n<p>doc.Close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8be6\u7ec6\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p><code>pywin32<\/code>\u5e93\u63d0\u4f9b\u4e86\u5bf9Word\u6587\u6863\u7684\u5168\u9762\u63a7\u5236\uff0c\u53ef\u4ee5\u64cd\u4f5c\u51e0\u4e4e\u6240\u6709\u7684\u6587\u6863\u5c5e\u6027\u548c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p><strong>\u64cd\u4f5c\u8868\u683c\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>Tables<\/code>\u5c5e\u6027\u8bbf\u95ee\u548c\u64cd\u4f5c\u6587\u6863\u4e2d\u7684\u8868\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u8868\u683c<\/p>\n<p>table = doc.Tables.Add(doc.Paragraphs(1).Range, 2, 2)<\/p>\n<h2><strong>\u586b\u5145\u8868\u683c<\/strong><\/h2>\n<p>table.Cell(1, 1).Range.Text = &#39;\u7b2c\u4e00\u884c\uff0c\u7b2c\u4e00\u5217&#39;<\/p>\n<p>table.Cell(1, 2).Range.Text = &#39;\u7b2c\u4e00\u884c\uff0c\u7b2c\u4e8c\u5217&#39;<\/p>\n<p>table.Cell(2, 1).Range.Text = &#39;\u7b2c\u4e8c\u884c\uff0c\u7b2c\u4e00\u5217&#39;<\/p>\n<p>table.Cell(2, 2).Range.Text = &#39;\u7b2c\u4e8c\u884c\uff0c\u7b2c\u4e8c\u5217&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f18\u52bf\u4e0e\u5c40\u9650<\/h4>\n<\/p>\n<p><p><code>pywin32<\/code>\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5168\u9762\u63a7\u5236Word\u6587\u6863\u7684\u5404\u4e2a\u65b9\u9762\u3002\u7136\u800c\uff0c\u5b83\u4ec5\u9650\u4e8eWindows\u5e73\u53f0\uff0c\u5e76\u4e14\u9700\u8981\u5b89\u88c5Microsoft Office\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>pypandoc<\/code>\u5e93<\/h3>\n<\/p>\n<p><p><code>pypandoc<\/code>\u662f\u4e00\u4e2a\u57fa\u4e8ePandoc\u7684Python\u5e93\uff0c\u53ef\u4ee5\u5c06\u6587\u4ef6\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u683c\u5f0f\uff0c\u5305\u62ecWord\u3002\u9002\u5408\u9700\u8981\u8fdb\u884c\u591a\u79cd\u683c\u5f0f\u8f6c\u6362\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>pypandoc<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pypandoc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f6c\u6362\u6587\u4ef6\u683c\u5f0f\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pypandoc<\/p>\n<h2><strong>\u5c06Markdown\u6587\u4ef6\u8f6c\u6362\u4e3aWord<\/strong><\/h2>\n<p>output = pypandoc.convert_file(&#39;example.md&#39;, &#39;docx&#39;, outputfile=&#39;example.docx&#39;)<\/p>\n<p>assert output == &quot;&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f18\u52bf\u4e0e\u5c40\u9650<\/h4>\n<\/p>\n<p><p><code>pypandoc<\/code>\u7684\u4e3b\u8981\u4f18\u52bf\u5728\u4e8e\u5176\u5f3a\u5927\u7684\u683c\u5f0f\u8f6c\u6362\u529f\u80fd\uff0c\u53ef\u4ee5\u5728\u591a\u79cd\u6587\u6863\u683c\u5f0f\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002\u7136\u800c\uff0c\u5b83\u4f9d\u8d56\u4e8ePandoc\uff0c\u9700\u8981\u989d\u5916\u5b89\u88c5Pandoc\uff0c\u5e76\u4e14\u4e0d\u9002\u5408\u9700\u8981\u590d\u6742Word\u6587\u6863\u64cd\u4f5c\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8bfb\u5199Word\u6587\u4ef6\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3002<code>python-docx<\/code>\u5e93\u9002\u5408\u5904\u7406\u7b80\u5355\u7684Word\u6587\u6863\u64cd\u4f5c\uff0c<code>pywin32<\/code>\u5e93\u9002\u5408\u9700\u8981\u5168\u9762\u63a7\u5236Word\u6587\u6863\u7684\u573a\u5408\uff0c\u800c<code>pypandoc<\/code>\u5e93\u9002\u5408\u9700\u8981\u591a\u683c\u5f0f\u8f6c\u6362\u7684\u573a\u5408\u3002\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6Word\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8bfb\u53d6Word\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7<code>python-docx<\/code>\u5e93\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u8be5\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip install python-docx<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u60a8\u53ef\u4ee5\u6253\u5f00\u6587\u6863\u5e76\u904d\u5386\u5176\u4e2d\u7684\u6bb5\u843d\uff0c\u63d0\u53d6\u6587\u672c\u5185\u5bb9\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from docx import Document\n\ndoc = Document(&#39;your_file.docx&#39;)\nfor paragraph in doc.paragraphs:\n    print(paragraph.text)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u8bfb\u53d6\u7b80\u5355\u7684\u6587\u672c\u5185\u5bb9\uff0c\u590d\u6742\u683c\u5f0f\u7684\u5185\u5bb9\u53ef\u80fd\u9700\u8981\u66f4\u8be6\u7ec6\u7684\u5904\u7406\u3002<\/p>\n<p><strong>Python\u53ef\u4ee5\u5199\u5165Word\u6587\u4ef6\u5417\uff1f\u5982\u679c\u53ef\u4ee5\uff0c\u5982\u4f55\u5b9e\u73b0\uff1f<\/strong><br \/>Python\u4e0d\u4ec5\u53ef\u4ee5\u8bfb\u53d6Word\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u521b\u5efa\u548c\u5199\u5165\u65b0\u7684Word\u6587\u4ef6\u3002\u4f7f\u7528<code>python-docx<\/code>\u5e93\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u6863\u5e76\u5411\u5176\u4e2d\u6dfb\u52a0\u5185\u5bb9\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from docx import Document\n\ndoc = Document()\ndoc.add_heading(&#39;Document Title&#39;, level=1)\ndoc.add_paragraph(&#39;This is a paragraph in the Word document.&#39;)\ndoc.save(&#39;new_file.docx&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u60a8\u81ea\u5b9a\u4e49\u6587\u6863\u7684\u6807\u9898\u3001\u6bb5\u843d\u3001\u6837\u5f0f\u7b49\uff0c\u9002\u5408\u9700\u8981\u751f\u6210\u62a5\u544a\u6216\u6587\u6863\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u5305\u542b\u8868\u683c\u7684Word\u6587\u4ef6\uff1f<\/strong><br \/>\u5904\u7406\u5305\u542b\u8868\u683c\u7684Word\u6587\u4ef6\u540c\u6837\u53ef\u4ee5\u4f7f\u7528<code>python-docx<\/code>\u5e93\u3002\u8bfb\u53d6\u8868\u683c\u7684\u8fc7\u7a0b\u7c7b\u4f3c\u4e8e\u8bfb\u53d6\u6bb5\u843d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6587\u6863\u5bf9\u8c61\u7684<code>tables<\/code>\u5c5e\u6027\u6765\u8bbf\u95ee\u8868\u683c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from docx import Document\n\ndoc = Document(&#39;your_file_with_table.docx&#39;)\nfor table in doc.tables:\n    for row in table.rows:\n        for cell in row.cells:\n            print(cell.text)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u60a8\u80fd\u591f\u63d0\u53d6\u548c\u5904\u7406Word\u6587\u6863\u4e2d\u7684\u7ed3\u6784\u5316\u6570\u636e\uff0c\u9002\u5408\u9700\u8981\u5206\u6790\u6216\u4fee\u6539\u8868\u683c\u5185\u5bb9\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u5199Word\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528python-docx\u5e93\u3001pywin32\u5e93\u3001pypan [&hellip;]","protected":false},"author":3,"featured_media":967622,"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\/967619"}],"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=967619"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/967619\/revisions"}],"predecessor-version":[{"id":967625,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/967619\/revisions\/967625"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/967622"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=967619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=967619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=967619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}