{"id":20,"date":"2025-06-30T18:00:13","date_gmt":"2025-06-30T10:00:13","guid":{"rendered":"https:\/\/www.idcyz.com\/blog\/?p=20"},"modified":"2025-06-30T18:00:13","modified_gmt":"2025-06-30T10:00:13","slug":"python-%e5%ae%9e%e7%8e%b0-word-%e6%96%87%e6%a1%a3%e4%b8%ad%e7%b9%81%e4%bd%93%e5%ad%97%e6%89%b9%e9%87%8f%e8%bd%ac%e6%8d%a2%e4%b8%ba%e7%ae%80%e4%bd%93%e5%ad%97%e7%9a%84%e5%ae%8c%e6%95%b4%e6%ad%a5","status":"publish","type":"post","link":"https:\/\/www.idcyz.com\/blog\/20.html","title":{"rendered":"Python \u5b9e\u73b0 Word \u6587\u6863\u4e2d\u7e41\u4f53\u5b57\u6279\u91cf\u8f6c\u6362\u4e3a\u7b80\u4f53\u5b57\u7684\u5b8c\u6574\u6b65\u9aa4\u4e0e\u793a\u4f8b\u4ee3\u7801"},"content":{"rendered":"<p><p>\u5728\u5bf9\u6587\u672c\u5185\u5bb9\u8fdb\u884c\u5904\u7406\u65f6\uff0c\u7e41\u4f53\u5b57\u548c\u7b80\u4f53\u5b57\u7684\u8f6c\u6362\u662f\u4e00\u9879\u5e38\u89c1\u7684\u9700\u6c42\u3002\u7279\u522b\u662f\u5728\u5904\u7406\u4e2d\u6587\u6587\u6863\u65f6\uff0c\u5f88\u591a\u7528\u6237\u9700\u8981\u5c06\u7e41\u4f53\u5b57\u6279\u91cf\u8f6c\u6362\u4e3a\u7b80\u4f53\u5b57\u3002Python \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5f3a\u5927\u7684\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Python \u6279\u91cf\u5c06 Word \u6587\u6863\u4e2d\u7684\u7e41\u4f53\u5b57\u8f6c\u6362\u4e3a\u7b80\u4f53\u5b57\uff0c\u5e76\u63d0\u4f9b\u660e\u786e\u7684\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.idcyz.com\/blog\/wp-content\/uploads\/2025\/06\/CYhSuBab.jpg\" alt=\"Python \u5b9e\u73b0 Word \u6587\u6863\u4e2d\u7e41\u4f53\u5b57\u6279\u91cf\u8f6c\u6362\u4e3a\u7b80\u4f53\u5b57\u7684\u5b8c\u6574\u6b65\u9aa4\u4e0e\u793a\u4f8b\u4ee3\u7801\" title=\"Python \u5b9e\u73b0 Word \u6587\u6863\u4e2d\u7e41\u4f53\u5b57\u6279\u91cf\u8f6c\u6362\u4e3a\u7b80\u4f53\u5b57\u7684\u5b8c\u6574\u6b65\u9aa4\u4e0e\u793a\u4f8b\u4ee3\u7801\"><\/p>\n<\/p>\n<p><h2>\u80cc\u666f\u4ecb\u7ecd<\/h2>\n<\/p>\n<p><p>\u5728\u4e2d\u6587\u5904\u7406\u4e2d\uff0c\u7e41\u4f53\u5b57\u548c\u7b80\u4f53\u5b57\u7684\u4f7f\u7528\u573a\u666f\u4e0d\u540c\u3002\u7e41\u4f53\u5b57\u4e3b\u8981\u5728\u9999\u6e2f\u3001\u6fb3\u95e8\u53ca\u53f0\u6e7e\u5730\u533a\u4f7f\u7528\uff0c\u800c\u7b80\u4f53\u5b57\u5219\u662f\u5927\u9646\u5730\u533a\u7684\u6807\u51c6\u4e66\u5199\u65b9\u5f0f\u3002\u4e3a\u4e86\u4fbf\u4e8e\u4e0d\u540c\u5730\u533a\u7528\u6237\u7684\u6c9f\u901a\u548c\u6587\u672c\u5171\u4eab\uff0c\u5e38\u5e38\u9700\u8981\u8fdb\u884c\u6279\u91cf\u7684\u5b57\u5f62\u8f6c\u6362\u3002\u6b64\u64cd\u4f5c\u4e0d\u4ec5\u9002\u7528\u4e8e\u6587\u6863\u5904\u7406\uff0c\u4e5f\u53ef\u4ee5\u5e94\u7528\u4e8e\u5404\u79cd\u6587\u672c\u5185\u5bb9\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h2>\u51c6\u5907\u5de5\u4f5c<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u7684 Python \u73af\u5883\u4e2d\u5b89\u88c5\u4e86\u4ee5\u4e0b\u5e93\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>python-docx<\/strong>\uff1a\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165 Word \u6587\u6863\u3002<\/li>\n<li><strong>opencc-python-reimplemented<\/strong>\uff1a\u7528\u4e8e\u7e41\u7b80\u4f53\u8f6c\u6362\u7684\u5de5\u5177\u3002<\/li>\n<\/ul>\n<p><p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7 pip \u5b89\u88c5\u6240\u9700\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install python-docx opencc-python-reimplemented<\/code><\/pre>\n<\/p>\n<p><h2>\u6b65\u9aa4\u6307\u5357<\/h2>\n<\/p>\n<p><h3>\u6b65\u9aa4 1\uff1a\u521b\u5efa\u8f6c\u6362\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u5904\u7406\u7e41\u7b80\u4f53\u7684\u8f6c\u6362\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 OpenCC \u6765\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code>from opencc import OpenCC<\/p>\r\n\r\n<p>def convert_text(text):<\/p>\r\n<p>    cc = OpenCC('t2s')  # \u7e41\u4f53\u8f6c\u7b80\u4f53<\/p>\r\n<p>    return cc.convert(text)<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6b65\u9aa4 2\uff1a\u8bfb\u53d6 Word \u6587\u6863<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 <strong>python-docx<\/strong> \u5e93\u8bfb\u53d6 Word \u6587\u6863\u7684\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u8bfb\u53d6\u6587\u6863\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code>from docx import Document<\/p>\r\n\r\n<p>def read_docx(file_path):<\/p>\r\n<p>    doc = Document(file_path)<\/p>\r\n<p>    content = []<\/p>\r\n<p>    for para in doc.paragraphs:<\/p>\r\n<p>        content.append(para.text)<\/p>\r\n<p>    return \"\\n\".join(content)<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6b65\u9aa4 3\uff1a\u5199\u5165\u8f6c\u6362\u540e\u7684\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u7b80\u4f53\u5b57\u540e\uff0c\u6211\u4eec\u9700\u8981\u5c06\u7ed3\u679c\u5199\u5165\u65b0\u7684 Word \u6587\u6863\u4e2d\u3002\u4f7f\u7528 <strong>python-docx<\/strong>\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u65b0\u6587\u6863\u5e76\u5199\u5165\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code>def write_docx(new_file_path, content):<\/p>\r\n<p>    doc = Document()<\/p>\r\n<p>    doc.add_paragraph(content)<\/p>\r\n<p>    doc.save(new_file_path)<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6b65\u9aa4 4\uff1a\u6574\u5408\u6574\u4e2a\u8fc7\u7a0b<\/h3>\n<\/p>\n<p><p>\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u6574\u5408\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u7a0b\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code>def convert_word_file(input_path, output_path):<\/p>\r\n<p>    # \u8bfb\u53d6\u6587\u6863\u4e2d\u7684\u5185\u5bb9<\/p>\r\n<p>    text = read_docx(input_path)<\/p>\r\n<p>    # \u8f6c\u6362\u4e3a\u7b80\u4f53\u5b57<\/p>\r\n<p>    simplified_text = convert_text(text)<\/p>\r\n<p>    # \u5199\u5165\u65b0\u6587\u6863<\/p>\r\n<p>    write_docx(output_path, simplified_text)<\/p>\r\n\r\n<p># \u8c03\u7528\u51fd\u6570\u8fdb\u884c\u5904\u7406<\/p>\r\n<p>input_file = 'path\/to\/input.docx'  # \u6e90\u6587\u4ef6\u8def\u5f84<\/p>\r\n<p>output_file = 'path\/to\/output.docx'  # \u8f93\u51fa\u6587\u4ef6\u8def\u5f84<\/p>\r\n<p>convert_word_file(input_file, output_file)<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u548c\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<ul>\n<li><strong>\u6587\u4ef6\u8def\u5f84<\/strong>\uff1a\u786e\u4fdd\u8f93\u5165\u548c\u8f93\u51fa\u7684\u6587\u4ef6\u8def\u5f84\u662f\u6b63\u786e\u7684\uff0c\u5e76\u4e14\u6307\u5b9a\u4e86\u76f8\u5e94\u7684\u6269\u5c55\u540d\uff08.docx\uff09\u3002<\/li>\n<li><strong>\u6587\u672c\u683c\u5f0f\u4e22\u5931<\/strong>\uff1a\u8be5\u65b9\u6cd5\u4ec5\u5904\u7406\u6587\u6863\u6587\u672c\u5185\u5bb9\uff0c\u53ef\u80fd\u4f1a\u4e22\u5931\u56fe\u7247\u3001\u8868\u683c\u7b49\u683c\u5f0f\u4fe1\u606f\u3002\u5982\u679c\u9700\u8981\u540c\u65f6\u4fdd\u7559\u683c\u5f0f\uff0c\u9700\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5904\u7406\u3002<\/li>\n<li><strong>\u7f16\u7801\u95ee\u9898<\/strong>\uff1a\u786e\u4fdd\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u4e0e Python \u7684\u9884\u671f\u7f16\u7801\u4e00\u81f4\uff0c\u4ee5\u907f\u514d\u4e71\u7801\u3002<\/li>\n<\/ul>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528 Python \u53ca\u76f8\u5173\u5e93\uff0c\u53ef\u4ee5\u5feb\u901f\u5b9e\u73b0 Word \u6587\u6863\u4e2d\u7e41\u4f53\u5b57\u5230\u7b80\u4f53\u5b57\u7684\u6279\u91cf\u8f6c\u6362\u3002\u4e0a\u6587\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u548c\u6269\u5c55\u3002\u638c\u63e1\u8fd9\u4e00\u6280\u672f\uff0c\u4e0d\u4ec5\u80fd\u591f\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\uff0c\u8fd8\u80fd\u66f4\u597d\u5730\u670d\u52a1\u4e0d\u540c\u5730\u533a\u7684\u7528\u6237\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5bf9\u6587\u672c\u5185\u5bb9\u8fdb\u884c\u5904\u7406\u65f6\uff0c\u7e41\u4f53\u5b57\u548c\u7b80\u4f53\u5b57\u7684\u8f6c\u6362\u662f\u4e00\u9879\u5e38\u89c1\u7684\u9700\u6c42\u3002\u7279\u522b\u662f\u5728\u5904\u7406\u4e2d\u6587\u6587\u6863\u65f6\uff0c\u5f88\u591a\u7528\u6237\u9700\u8981\u5c06\u7e41\u4f53\u5b57\u6279\u91cf\u8f6c\u6362\u4e3a\u7b80\u4f53\u5b57\u3002Python \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5f3a\u5927\u7684\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Python \u6279\u91cf\u5c06 Word \u6587&#8230;<\/p>\n","protected":false},"author":1,"featured_media":21,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[12,10,11],"topic":[],"class_list":["post-20","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jiushu","tag-python","tag-10","tag-11"],"_links":{"self":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/comments?post=20"}],"version-history":[{"count":1,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions"}],"predecessor-version":[{"id":23,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions\/23"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media\/21"}],"wp:attachment":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media?parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/categories?post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/tags?post=20"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/topic?post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}