2017-09-06 149 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