如何在 Linux 上使用 Python 读取 word 文件信息 | 您所在的位置:网站首页 › python如何打开word文档 › 如何在 Linux 上使用 Python 读取 word 文件信息 |
第一步:获取doc文件的xml组成文件 import zipfiledef get_word_xml(docx_filename): with open(docx_filename) as f: zip = zipfile.ZipFile(f) xml_content = zip.read('word/document.xml') return xml_content 第二步:解析xml为树形数据结构 from lxml import etreedef get_xml_tree(xml_string): return etree.fromstring(xml_string) 第三步:读取word内容: def _itertext(self, my_etree): """Iterator to go through xml tree's text nodes""" for node in my_etree.iter(tag=etree.Element): if self._check_element_is(node, 't'): yield (node, node.text)def _check_element_is(self, element, type_char): word_schema = '99999' return element.tag == '{%s}%s' % (word_schema,type_char) 可以使用金山公司的wps软件或者libreoffice或者其他的office软件。1,下载wps或者libreoffice任意一款软件安装包,如果是debian系统,得到安装包XX.deb。 2,安装软件:打开终端,输入命令,sudo dpkg -i XXX.deb。 3,安装完成后可以打开doc文件。图形界面:右键点击文件,选择属性,设置默认打开方式为wps,确定后双击文件即可打开。命令:wps fileName.doc。 欢迎分享,转载请注明来源:内存溢出 原文地址:https://outofmemory.cn/yw/7258154.html |
CopyRight 2018-2019 实验室设备网 版权所有 |