2017-09-06 37 views
0

我正在使用python 3.5如何将xml.etree.ElementTree中的XML内容写入文件?

为了从word文档中提取文本内容,我使用了xml.etree.ElementTree。 我无法将生成的XML内容写入其他文件。

下面是我的一段代码

import zipfile 
import xml.etree.ElementTree 
with zipfile.ZipFile('<path to docx file>') as docx: 
    tree = xml.etree.ElementTree.XML(docx.read('word/document.xml')) 

我已经尝试了两种方法: tree.write('<path to file>', encoding='utf8')

xml.etree.ElementTree.write('<path to file>') 

但是这两种方法都引发错误为:

AttributeError的: 'xml.etree.ElementTree.Element'对象没有属性'写'

请帮助。

回答

0

是generaly ElementTree的对象,而是其子根元素。

正在加载xml.etree.ElementTree.parse()会返回一个合适的ElementTree。

# XML() -> returns root Element 
root = xml.etree.ElementTree.XML(docx.read('word/document.xml')) 
print(root) 
<Element '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}document' at 0x7f5f117ae5d0> 


# parse() -> returns tree ElementTree 
tree = xml.etree.ElementTree.parse(docx.open('word/document.xml')) 
print(tree) 
<xml.etree.ElementTree.ElementTree object at 0x7f5f11805890> 

root = tree.getroot() 
print(root) 
<Element '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}document' at 0x7f5f11805950> 

然后:

import zipfile 
import xml.etree.ElementTree 

with zipfile.ZipFile('<path to docx file>') as docx: 
    tree = xml.etree.ElementTree.parse(docx.open('word/document.xml')) 

tree.write('<path to file>') 
+0

它的工作原理。谢谢。 – Bonson