【问题标题】:Words of a sentence appearing in separate nodes, Elementtree出现在单独节点中的句子的单词,Elementtree
【发布时间】:2014-09-14 09:38:54
【问题描述】:

基本上,我创建了一个 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 出现在不同节点中的原因。有没有办法查看这个?

【问题讨论】:

    标签: python xml python-2.7 docx elementtree


    【解决方案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..
    

    【讨论】:

    • 谢谢,您能解释一下实际发生的情况以及如何解决它..?
    • @Swordy,我想我在您发表评论时正在编辑答案(添加解释)。请再次查看答案。
    • @Swordy,对于传递给findall 的表达式,请参见xml.etree.ElementTree documentation - XPath Support
    • 我们可以添加条件 w.r.t 文本的属性,如颜色或字体类型吗?
    • @Swordy,如果您点击我在上一条评论中提供的链接,您会发现 XPath 语法类似于 [@attrib='value']。如果你需要更复杂的条件,你最好看看lxml,它支持更多的XPath语法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多