出现在单独节点中的句子单词,Elementtree

时间:2014-09-14 09:38:55

标签: python xml python-2.7 docx elementtree

基本上我创建了一个word文档来检查XML中的解析是如何进行的。我做了:

import xml.etree.ElementTree  
import zipfile as zf  
z = zf.ZipFile("INTRODUCTION.docx")  
doc_xml = z.open("word/document.xml")  

tree = ET.parse(doc_xml)

NAMESPACE_PREFIXES = {
    'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'
    }  


text_elements = [element for element in tree.iter() if element.tag == 
'{' + NAMESPACE_PREFIXES['w'] + '}t']
for node in text_elements:
    print node.text  

命名空间前缀用于处理这些链接,以便忽略它们。 node.text打印为:

INTRODUCTION
This is a test document for xml
.
Lets
 see how this works.
Conclusion
It should hopefully
..

在我的原始文档中,Lets see how this works出现在一行中,同样地,我看到同一个句子的完整句点出现在单独的节点中(如“..”)。我该如何解决?这是xml代码:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
    <w:body>
        <w:p w:rsidR="00470EEF" w:rsidRDefault="00456755"><w:pPr><w:rPr><w:b/></w:rPr></w:pPr><w:r w:rsidRPr="00456755"><w:rPr><w:b/></w:rPr><w:t>INTRODUCTION</w:t></w:r></w:p>
        <w:p w:rsidR="00456755" w:rsidRDefault="00456755"><w:r w:rsidRPr="00456755"><w:t>This is a test document for xml</w:t></w:r><w:r><w:t>.</w:t></w:r></w:p>
        <w:p w:rsidR="00456755" w:rsidRDefault="00456755"><w:proofErr w:type="spellStart"/><w:proofErr w:type="gramStart"/><w:r><w:t>Lets</w:t></w:r><w:proofErr w:type="spellEnd"/><w:proofErr w:type="gramEnd"/><w:r><w:t xml:space="preserve"> see how this works.</w:t></w:r></w:p>
        <w:p w:rsidR="00456755" w:rsidRDefault="00456755"/>
        <w:p w:rsidR="00456755" w:rsidRDefault="00456755"/>
        <w:p w:rsidR="00456755" w:rsidRDefault="00456755"><w:pPr><w:rPr><w:b/></w:rPr></w:pPr><w:r w:rsidRPr="00456755"><w:rPr><w:b/></w:rPr><w:t>Conclusion</w:t></w:r></w:p>
        <w:p w:rsidR="00456755" w:rsidRPr="00456755" w:rsidRDefault="00456755"><w:r w:rsidRPr="00456755"><w:t>It should hopefully</w:t></w:r><w:r><w:t>..</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p>
        <w:sectPr w:rsidR="00456755" w:rsidRPr="00456755"><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="708"/><w:docGrid w:linePitch="360"/></w:sectPr>
    </w:body>
</w:document>

我注意到w:type="spellStart""grasmStart"之类的内容,这就是Lets出现在不同节点中的原因。有没有办法看一下这个?

1 个答案:

答案 0 :(得分:1)

print语句在您打印的字符串后添加换行符。

您需要按p代码对代码进行分组:查找p代码,并在其中找到t代码。

...
w = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'    
for p in tree.findall('.//{' + w + '}p'):
    print ''.join(t.text for t in p.findall('.//{' + w + '}t'))

输出:

INTRODUCTION
This is a test document for xml.
Lets see how this works.


Conclusion
It should hopefully..