{"id":943072,"date":"2024-12-26T22:28:19","date_gmt":"2024-12-26T14:28:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/943072.html"},"modified":"2024-12-26T22:28:22","modified_gmt":"2024-12-26T14:28:22","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8docx","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/943072.html","title":{"rendered":"python\u5982\u4f55\u7528docx"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080623\/05ddcbfc-6cb2-4e4a-8d42-416395399a84.webp\" alt=\"python\u5982\u4f55\u7528docx\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4f7f\u7528docx\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u3001\u7f16\u8f91\u548c\u8bfb\u53d6Word\u6587\u6863\u3002\u8981\u4f7f\u7528docx\u6a21\u5757\uff0c\u901a\u5e38\u9700\u8981\u5b89\u88c5<code>python-docx<\/code>\u5e93\u3001\u5bfc\u5165\u5e93\u5e76\u521b\u5efa\u6216\u6253\u5f00\u6587\u6863\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u65b9\u6cd5\u5411\u6587\u6863\u4e2d\u6dfb\u52a0\u6587\u672c\u3001\u6bb5\u843d\u548c\u8868\u683c\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a\u5b89\u88c5\u5e93\u3001\u521b\u5efa\u6587\u6863\u3001\u7f16\u8f91\u6587\u6863\u5185\u5bb9\u3002<\/strong><\/p>\n<\/p>\n<hr>\n<h2><strong>PYTHON\u4e2d\u4f7f\u7528DOCX\u7684\u8be6\u7ec6\u6307\u5357<\/strong><\/h2>\n<p><p>\u5728\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u5904\u7406\u6587\u6863\u6587\u4ef6\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002Python\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5e93\u6765\u5904\u7406Word\u6587\u6863\uff0c\u5176\u4e2d\u6700\u6d41\u884c\u7684\u5c31\u662f<code>python-docx<\/code>\u5e93\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528<code>python-docx<\/code>\u5e93\u6765\u521b\u5efa\u548c\u7f16\u8f91Word\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b89\u88c5PYTHON-DOCX\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>python-docx<\/code>\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5b83\u5df2\u5b89\u88c5\u5728\u4f60\u7684Python\u73af\u5883\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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><p>\u5b89\u88c5\u6210\u529f\u540e\uff0c\u5373\u53ef\u5f00\u59cb\u4f7f\u7528\u8be5\u5e93\u6765\u8fdb\u884cWord\u6587\u6863\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u521b\u5efaWORD\u6587\u6863<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528<code>python-docx<\/code>\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u4e00\u4e2a\u7b80\u5355Word\u6587\u6863\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>1. \u5bfc\u5165\u5e93\u5e76\u521b\u5efa\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5bfc\u5165<code>Document<\/code>\u7c7b\uff0c\u7136\u540e\u5b9e\u4f8b\u5316\u4e00\u4e2a\u6587\u6863\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx import Document<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u6863<\/strong><\/h2>\n<p>doc = Document()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6dfb\u52a0\u6807\u9898\u548c\u6bb5\u843d<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u6587\u6863\u5bf9\u8c61\u7684<code>add_heading<\/code>\u548c<code>add_paragraph<\/code>\u65b9\u6cd5\u6765\u6dfb\u52a0\u6807\u9898\u548c\u6bb5\u843d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u6807\u9898<\/p>\n<p>doc.add_heading(&#39;Python-Docx\u4f7f\u7528\u6307\u5357&#39;, level=1)<\/p>\n<h2><strong>\u6dfb\u52a0\u6bb5\u843d<\/strong><\/h2>\n<p>doc.add_paragraph(&#39;\u8fd9\u662f\u4e00\u4e2a\u7528Python\u521b\u5efa\u7684Word\u6587\u6863\u793a\u4f8b\u3002&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4fdd\u5b58\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u5e76\u7f16\u8f91\u5b8c\u6587\u6863\u540e\uff0c\u9700\u8981\u5c06\u5176\u4fdd\u5b58\u5230\u672c\u5730\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u6587\u6863<\/p>\n<p>doc.save(&#39;example.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u7f16\u8f91WORD\u6587\u6863\u5185\u5bb9<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u521b\u5efa\u6587\u6863\uff0c<code>python-docx<\/code>\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u7f16\u8f91\u5df2\u6709\u7684Word\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h3>1. \u6253\u5f00\u5e76\u8bfb\u53d6\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>Document<\/code>\u7c7b\u6253\u5f00\u4e00\u4e2a\u5df2\u6709\u7684Word\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u5df2\u6709\u7684\u6587\u6863<\/p>\n<p>doc = Document(&#39;example.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u904d\u5386\u6587\u6863\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u904d\u5386\u6587\u6863\u4e2d\u7684\u6bb5\u843d\u548c\u8868\u683c\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u6bb5\u843d<\/p>\n<p>for para in doc.paragraphs:<\/p>\n<p>    print(para.text)<\/p>\n<h2><strong>\u904d\u5386\u8868\u683c<\/strong><\/h2>\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><h3>3. \u4fee\u6539\u6587\u6863\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u6bb5\u843d\u548c\u8868\u683c\u5355\u5143\u683c\u7684<code>text<\/code>\u5c5e\u6027\u6765\u6539\u53d8\u6587\u6863\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fee\u6539\u6bb5\u843d\u5185\u5bb9<\/p>\n<p>doc.paragraphs[0].text = &#39;\u4fee\u6539\u540e\u7684\u6bb5\u843d\u5185\u5bb9&#39;<\/p>\n<h2><strong>\u4fee\u6539\u8868\u683c\u5185\u5bb9<\/strong><\/h2>\n<p>doc.tables[0].cell(0, 0).text = &#39;\u65b0\u7684\u5355\u5143\u683c\u5185\u5bb9&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u6dfb\u52a0\u9ad8\u7ea7\u683c\u5f0f\u548c\u5143\u7d20<\/h2>\n<\/p>\n<p><p><code>python-docx<\/code>\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u57fa\u672c\u6587\u672c\uff0c\u8fd8\u652f\u6301\u6dfb\u52a0\u590d\u6742\u7684\u683c\u5f0f\u548c\u5143\u7d20\uff0c\u5982\u6837\u5f0f\u3001\u56fe\u7247\u548c\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>1. \u6dfb\u52a0\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4e3a\u6bb5\u843d\u548c\u6587\u672c\u5e94\u7528\u6837\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5e94\u7528\u6bb5\u843d\u6837\u5f0f<\/p>\n<p>para = doc.add_paragraph(&#39;\u5e26\u6709\u6837\u5f0f\u7684\u6bb5\u843d&#39;)<\/p>\n<p>para.style = &#39;Title&#39;<\/p>\n<h2><strong>\u5e94\u7528\u6587\u672c\u6837\u5f0f<\/strong><\/h2>\n<p>run = para.add_run(&#39; \u52a0\u7c97\u6587\u672c&#39;)<\/p>\n<p>run.bold = True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u63d2\u5165\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>add_picture<\/code>\u65b9\u6cd5\u63d2\u5165\u56fe\u7247\uff1a<\/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(2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u521b\u5efa\u548c\u7f16\u8f91\u8868\u683c<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>add_table<\/code>\u65b9\u6cd5\u521b\u5efa\u8868\u683c\uff0c\u5e76\u901a\u8fc7\u8bbf\u95ee\u8868\u683c\u7684<code>rows<\/code>\u548c<code>cells<\/code>\u5c5e\u6027\u6765\u7f16\u8f91\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u8868\u683c<\/p>\n<p>table = doc.add_table(rows=2, cols=2)<\/p>\n<h2><strong>\u586b\u5145\u8868\u683c\u5185\u5bb9<\/strong><\/h2>\n<p>table.cell(0, 0).text = &#39;\u5355\u5143\u683c 1,1&#39;<\/p>\n<p>table.cell(0, 1).text = &#39;\u5355\u5143\u683c 1,2&#39;<\/p>\n<p>table.cell(1, 0).text = &#39;\u5355\u5143\u683c 2,1&#39;<\/p>\n<p>table.cell(1, 1).text = &#39;\u5355\u5143\u683c 2,2&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u5904\u7406\u590d\u6742\u6587\u6863\u7ed3\u6784<\/h2>\n<\/p>\n<p><p>\u5904\u7406\u590d\u6742\u7684Word\u6587\u6863\u7ed3\u6784\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u9ad8\u7ea7\u7684\u64cd\u4f5c\uff0c\u5982\u6bb5\u843d\u683c\u5f0f\u8bbe\u7f6e\u3001\u9875\u7709\u9875\u811a\u7684\u7ba1\u7406\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1. \u8bbe\u7f6e\u6bb5\u843d\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>ParagraphFormat<\/code>\u7c7b\u8bbe\u7f6e\u6bb5\u843d\u7684\u5bf9\u9f50\u65b9\u5f0f\u3001\u7f29\u8fdb\u548c\u95f4\u8ddd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx.shared import Inches<\/p>\n<h2><strong>\u8bbe\u7f6e\u6bb5\u843d\u683c\u5f0f<\/strong><\/h2>\n<p>para_format = doc.paragraphs[0].paragraph_format<\/p>\n<p>para_format.alignment = WD_ALIGN_PARAGRAPH.CENTER<\/p>\n<p>para_format.left_indent = Inches(0.5)<\/p>\n<p>para_format.space_before = Pt(12)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6dfb\u52a0\u9875\u7709\u548c\u9875\u811a<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>sections<\/code>\u5c5e\u6027\u8bbf\u95ee\u6587\u6863\u7684\u8282\uff0c\u5e76\u6dfb\u52a0\u9875\u7709\u548c\u9875\u811a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u9875\u7709<\/p>\n<p>header = doc.sections[0].header<\/p>\n<p>header_para = header.paragraphs[0]<\/p>\n<p>header_para.text = &#39;\u8fd9\u662f\u9875\u7709&#39;<\/p>\n<h2><strong>\u6dfb\u52a0\u9875\u811a<\/strong><\/h2>\n<p>footer = doc.sections[0].footer<\/p>\n<p>footer_para = footer.paragraphs[0]<\/p>\n<p>footer_para.text = &#39;\u8fd9\u662f\u9875\u811a&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u5230\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528<code>python-docx<\/code>\u5e93\u6765\u521b\u5efa\u548c\u7f16\u8f91Word\u6587\u6863\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6587\u6863\u64cd\u4f5c\u8fd8\u662f\u590d\u6742\u7684\u6587\u6863\u683c\u5f0f\u8bbe\u7f6e\uff0c<code>python-docx<\/code>\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u652f\u6301\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528<code>python-docx<\/code>\u5e93\u6765\u6ee1\u8db3\u4f60\u7684\u6587\u6863\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5\u548c\u4f7f\u7528docx\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528docx\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5<code>python-docx<\/code>\u6a21\u5757\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install python-docx<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>import docx<\/code>\u6765\u5f15\u5165\u5e93\u3002\u63a5\u7740\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b83\u521b\u5efa\u3001\u4fee\u6539\u548c\u8bfb\u53d6Word\u6587\u6863\u3002\u5b98\u65b9\u6587\u6863\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u4f7f\u7528\u793a\u4f8b\uff0c\u5e2e\u52a9\u60a8\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<p><strong>\u4f7f\u7528docx\u5e93\u53ef\u4ee5\u8fdb\u884c\u54ea\u4e9b\u64cd\u4f5c\uff1f<\/strong><br \/>docx\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u64cd\u4f5cWord\u6587\u6863\u7684\u529f\u80fd\uff0c\u5305\u62ec\u521b\u5efa\u65b0\u6587\u6863\u3001\u6dfb\u52a0\u6bb5\u843d\u548c\u6807\u9898\u3001\u63d2\u5165\u8868\u683c\u548c\u56fe\u7247\u3001\u8bbe\u7f6e\u6587\u672c\u683c\u5f0f\uff08\u5982\u5b57\u4f53\u3001\u989c\u8272\u3001\u5927\u5c0f\uff09\u7b49\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684Python\u4ee3\u7801\u5b9e\u73b0\u590d\u6742\u7684\u6587\u6863\u5904\u7406\u4efb\u52a1\uff0c\u975e\u5e38\u9002\u5408\u81ea\u52a8\u5316\u751f\u6210\u62a5\u544a\u6216\u6587\u6863\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bfb\u53d6\u73b0\u6709\u7684Word\u6587\u6863\u5e76\u63d0\u53d6\u5185\u5bb9\uff1f<\/strong><br \/>\u8981\u8bfb\u53d6Word\u6587\u6863\uff0c\u53ef\u4ee5\u4f7f\u7528<code>docx.Document(&#39;\u6587\u4ef6\u540d.docx&#39;)<\/code>\u52a0\u8f7d\u6587\u6863\u3002\u52a0\u8f7d\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u6587\u6863\u7684\u6bb5\u843d\u3001\u8868\u683c\u7b49\u5bf9\u8c61\u6765\u63d0\u53d6\u6587\u672c\u5185\u5bb9\u3002\u4f7f\u7528<code>paragraph.text<\/code>\u53ef\u4ee5\u83b7\u53d6\u6bcf\u4e2a\u6bb5\u843d\u7684\u6587\u672c\uff0c\u4f7f\u7528<code>table.cell(row, col).text<\/code>\u53ef\u4ee5\u83b7\u53d6\u8868\u683c\u4e2d\u7279\u5b9a\u5355\u5143\u683c\u7684\u5185\u5bb9\u3002\u8fd9\u4f7f\u5f97\u4ece\u73b0\u6709\u6587\u6863\u4e2d\u63d0\u53d6\u4fe1\u606f\u53d8\u5f97\u7b80\u5355\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528docx\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u3001\u7f16\u8f91\u548c\u8bfb\u53d6Word\u6587\u6863\u3002\u8981\u4f7f\u7528docx\u6a21\u5757\uff0c\u901a\u5e38\u9700\u8981\u5b89\u88c5pyt [&hellip;]","protected":false},"author":3,"featured_media":943073,"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\/943072"}],"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=943072"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/943072\/revisions"}],"predecessor-version":[{"id":943074,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/943072\/revisions\/943074"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/943073"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=943072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=943072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=943072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}