{"id":147,"date":"2020-07-08T16:32:26","date_gmt":"2020-07-08T08:32:26","guid":{"rendered":"http:\/\/www.hufe.org.cn\/?p=147"},"modified":"2020-07-08T16:32:26","modified_gmt":"2020-07-08T08:32:26","slug":"%e4%bd%bf%e7%94%a8python%e5%b0%86doc%e6%96%87%e4%bb%b6%e8%bd%ac%e4%b8%bautf8%e7%bc%96%e7%a0%81%e6%a0%bc%e5%bc%8f%e7%9a%84txt","status":"publish","type":"post","link":"https:\/\/www.hufe.org.cn\/?p=147","title":{"rendered":"\u4f7f\u7528python\u5c06doc\u6587\u4ef6\u8f6c\u4e3autf8\u7f16\u7801\u683c\u5f0f\u7684txt"},"content":{"rendered":"<p>\u6700\u8fd1\u82b1\u4e86\u70b9\u65f6\u95f4\u770b\u4e86\u4f1adoc\u7684\u8f6c\u6362\uff0c\u679c\u7136\u5b98\u65b9\u6587\u6863\u6700\u597d\u7528\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<br \/>\nimport os<br \/>\nimport sys<br \/>\nimport fnmatch<br \/>\nimport win32com.client<\/p>\n<p>PATH = os.path.abspath(os.path.dirname(sys.argv[0]))<br \/>\ndoc_path = PATH + &#8216;\/data\/doc\/&#8217;<br \/>\ntxt_path = PATH + &#8216;\/data\/txt\/&#8217;<\/p>\n<p>def convert_dir_to_txt():<br \/>\n    &#8220;&#8221;&#8221;<br \/>\n    \u5c06\u9ed8\u8ba4\u6574\u4e2a\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6\u90fd\u8fdb\u884c\u8f6c\u6362<br \/>\n    :return:<br \/>\n    &#8220;&#8221;&#8221;<br \/>\n    for root, dirs, files in os.walk(doc_path):<br \/>\n        for _dir in dirs:<br \/>\n            pass<br \/>\n        for _file in files:<br \/>\n            if fnmatch.fnmatch(_file, &#8216;*.doc&#8217;):<br \/>\n                store_file = txt_path + _file[:-3] + &#8216;txt&#8217;<br \/>\n            elif fnmatch.fnmatch(_file, &#8216;*.docx&#8217;):<br \/>\n                store_file = txt_path + _file[:-4] + &#8216;txt&#8217;<br \/>\n            word_file = os.path.join(root, _file)<br \/>\n            dealer.Documents.Open(word_file)<br \/>\n            try:<br \/>\n                dealer.ActiveDocument.SaveAs(store_file, FileFormat=7,Encoding=65001)<br \/>\n            except Exception as e:<br \/>\n                print(e)<br \/>\n            dealer.ActiveDocument.Close()<\/p>\n<p>dealer = win32com.client.gencache.EnsureDispatch(&#8216;Word.Application&#8217;)<br \/>\nconvert_dir_to_txt()<\/p>\n<p>\u5ef6\u4f38\uff1a\u6839\u636e\u4e0b\u9762\u5b98\u65b9\u7ed9\u51fa\u7684\u53c2\u8003\u8d44\u6599\u4e2d\u7684\u6570\u503c\uff0c\u53ef\u4ee5\u8f6c\u6362\u6210\u591a\u79cd\u5f62\u5f0f\u591a\u79cd\u7f16\u7801\u6587\u4ef6\u3002<\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n<p>https:\/\/docs.microsoft.com\/zh-cn\/office\/vba\/api\/Office.MsoEncoding<br \/>\nhttps:\/\/docs.microsoft.com\/en-us\/previous-versions\/office\/developer\/office-2010\/ff839952(v=office.14)?redirectedfrom=MSDN<br \/>\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<br \/>\n\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3aCSDN\u535a\u4e3b\u300c\u516d\u795e\u5c31\u662f\u6211\u300d\u7684\u539f\u521b\u6587\u7ae0\uff0c\u9075\u5faaCC 4.0 BY-SA\u7248\u6743\u534f\u8bae\uff0c\u8f6c\u8f7d\u8bf7\u9644\u4e0a\u539f\u6587\u51fa\u5904\u94fe\u63a5\u53ca\u672c\u58f0\u660e\u3002<br \/>\n\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/sinat_33455447\/java\/article\/details\/101020285<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u82b1\u4e86\u70b9\u65f6\u95f4\u770b\u4e86\u4f1adoc\u7684\u8f6c\u6362\uff0c\u679c\u7136\u5b98\u65b9\u6587\u6863\u6700\u597d\u7528\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a import os import sys i&hellip;&nbsp;<a href=\"https:\/\/www.hufe.org.cn\/?p=147\" class=\"\" rel=\"bookmark\">\u9605\u8bfb\u66f4\u591a &raquo;<span class=\"screen-reader-text\">\u4f7f\u7528python\u5c06doc\u6587\u4ef6\u8f6c\u4e3autf8\u7f16\u7801\u683c\u5f0f\u7684txt<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/147"}],"collection":[{"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=147"}],"version-history":[{"count":1,"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/147\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}