我正在尝试提取DOCX Word文档中表格的内容,但是我对xml/xpath不熟悉。
from docx import *
document = opendocx('someFile.docx')
tableList = document.xpath('/w:tbl')
这会触发“XPathEvalError:未定义的命名空间前缀”错误。我相信这只是在开发脚本时可以预期的第一个错误。不幸的是,我找不到有关python-docx的教程。
您能否提供一个表格提取的示例?
我正在尝试提取DOCX Word文档中表格的内容,但是我对xml/xpath不熟悉。
from docx import *
document = opendocx('someFile.docx')
tableList = document.xpath('/w:tbl')
这会触发“XPathEvalError:未定义的命名空间前缀”错误。我相信这只是在开发脚本时可以预期的第一个错误。不幸的是,我找不到有关python-docx的教程。
您能否提供一个表格提取的示例?
tblList = document.xpath('//w:tbl', namespaces=document.nsmap) 的效果非常好。因此,我理解 w: 是必须扩展为完整命名空间名称的简写形式,并且该字典由 document.nsmap 提供。
首先按照 @abdulsaboor 的建议安装 python-docx
pip install python-docx
然后这段代码应该做什么:
from docx import Document
document = Document('myfile.docx')
for table in document.tables:
print()
for row in table.rows:
for cell in row.cells:
print(cell.text, end=' ')
from docx import Document()
document = Document(file_path)
tables = document.tables
tableList = document.xpath('//tbl')
print tableList
这是我不理解的DOCX特性吗? - mgierdalprint document
打印出整个xml文件,看看是否有您期望的标签。如果有,那么您的函数调用可能有问题。查找lxml库中xpath函数的文档,看看您是否有格式错误的命令。 - Spencer Rathbun