{"id":204174,"date":"2025-05-29T14:32:21","date_gmt":"2025-05-29T06:32:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204174\/"},"modified":"2025-05-29T14:32:21","modified_gmt":"2025-05-29T06:32:21","slug":"python-docx-%e4%b8%ad%e4%bf%ae%e6%94%b9%e4%b8%ad%e6%96%87%e5%ad%97%e4%bd%93%e4%b8%ba%e4%bd%95%e5%a4%b1%e6%95%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204174\/","title":{"rendered":"Python-docx \u4e2d\u4fee\u6539\u4e2d\u6587\u5b57\u4f53\u4e3a\u4f55\u5931\u6548\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python-docx \u4e2d\u4fee\u6539\u4e2d\u6587\u5b57\u4f53\u4e3a\u4f55\u5931\u6548\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241030\/1730294511672232ef5d8fb.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python-docx \u5b57\u4f53\u65e0\u6cd5\u4fee\u6539\uff0c\u5bfb\u6c42\u89e3\u51b3\u4e4b\u9053<\/strong><\/p>\n<p>\u5728\u4f7f\u7528python-docx\u5e93\u4fee\u6539\u5b57\u4f53\u65f6\uff0c\u9047\u5230\u65e0\u6cd5\u6b63\u786e\u663e\u793a\u4e2d\u6587\u5b57\u4f53\u7684\u95ee\u9898\uff0c\u90e8\u5206\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>from docx import document\nfrom docx.shared import pt\n\ndocument = document()\nfirst_paragraph = document.add_paragraph()\nfirst_style = first_paragraph.add_run('\u6211\u662f\u4e2d\u56fd\u4eba') #\u65b0\u589e\u9996\u884c\u6837\u5f0f\u5e76\u6dfb\u52a0\u6587\u5b57\nfirst_style.font.size = pt(20) # \u8bbe\u7f6e\u5b57\u53f7\u6837\u5f0f\nfirst_style.font.name = '\u9ed1\u4f53'  # \u8bbe\u7f6e\u5b57\u4f53\u6837\u5f0f<\/pre>\n<p>\u7ed3\u679c\u663e\u793a\uff0c&#8221;\u9ed1\u4f53&#8221;\u6837\u5f0f\u5e76\u672a\u751f\u6548\uff0c\u800c\u662f\u663e\u793a\u9ed8\u8ba4\u5b57\u4f53&#8221;ms mincho&#8221;\u3002<\/p>\n<p><strong>\u539f\u56e0\u5206\u6790<\/strong><\/p>\n<p>\u5728\u6d4b\u8bd5\u8be5\u4ee3\u7801\u65f6\u53d1\u73b0\uff0c\u5b83\u53ea\u80fd\u5bf9\u82f1\u6587\u5b57\u7b26\u751f\u6548\u3002\u56e0\u6b64\uff0c\u65e0\u6cd5\u6b63\u786e\u66f4\u6539\u4e2d\u6587\u5b57\u4f53\u7684\u6837\u5f0f\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u8981\u4fee\u6539\u4e2d\u6587\u5b57\u4f53\u6837\u5f0f\uff0c\u9700\u8981\u4fee\u6539\u6837\u5f0f\uff0c\u5e76\u901a\u8fc7\u4e0d\u540c\u7684\u6837\u5f0f\u6765\u63a7\u5236\u4e2d\u6587\u5b57\u4f53\u3002\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>document = Document()\ndocument.styles['Normal'].font.name = u'\u9ed1\u4f53'\ndocument.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'\u9ed1\u4f53')<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPython-docx \u4e2d\u4fee\u6539\u4e2d\u6587\u5b57\u4f53\u4e3a\u4f55\u5931\u6548\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python-docx \u4e2d\u4fee\u6539\u4e2d&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204174","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204174"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204174\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}