(Python文件处理)doc文档转UTF-8格式的TXT文档

目录

目录下所有doc文档转txt,本来想直接用SaveAs规定转存编码格式,但是得到的是带BOM的UTF-8格式,所以又加了个去BOM的过程。

doc转txt

得到带BOM的UTF-8:

import os
import sys
import codecs
import fnmatch
import win32com.client

PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
doc_path = PATH + '/data/doc/'
txt_path = PATH + '/data/txt/'


def convert_dir_to_txt():
    """
    将默认整个文件夹下的文件都进行转换
    :return:
    """
    for root, dirs, files in os.walk(doc_path):
        for _dir in dirs:
            pass
        for _file in files:
            if fnmatch.fnmatch(_file, '*.doc'):
                store_file = txt_path + _file[:-3] + 'txt'
            elif fnmatch.fnmatch(_file, '*.docx'):
                store_file = txt_path + _file[:-4] + 'txt'
            word_file = os.path.join(root, _file)
            dealer.Documents.Open(word_file)
            try:
                dealer.ActiveDocument.SaveAs(store_file, FileFormat=7, Encoding=65001)
            except Exception as e:
                print(e)
            dealer.ActiveDocument.Close()

dealer = win32com.client.gencache.EnsureDispatch('Word.Application')
convert_dir_to_txt()

UTF-8去BOM

限制一次阅读量(因为BOM只在最开头,所以不需要遍历整个文件):

BUFSIZE = 4096
BOMLEN = len(codecs.BOM_UTF8)

在convert_dir_to_txt函数的末尾增加去BOM的代码段:

    for root, dirs, files in os.walk(txt_path):
        for _dir in dirs:
            pass
        for _file in files:
            path = txt_path+_file
            try:
                with open(path, "r+b") as fp:
                    chunk = fp.read(BUFSIZE)
                    if chunk.startswith(codecs.BOM_UTF8):
                        i = 0
                        chunk = chunk[BOMLEN:]
                        while chunk:
                            fp.seek(i)
                            fp.write(chunk)
                            i += len(chunk)
                            fp.seek(BOMLEN, os.SEEK_CUR)
                            chunk = fp.read(BUFSIZE)
                        fp.seek(-BOMLEN, os.SEEK_CUR)
                        fp.truncate()
            except Exception as e:
                print(e)

完事儿。

参考:

第一部分doc转带BOM的UTF-8参考博文:使用python将doc文件转为utf8编码格式的txt

上一篇:JavaScript之BOM


下一篇:了解js中的dom和bom对象,以及两者区别