【发布时间】:2015-11-20 13:34:39
【问题描述】:
如果有这样的 XML 文档:
<!-- Location -->
<w:t>Lokacioni:</w:t>
<w:t>Kucni:</w:t>
<w:t>Extension:</w:t>
<w:t>Hajvali –Prishtinë</w:t>
<w:t>Rr. “ Dëshmorët e Gollakut “</w:t>
<w:t>P. N. Prishtinë</w:t>
<!-- Date -->
<w:t>Dat:</w:t>
<w:t>Datum:</w:t>
<w:t>Date:</w:t>
<w:t xml:space="preserve"> </w:t>
<!-- Free text - contains time and description-->
<w:t>1.</w:t><w:t>08:05 Aksident trafiku me dëme materiale Audi dhe Kombi te Kisha Graqanic</w:t>
<!-- Checkboxes - 1 means it is checked -->
<w:t>Informuar:PK</w:t><w:checkBox><w:sizeAuto/><w:default w:val="1"/></w:checkBox>
<w:t>SHME</w:t><w:checkBox><w:sizeAuto/><w:default w:val="0"/></w:checkBox>
<w:t>SHZSH</w:t><w:checkBox><w:sizeAuto/><w:default w:val="0"/></w:checkBox>
<w:t>,Shërbimet tjera</w:t><w:checkBox><w:sizeAuto/><w:default w:val="0"/></w:checkBox>
在 python 中,我想从 .docx 文档生成的 xml 中选择包含复选框的值。我写了这样的代码:
WordNameSpace = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
para_tag = WordNameSpace + 'p'
text_tag = WordNameSpace + 't'
checkBox_tag = WordNameSpace + 'checkBox'
def get_docx_text(path):
document = zipfile.ZipFile(path)
xml_content = document.read('word/document.xml')
document.close()
tree = XML(xml_content)
paragraphs = []
for paragraph in tree.getiterator(checkBox_tag):
texts = [node.text for node in paragraph.getiterator(text_tag) if node.text]
if texts:
paragraphs.append(''.join(texts))
return paragraphs
results = get_docx_text('test.docx')
print results
当我打印 results 变量时,结果只是 [] ?为什么会这样?
【问题讨论】: