从xml中提取文本时保留换行符

时间:2015-08-28 21:20:28

标签: python xml lxml

我的XML(从.docx中提取):

<w:p>
  <w:pPr>
    <w:pStyle w:val="Normal"/>
    <w:rPr/>
  </w:pPr>
  <w:r>
    <w:rPr/>
    <w:t>0 things and stuff</w:t>
  </w:r>
</w:p>
<w:p>
  <w:pPr>
    <w:pStyle w:val="Normal"/>
    <w:rPr/>
  </w:pPr>
  <w:r>
    <w:rPr/>
    <w:t>1 things and stuff</w:t>
  </w:r>
</w:p>

期望的输出:

0 things and stuff
1 things and stuff

实际输出:

0 things and stuff1 things and stuff

我尝试使用lxml包,希望他们使用pretty_print的tostring方法比默认的xml包产生更好的结果。

在研究这个问题时,我发现在tostring中使用method ='text'导致所有格式都丢失的解释。

我的代码:

tree = etree.fromstring(xml_content)
docx_text = etree.tostring(tree, method='text')

我尝试过使用pretty_print = True,tostringlist和tounicode。我只是在寻找这个包中不存在的功能吗?

1 个答案:

答案 0 :(得分:3)

您需要一个能够理解docx xml语义的所有业务逻辑的解析器,例如:因为两个文本行在不同的段落中,所以它们应该在不同的行中显示。

您可以自己尝试这样做,但我建议您使用类似docx的内容 - 或者至少查看源代码中的getdocumenttext()函数,了解一下此方法

import os
from docx import getdocumenttext
from lxml import etree

# get `xml_content` from word doc...    

tree = etree.fromstring(xml_content)
paragraphs = getdocumenttext(tree)
print(os.linesep.join(paragraphs))
# Result: 
# 0 things and stuff
# 1 things and stuff

更新:有关完全可重复的示例,请参阅下文

import os
from docx import getdocumenttext, opendocx
from lxml import etree

## load the xml tree from word document ##
# EITHER:
tree = opendocx('/path/to/my/file.docx')

# OR
xml_content = """<?xml version="1.0" encoding="utf-8"?>
<w:document mc:Ignorable="w14 w15 wp14" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
 <w:body>
  <w:p>
   <w:r>
    <w:t>0 things and stuff</w:t>
   </w:r>
  </w:p>
  <w:p>
   <w:r>
    <w:t>1 things and stuff</w:t>
   </w:r>
  </w:p>
 </w:body>
</w:document>
"""
tree = etree.fromstring(xml_content)
##

paragraphs = getdocumenttext(tree)
print(os.linesep.join(paragraphs))