把docx格式的word文档转换为txt文件

先将doc格式转换为docx格式

这里可以参考我的另一个博客: doc转docx.

def doc_to_docx(file_dir):
    docfiles = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] == '.doc':
                docfiles.append(os.path.join(root, file))
        word = wc.Dispatch("Word.Application")  # 打开word应用程序
        for docfile in docfiles:
            doc = word.Documents.Open(docfile)  # 打开word文件
            doc.SaveAs('{}x'.format(docfile), 12)  # 另存为后缀为".docx"的文件,其中参数12指docx文件
            doc.Close()  # 关闭原来word文件
            os.remove(docfile)
        word.Quit()
        print("完成!")  

运用python-docx工具包来操作word文档

首先下载docx工具包,在命令行中输入pip install docx,就可以安装docx工具包。

接着,读取word文档里面的文本和表格内容(因为我暂时处理的主要是这两种格式,所以只探究了如何提取这两种格式文本的方法)。

from docx import Document #导入方法
import re
document = Document(filename) #注意这里的filename必须是包含绝对路径的文件名

# 读取每段资料
l = [paragraph.text.encode('utf-8') for paragraph in document.paragraphs]
 # 输出并观察结果,也可以通过其他手段处理文本即可
pattern = r'(。|!|?|;)'
for i in l:
	list = []
	seg = i.decode('utf-8')
	seg = re.split(pattern, seg)
	seg.append("")
	seg = ["".join(i) for i in zip(seg[0::2], seg[1::2])]
for word in seg:
# 读取表格材料,并输出结果
tables = [table for table in document.tables]
for table in tables:
    for row in table.rows:
         for cell in row.cells:
             print(cell.text.encode('utf-8').decode('utf-8'), '\t', )                                        

乱码问题

最开始的时候,会出现一些乱码问题,上网查询了一下是编码问题,具体操作就是给定指定的编码格式utf-8,这里以后再去详细了解,我主要就靠着给含有文本的变量名定义decode(‘utf-8’)来使得编码成功解析出汉字(如果尝试的变量名没有decode后缀,可以先encode(‘utf-8’)再decode(‘utf-8’))例如代码里面的:

seg = i.decode('utf-8')
cell.text.encode('utf-8').decode('utf-8')

通过给定分隔符切分文段,并且保留分隔符

在提取文本时,我需要将大段的文本通过我要求的字符来切分成一句一句的句子,同时,需要保留分隔符在句尾。平常运用的split方法会直接将切分符号去掉,满足不了要求(这里,我没想到运用split方法能切分文段且保留标记的办法),网上找到了一个方法,特此记录学习一下。贴一下博客链接: 保留分隔符在句尾.

pattern = r'(。|!|?|;)'	#定义需要切割的分割符,加上()保留分隔符
seg = re.split(pattern, seg)	#通过split先进行切分
seg.append("")
seg = ["".join(i) for i in zip(seg[0::2], seg[1::2])]

写进txt文件里面

 output = open(filename, 'w', encoding='utf-8')
 for sentence in seg:
     output.write(sentence + '\n')
 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值