{"id":961721,"date":"2024-12-27T04:01:25","date_gmt":"2024-12-26T20:01:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/961721.html"},"modified":"2024-12-27T04:01:27","modified_gmt":"2024-12-26T20:01:27","slug":"%e5%a6%82%e4%bd%95%e6%8a%8apython%e8%bd%ac%e4%b8%baword","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/961721.html","title":{"rendered":"\u5982\u4f55\u628aPython\u8f6c\u4e3aword"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103859\/392588f8-8cc3-4bf3-b355-6593472b1c5a.webp\" alt=\"\u5982\u4f55\u628aPython\u8f6c\u4e3aword\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3aWord\u6587\u6863\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5e93\u5982<code>python-docx<\/code>\u3001Markdown\u8bed\u6cd5\u4e0e\u8f6c\u6362\u5de5\u5177\u3001\u8f93\u51fa\u6587\u4ef6\u683c\u5f0f\u8f6c\u6362\u5de5\u5177\u7b49\u65b9\u6cd5\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u9002\u5408\u4f60\u7684\u9700\u6c42\u548c\u73af\u5883\u7684\u65b9\u6cd5\u662f\u5173\u952e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0Python\u8f6c\u6362\u4e3aWord\u6587\u6863\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>python-docx<\/code>\u5e93<\/p>\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\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u64cd\u4f5cWord\u6587\u6863\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5<code>python-docx<\/code>\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>python-docx<\/code>\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\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<\/li>\n<li>\n<p><strong>\u521b\u5efa\u548c\u7f16\u8f91Word\u6587\u6863<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>python-docx<\/code>\u5e93\u521b\u5efa\u548c\u7f16\u8f91Word\u6587\u6863\u975e\u5e38\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/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\u6807\u9898<\/strong><\/h2>\n<p>doc.add_heading(&#39;Python to Word Example&#39;, level=1)<\/p>\n<h2><strong>\u6dfb\u52a0\u6bb5\u843d<\/strong><\/h2>\n<p>doc.add_paragraph(&#39;This is a sample paragraph in the Word document.&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u6863<\/strong><\/h2>\n<p>doc.save(&#39;example.docx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06Python\u811a\u672c\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u8f93\u51fa\u5230Word\u6587\u6863\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u683c\u5f0f\u548c\u6837\u5f0f<\/strong><\/p>\n<\/p>\n<p><p><code>python-docx<\/code>\u5e93\u8fd8\u652f\u6301\u4e3a\u6587\u6863\u5185\u5bb9\u6dfb\u52a0\u683c\u5f0f\u548c\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4e3a\u6587\u672c\u6dfb\u52a0\u7c97\u4f53\u3001\u659c\u4f53\u6216\u8bbe\u7f6e\u4e0d\u540c\u7684\u5b57\u4f53\u548c\u989c\u8272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx.shared import Pt<\/p>\n<p>from docx.oxml.ns import qn<\/p>\n<h2><strong>\u6dfb\u52a0\u5e26\u683c\u5f0f\u7684\u6bb5\u843d<\/strong><\/h2>\n<p>paragraph = doc.add_paragraph()<\/p>\n<p>run = paragraph.add_run(&#39;This is a bold and italic text.&#39;)<\/p>\n<p>run.bold = True<\/p>\n<p>run.italic = True<\/p>\n<p>run.font.size = Pt(14)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53<\/strong><\/h2>\n<p>run.font.name = &#39;Arial&#39;<\/p>\n<p>run._element.rPr.rFonts.set(qn(&#39;w:eastAsia&#39;), &#39;\u5b8b\u4f53&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9Word\u6587\u6863\u5185\u5bb9\u7684\u590d\u6742\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528Markdown\u4e0e\u8f6c\u6362\u5de5\u5177<\/p>\n<\/p>\n<p><p>Markdown\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u6807\u8bb0\u8bed\u8a00\uff0c\u4f7f\u7528\u7b80\u6d01\u7684\u8bed\u6cd5\u6765\u8868\u793a\u6587\u6863\u7ed3\u6784\u3002\u5c06Python\u811a\u672c\u7684\u8f93\u51fa\u8f6c\u6362\u4e3aMarkdown\u683c\u5f0f\uff0c\u7136\u540e\u4f7f\u7528\u5de5\u5177\u5c06Markdown\u8f6c\u6362\u4e3aWord\u6587\u6863\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u751f\u6210Markdown\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7Python\u811a\u672c\u751f\u6210Markdown\u683c\u5f0f\u7684\u6587\u672c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">markdown_content = &quot;&quot;&quot;<\/p>\n<h2><strong>Python to Word Example<\/strong><\/h2>\n<p>This is a sample paragraph in the Word document.<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>with open(&#39;example.md&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(markdown_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Pandoc\u8f6c\u6362\u4e3aWord\u6587\u6863<\/strong><\/p>\n<\/p>\n<p><p>Pandoc\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6587\u6863\u8f6c\u6362\u5de5\u5177\uff0c\u652f\u6301Markdown\u5230Word\u7684\u8f6c\u6362\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Pandoc\uff0c\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pandoc example.md -o example.docx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4eceMarkdown\u5230\u591a\u79cd\u6587\u6863\u683c\u5f0f\u8f6c\u6362\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u8f93\u51fa\u6587\u4ef6\u683c\u5f0f\u8f6c\u6362\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u5982\u679cPython\u811a\u672c\u5df2\u7ecf\u751f\u6210\u4e86\u5176\u4ed6\u683c\u5f0f\u7684\u8f93\u51fa\u6587\u4ef6\uff08\u5982PDF\u3001HTML\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u683c\u5f0f\u8f6c\u6362\u5de5\u5177\u5c06\u5176\u8f6c\u6362\u4e3aWord\u6587\u6863\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u5728\u7ebf\u8f6c\u6362\u5de5\u5177<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u8bb8\u591a\u5728\u7ebf\u5de5\u5177\u53ef\u4ee5\u5c06PDF\u6216HTML\u6587\u4ef6\u8f6c\u6362\u4e3aWord\u6587\u6863\u3002\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u652f\u6301\u6279\u91cf\u8f6c\u6362\uff0c\u5e76\u63d0\u4f9b\u7b80\u5355\u7684\u754c\u9762\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u672c\u5730\u8f6f\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u672c\u5730\u5b89\u88c5\u7684\u8f6f\u4ef6\uff0c\u5982Adobe Acrobat\u6216Microsoft Word\uff0c\u4e5f\u63d0\u4f9b\u4e86\u5c06PDF\u6216\u5176\u4ed6\u683c\u5f0f\u6587\u4ef6\u8f6c\u6362\u4e3aWord\u6587\u6863\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\uff0c\u5c06Python\u811a\u672c\u7684\u8f93\u51fa\u8f6c\u6362\u4e3aWord\u6587\u6863\u3002\u65e0\u8bba\u662f\u4f7f\u7528<code>python-docx<\/code>\u5e93\u8fdb\u884c\u76f4\u63a5\u751f\u6210\uff0c\u8fd8\u662f\u901a\u8fc7Markdown\u4e0ePandoc\u8fdb\u884c\u95f4\u63a5\u8f6c\u6362\uff0c\u6291\u6216\u662f\u4f7f\u7528\u6587\u4ef6\u683c\u5f0f\u8f6c\u6362\u5de5\u5177\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u5728\u4e0d\u540c\u7a0b\u5ea6\u4e0a\u6ee1\u8db3\u5c06Python\u5185\u5bb9\u8f6c\u4e3aWord\u6587\u6863\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u751f\u6210\u7684\u5185\u5bb9\u5bfc\u51fa\u4e3aWord\u6587\u6863\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>python-docx<\/code>\u5e93\u6765\u521b\u5efa\u548c\u64cd\u4f5cWord\u6587\u6863\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>pip install python-docx<\/code>\u6765\u5b8c\u6210\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>Document()<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u6863\uff0c\u5e76\u4f7f\u7528<code>add_paragraph()<\/code>\u65b9\u6cd5\u6dfb\u52a0\u6587\u672c\u5185\u5bb9\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>save()<\/code>\u65b9\u6cd5\u5c06\u6587\u6863\u4fdd\u5b58\u4e3a\u6307\u5b9a\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u73b0\u6709\u7684\u6587\u672c\u6587\u4ef6\u8f6c\u6362\u4e3aWord\u683c\u5f0f\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5c06\u6587\u672c\u6587\u4ef6\u8f6c\u6362\u4e3aWord\u6587\u6863\u3002\u901a\u8fc7\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528<code>python-docx<\/code>\u5e93\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\uff0c\u5e76\u5c06\u8bfb\u53d6\u7684\u5185\u5bb9\u6dfb\u52a0\u5230\u6587\u6863\u4e2d\u3002\u5b8c\u6210\u540e\uff0c\u4fdd\u5b58\u6587\u6863\u5373\u53ef\u3002<\/p>\n<p><strong>\u5728\u5c06Python\u5185\u5bb9\u5bfc\u51fa\u4e3aWord\u65f6\uff0c\u5982\u4f55\u5904\u7406\u683c\u5f0f\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>python-docx<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5b57\u4f53\u3001\u6bb5\u843d\u6837\u5f0f\u3001\u989c\u8272\u7b49\u6765\u5904\u7406\u683c\u5f0f\u95ee\u9898\u3002\u8be5\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6587\u672c\u7684\u5916\u89c2\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>add_heading()<\/code>\u65b9\u6cd5\u4e3a\u6807\u9898\u8bbe\u7f6e\u6837\u5f0f\uff0c\u4f7f\u7528<code>add_paragraph()<\/code>\u65b9\u6cd5\u8c03\u6574\u6bb5\u843d\u683c\u5f0f\uff0c\u786e\u4fdd\u8f93\u51fa\u7684Word\u6587\u6863\u7b26\u5408\u4f60\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u811a\u672c\u8f6c\u6362\u4e3aWord\u6587\u6863\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5e93\u5982python-docx\u3001Markdown\u8bed\u6cd5\u4e0e [&hellip;]","protected":false},"author":3,"featured_media":961726,"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\/961721"}],"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=961721"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/961721\/revisions"}],"predecessor-version":[{"id":961729,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/961721\/revisions\/961729"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/961726"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=961721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=961721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=961721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}