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

标签 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 包,希望他们的 tostring 方法与 Pretty_print 能够比默认的 xml 包产生更好的结果。

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

我的代码:

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

我尝试过使用 Pretty_print=True、tostringlist 和 tounicode。我只是在寻找此软件包中不存在的功能吗?

最佳答案

您需要一个能够理解 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))

关于python - 从 xml 中提取文本时保留换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32279927/

相关文章:

python - 需要 session 的单元测试 webapp2 RequestHandler

python - 我正在尝试使用 wgrib2 将 Lambert conformal GRIB 转换为纬度/经度坐标,以便导航程序可以读取数据

sql-server - 想要将 XML 存储在 SQL Server 2014 表中并在同一表上应用选择

python - 使用 Python 解析 XML - 访问元素

python - 参数化 django 的迁移以跳过(--fake 以编程方式)

python 在txt文件中换行

.net - 如何反序列化带有前缀命名空间但没有 ns 前缀元素的 XML 文档?

ruby - 将对象序列化为 JSON、XML、YAML?

python - 正确的 xpath 来卷起子项的文本

python - LXML - 删除元素的父元素,同时保留元素