{"id":999463,"date":"2024-12-27T09:41:45","date_gmt":"2024-12-27T01:41:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/999463.html"},"modified":"2024-12-27T09:41:47","modified_gmt":"2024-12-27T01:41:47","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%a4%84%e7%90%86word","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/999463.html","title":{"rendered":"\u5982\u4f55\u7528python\u5904\u7406word"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074443\/e7ba8e6f-62df-4cfc-a210-54a1cf218d18.webp\" alt=\"\u5982\u4f55\u7528python\u5904\u7406word\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5904\u7406Word\u6587\u6863\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Python\u7684\u5e93\u5982<code>python-docx<\/code>\u3001<code>pandas<\/code>\u3001<code>openpyxl<\/code>\u7b49\u8fdb\u884c\u6587\u6863\u7684\u8bfb\u53d6\u3001\u7f16\u8f91\u548c\u683c\u5f0f\u5316\uff0c\u4ee5\u53ca\u5b9e\u73b0\u81ea\u52a8\u5316\u5904\u7406\u3002<\/strong>\u5176\u4e2d\uff0c<code>python-docx<\/code>\u662f\u6700\u5e38\u7528\u7684\u5de5\u5177\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a5\u53e3\u6765\u521b\u5efa\u3001\u8bfb\u53d6\u548c\u4fee\u6539Word\u6587\u6863\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u5904\u7406Word\u6587\u6863\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PYTHON-DOCX\u5e93\u7684\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/p>\n<\/p>\n<p><p><code>python-docx<\/code>\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u5904\u7406Word\u6587\u6863\u7684Python\u5e93\u3002\u5b83\u652f\u6301\u521b\u5efa\u3001\u4fee\u6539\u548c\u4fdd\u5b58Word\u6587\u6863\uff0c\u662f\u5904\u7406.docx\u6587\u4ef6\u7684\u9996\u9009\u5de5\u5177\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5python-docx<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528<code>python-docx<\/code>\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8fd9\u4e2a\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\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<\/li>\n<li>\n<p><strong>\u8bfb\u53d6Word\u6587\u6863<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>python-docx<\/code>\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6Word\u6587\u6863\u4e2d\u7684\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx import Document<\/p>\n<p>doc = Document(&#39;example.docx&#39;)<\/p>\n<p>for para in doc.paragraphs:<\/p>\n<p>    print(para.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u8bfb\u53d6\u4e00\u4e2aWord\u6587\u6863\uff0c\u5e76\u6253\u5370\u51fa\u6bcf\u4e2a\u6bb5\u843d\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u548c\u4fdd\u5b58Word\u6587\u6863<\/strong><\/p>\n<\/p>\n<p><p><code>python-docx<\/code>\u4e0d\u4ec5\u53ef\u4ee5\u8bfb\u53d6\u73b0\u6709\u6587\u6863\uff0c\u8fd8\u53ef\u4ee5\u521b\u5efa\u65b0\u7684\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx import Document<\/p>\n<p>doc = Document()<\/p>\n<p>doc.add_heading(&#39;Document Title&#39;, 0)<\/p>\n<p>doc.add_paragraph(&#39;This is a paragraph in the document.&#39;)<\/p>\n<p>doc.save(&#39;new_document.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u6f14\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u6863\uff0c\u6dfb\u52a0\u6807\u9898\u548c\u6bb5\u843d\uff0c\u7136\u540e\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u7f16\u8f91Word\u6587\u6863<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406Word\u6587\u6863\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u6587\u6863\u8fdb\u884c\u7f16\u8f91\uff0c\u5982\u6dfb\u52a0\u6587\u672c\u3001\u4fee\u6539\u6837\u5f0f\u3001\u63d2\u5165\u8868\u683c\u548c\u56fe\u7247\u7b49\u3002<code>python-docx<\/code>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a5\u53e3\u6765\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6dfb\u52a0\u6587\u672c\u548c\u6bb5\u843d<\/strong><\/p>\n<\/p>\n<p><p>\u6dfb\u52a0\u6587\u672c\u662f\u5904\u7406Word\u6587\u6863\u7684\u57fa\u7840\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528<code>add_paragraph<\/code>\u65b9\u6cd5\u6dfb\u52a0\u6bb5\u843d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx import Document<\/p>\n<p>doc = Document(&#39;existing_document.docx&#39;)<\/p>\n<p>doc.add_paragraph(&#39;This is a new paragraph.&#39;)<\/p>\n<p>doc.save(&#39;modified_document.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6dfb\u52a0\u6bb5\u843d\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u6837\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">para = doc.add_paragraph(&#39;This is a styled paragraph.&#39;, style=&#39;Heading1&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63d2\u5165\u8868\u683c<\/strong><\/p>\n<\/p>\n<p><p><code>python-docx<\/code>\u5141\u8bb8\u5728\u6587\u6863\u4e2d\u63d2\u5165\u8868\u683c\uff0c\u8fd9\u5bf9\u4e8e\u5c55\u793a\u7ed3\u6784\u5316\u6570\u636e\u975e\u5e38\u6709\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">table = doc.add_table(rows=3, cols=3)<\/p>\n<p>cell = table.cell(0, 0)<\/p>\n<p>cell.text = &#39;Cell content&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u884c\u5217\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539\u8868\u683c\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u56fe\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u5728Word\u6587\u6863\u4e2d\u63d2\u5165\u56fe\u7247\u53ef\u4ee5\u901a\u8fc7<code>add_picture<\/code>\u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">doc.add_picture(&#39;image.png&#39;, width=Inches(1.25))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u8c03\u6574\u56fe\u7247\u7684\u5927\u5c0f\u4ee5\u9002\u5e94\u6587\u6863\u7684\u5e03\u5c40\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u683c\u5f0f\u5316Word\u6587\u6863<\/p>\n<\/p>\n<p><p>\u5904\u7406Word\u6587\u6863\u65f6\uff0c\u683c\u5f0f\u5316\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u65b9\u9762\u3002<code>python-docx<\/code>\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8bbe\u7f6e\u6587\u672c\u7684\u683c\u5f0f\u3001\u6bb5\u843d\u6837\u5f0f\u548c\u5176\u4ed6\u6587\u6863\u5143\u7d20\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bbe\u7f6e\u6587\u672c\u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>runs<\/code>\u6765\u8bbe\u7f6e\u6bb5\u843d\u4e2d\u4e0d\u540c\u90e8\u5206\u7684\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">para = doc.add_paragraph(&#39;This is a &#39;)<\/p>\n<p>run = para.add_run(&#39;bold&#39;)<\/p>\n<p>run.bold = True<\/p>\n<p>para.add_run(&#39; text.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e<code>bold<\/code>\u3001<code>italic<\/code>\u3001<code>underline<\/code>\u7b49\u5c5e\u6027\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u6587\u672c\u683c\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6bb5\u843d\u5bf9\u9f50\u548c\u7f29\u8fdb<\/strong><\/p>\n<\/p>\n<p><p>\u6bb5\u843d\u7684\u5bf9\u9f50\u65b9\u5f0f\u53ef\u4ee5\u901a\u8fc7<code>alignment<\/code>\u5c5e\u6027\u8bbe\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx.enum.text import WD_ALIGN_PARAGRAPH<\/p>\n<p>para.alignment = WD_ALIGN_PARAGRAPH.CENTER<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u9009\u62e9<code>LEFT<\/code>\u3001<code>CENTER<\/code>\u3001<code>RIGHT<\/code>\u7b49\u5bf9\u9f50\u65b9\u5f0f\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u6bb5\u843d\u7684\u7f29\u8fdb\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">para.paragraph_format.left_indent = Inches(0.5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u9875\u9762\u5e03\u5c40<\/strong><\/p>\n<\/p>\n<p><p><code>python-docx<\/code>\u5141\u8bb8\u8bbe\u7f6e\u9875\u9762\u5e03\u5c40\uff0c\u5982\u7eb8\u5f20\u5927\u5c0f\u3001\u65b9\u5411\u548c\u9875\u8fb9\u8ddd\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">section = doc.sections[0]<\/p>\n<p>section.page_height = Inches(11)<\/p>\n<p>section.page_width = Inches(8.5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>sections<\/code>\u5c5e\u6027\u8bbf\u95ee\u6587\u6863\u7684\u8282\uff0c\u4ee5\u8c03\u6574\u5e03\u5c40\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u81ea\u52a8\u5316\u5904\u7406Word\u6587\u6863<\/p>\n<\/p>\n<p><p>Python\u7684\u5f3a\u5927\u4e4b\u5904\u5728\u4e8e\u5176\u81ea\u52a8\u5316\u80fd\u529b\u3002\u4f7f\u7528<code>python-docx<\/code>\uff0c\u53ef\u4ee5\u81ea\u52a8\u5316\u5904\u7406\u5927\u91cfWord\u6587\u6863\uff0c\u6781\u5927\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6279\u91cf\u5904\u7406\u6587\u6863<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Python\u811a\u672c\u6279\u91cf\u8bfb\u53d6\u548c\u5904\u7406\u591a\u4e2a\u6587\u6863\uff0c\u4f8b\u5982\uff0c\u63d0\u53d6\u7279\u5b9a\u4fe1\u606f\u6216\u7edf\u4e00\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from docx import Document<\/p>\n<p>directory = &#39;path\/to\/documents&#39;<\/p>\n<p>for filename in os.listdir(directory):<\/p>\n<p>    if filename.endswith(&#39;.docx&#39;):<\/p>\n<p>        doc = Document(os.path.join(directory, filename))<\/p>\n<p>        # Perform operations on the document<\/p>\n<p>        doc.save(os.path.join(directory, &#39;processed_&#39; + filename))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u62a5\u544a<\/strong><\/p>\n<\/p>\n<p><p>\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u5904\u7406\u5e93\uff0c\u5982<code>pandas<\/code>\uff0c\u53ef\u4ee5\u751f\u6210\u57fa\u4e8e\u6570\u636e\u7684Word\u62a5\u544a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.read_excel(&#39;data.xlsx&#39;)<\/p>\n<p>doc = Document()<\/p>\n<p>doc.add_heading(&#39;Report&#39;, 0)<\/p>\n<p>for index, row in data.iterrows():<\/p>\n<p>    doc.add_paragraph(f&quot;Record {index+1}: {row[&#39;Name&#39;]} - {row[&#39;Value&#39;]}&quot;)<\/p>\n<p>doc.save(&#39;report.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4eceExcel\u6570\u636e\u751f\u6210\u7ed3\u6784\u5316\u7684Word\u62a5\u544a\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u6269\u5c55Python\u5904\u7406Word\u6587\u6863\u7684\u80fd\u529b<\/p>\n<\/p>\n<p><p>\u867d\u7136<code>python-docx<\/code>\u662f\u5904\u7406Word\u6587\u6863\u7684\u4e3b\u8981\u5de5\u5177\uff0c\u4f46\u5728\u67d0\u4e9b\u590d\u6742\u573a\u666f\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u5de5\u5177\u548c\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e0e\u5176\u4ed6Python\u5e93\u7ed3\u5408<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9700\u8981\u5904\u7406\u590d\u6742\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408<code>pandas<\/code>\u548c<code>numpy<\/code>\u7b49\u6570\u636e\u5904\u7406\u5e93\uff0c\u4e0e<code>matplotlib<\/code>\u7ed3\u5408\u751f\u6210\u56fe\u8868\uff0c\u5e76\u63d2\u5165\u5230Word\u6587\u6863\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>plt.savefig(&#39;chart.png&#39;)<\/p>\n<p>doc.add_picture(&#39;chart.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u521b\u5efa\u66f4\u52a0\u4e30\u5bcc\u7684\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u5176\u4ed6\u6587\u6863\u5904\u7406\u5de5\u5177<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9700\u8981\u5904\u7406\u975e.docx\u683c\u5f0f\u7684\u6587\u6863\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u5de5\u5177\uff0c\u5982<code>PyPDF2<\/code>\u5904\u7406PDF\u6587\u4ef6\uff0c\u6216<code>openpyxl<\/code>\u5904\u7406Excel\u6587\u4ef6\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u4e0e<code>python-docx<\/code>\u7ed3\u5408\u4f7f\u7528\uff0c\u5f62\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u6587\u6863\u5904\u7406\u5de5\u4f5c\u6d41\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u603b\u7ed3\u4e0e\u5b9e\u8df5\u5efa\u8bae<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5904\u7406Word\u6587\u6863\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u529e\u516c\u6548\u7387\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5904\u7406\u5927\u91cf\u6587\u6863\u6216\u751f\u6210\u81ea\u52a8\u5316\u62a5\u544a\u65f6\u3002<code>python-docx<\/code>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a5\u53e3\u6765\u6ee1\u8db3\u5927\u591a\u6570\u6587\u6863\u5904\u7406\u9700\u6c42\uff0c\u4f46\u5728\u4e00\u4e9b\u590d\u6742\u573a\u666f\u4e0b\uff0c\u7ed3\u5408\u5176\u4ed6\u5de5\u5177\u548c\u5e93\u4f1a\u83b7\u5f97\u66f4\u597d\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u8df5\u4e2d\uff0c\u5efa\u8bae\u9996\u5148\u719f\u6089<code>python-docx<\/code>\u7684\u57fa\u672c\u529f\u80fd\uff0c\u7406\u89e3\u5176API\uff0c\u7136\u540e\u6839\u636e\u5177\u4f53\u9700\u6c42\u6269\u5c55\u5e94\u7528\u3002\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u811a\u672c\u5b9e\u73b0\u6587\u6863\u7684\u81ea\u52a8\u5316\u5904\u7406\uff0c\u5e76\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u5904\u7406\u5e93\u751f\u6210\u52a8\u6001\u5185\u5bb9\u3002\u901a\u8fc7\u4e0d\u65ad\u5b9e\u8df5\u548c\u79ef\u7d2f\u7ecf\u9a8c\uff0c\u53ef\u4ee5\u638c\u63e1\u66f4\u9ad8\u6548\u7684\u6587\u6863\u5904\u7406\u6280\u80fd\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\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8bfb\u53d6Word\u6587\u6863\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>python-docx<\/code>\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install python-docx<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6Word\u6587\u6863\u7684\u6587\u672c\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-python\">from docx import Document\n\n# \u6253\u5f00Word\u6587\u6863\ndoc = Document(&#39;your_file.docx&#39;)\n# \u63d0\u53d6\u6240\u6709\u6bb5\u843d\u6587\u672c\nfor para in doc.paragraphs:\n    print(para.text)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5c31\u53ef\u4ee5\u8f7b\u677e\u63d0\u53d6Word\u6587\u6863\u4e2d\u7684\u6587\u672c\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Word\u6587\u6863\u4e2d\u63d2\u5165\u56fe\u7247\u6216\u8868\u683c\uff1f<\/strong><br \/>\u4f7f\u7528<code>python-docx<\/code>\u5e93\uff0c\u63d2\u5165\u56fe\u7247\u548c\u8868\u683c\u4e5f\u975e\u5e38\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u63d2\u5165\u56fe\u7247\u548c\u8868\u683c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<p>\u63d2\u5165\u56fe\u7247\uff1a<\/p>\n<pre><code class=\"language-python\">from docx import Document\n\ndoc = Document()\ndoc.add_paragraph(&#39;\u8fd9\u91cc\u662f\u63d2\u5165\u7684\u56fe\u7247\uff1a&#39;)\ndoc.add_picture(&#39;image.png&#39;, width=docx.shared.Inches(2))  # \u8bbe\u7f6e\u56fe\u7247\u5bbd\u5ea6\ndoc.save(&#39;new_document.docx&#39;)\n<\/code><\/pre>\n<p>\u63d2\u5165\u8868\u683c\uff1a<\/p>\n<pre><code class=\"language-python\">from docx import Document\n\ndoc = Document()\ntable = doc.add_table(rows=2, cols=2)\ntable.cell(0, 0).text = &#39;\u5355\u5143\u683c1&#39;\ntable.cell(0, 1).text = &#39;\u5355\u5143\u683c2&#39;\ntable.cell(1, 0).text = &#39;\u5355\u5143\u683c3&#39;\ntable.cell(1, 1).text = &#39;\u5355\u5143\u683c4&#39;\ndoc.save(&#39;new_document_with_table.docx&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728Word\u6587\u6863\u4e2d\u6dfb\u52a0\u56fe\u7247\u548c\u8868\u683c\u4e86\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5904\u7406Word\u6587\u6863\u65f6\uff0c\u5982\u4f55\u6279\u91cf\u4fee\u6539\u6587\u6863\u7684\u683c\u5f0f\uff1f<\/strong><br \/>\u5728\u5904\u7406\u591a\u4e2aWord\u6587\u6863\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u6279\u91cf\u4fee\u6539\u6587\u6863\u7684\u683c\u5f0f\u3002\u53ef\u4ee5\u901a\u8fc7<code>python-docx<\/code>\u5e93\u904d\u5386\u6240\u6709\u6bb5\u843d\u548c\u6587\u672c\uff0c\u9010\u4e00\u4fee\u6539\u5176\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6240\u6709\u6bb5\u843d\u7684\u5b57\u4f53\u8bbe\u7f6e\u4e3a\u7c97\u4f53\uff1a<\/p>\n<pre><code class=\"language-python\">from docx import Document\nfrom docx.shared import Pt\n\ndoc = Document(&#39;your_file.docx&#39;)\nfor para in doc.paragraphs:\n    for run in para.runs:\n        run.bold = True  # \u5c06\u6587\u672c\u8bbe\u4e3a\u7c97\u4f53\n        run.font.size = Pt(12)  # \u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\ndoc.save(&#39;formatted_document.docx&#39;)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u7247\u6bb5\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7Python\u5feb\u901f\u6279\u91cf\u4fee\u6539Word\u6587\u6863\u7684\u683c\u5f0f\uff0c\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5904\u7406Word\u6587\u6863\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Python\u7684\u5e93\u5982python-docx\u3001pandas\u3001o [&hellip;]","protected":false},"author":3,"featured_media":999472,"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\/999463"}],"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=999463"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999463\/revisions"}],"predecessor-version":[{"id":999476,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999463\/revisions\/999476"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/999472"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=999463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=999463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=999463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}