Ubuntu下使用python读取doc和docx文档的内容

来源:互联网 发布:淘宝买到假货已经下架 编辑:程序博客网 时间:2024/05/29 15:25

读取docx文档

使用的包是python-docx
1. 安装python-docx包
sudo pip install python-docx
2. 使用python-docx包读取数据
#encoding:utf8import docxdoc = docx.Document('test.docx')docText = '\n'.join([paragraph.text for paragraph in doc.paragraphs])#print(docText)

python-docx这个包是不能处理doc文档的,要读取doc文档内容的话需要使用antiword这个工具。

读取doc文档

1. 到网站下载antiword。
2. 下载完毕之后解压,在解压得到的文件夹中依次运行make和make install命令。
3. 使用antiword读取doc文档内容
#encoding:utf8import subprocessword = 'test.doc'output = subprocess.check_output(['antiword',word])print(output)


0 0