python对word提取数据,如何使用Python从doc / docx文件中提取数据
docx是一個包含文檔XML的zip文件.您可以打開zip,閱讀文檔并使用ElementTree解析數(shù)據(jù).
這種技術(shù)的優(yōu)點是你不需要安裝任何額外的python庫.
import zipfile
import xml.etree.ElementTree
WORD_NAMESPACE = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
PARA = WORD_NAMESPACE + 'p'
TEXT = WORD_NAMESPACE + 't'
TABLE = WORD_NAMESPACE + 'tbl'
ROW = WORD_NAMESPACE + 'tr'
CELL = WORD_NAMESPACE + 'tc'
with zipfile.ZipFile('') as docx:
tree = xml.etree.ElementTree.XML(docx.read('word/document.xml'))
for table in tree.iter(TABLE):
for row in table.iter(ROW):
for cell in row.iter(CELL):
print ''.join(node.text for node in cell.iter(TEXT))
總結(jié)
以上是生活随笔為你收集整理的python对word提取数据,如何使用Python从doc / docx文件中提取数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一语成谶!网传鬼火少年“三部曲”:片头壮
- 下一篇: 小米8se指纹设置在哪(小米官方售后服务