从 Word 中提取结构化内容 XML

Extracting structured content from Word XML

我想从 Word 文档中提取文本内容,同时保留文档结构。 This question comes closest and provides the starting point. Here is a dummy word document. 我已经解压缩并包含下面的 document.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 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: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 w14:paraId="09AE8A7D" w14:textId="338F199D" w:rsidR="00D8348A" w:rsidRPr="00987637" w:rsidRDefault="00987637" w:rsidP="00987637"><w:pPr><w:pStyle w:val="Title"/><w:rPr><w:sz w:val="40"/><w:szCs w:val="40"/></w:rPr></w:pPr><w:r w:rsidRPr="00987637"><w:rPr><w:sz w:val="40"/><w:szCs w:val="40"/></w:rPr><w:t xml:space="preserve">The </w:t></w:r><w:r w:rsidR="00A6772E"><w:rPr><w:sz w:val="40"/><w:szCs w:val="40"/></w:rPr><w:t>Example Docx</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p><w:p w14:paraId="0803A50C" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637"/><w:p w14:paraId="0EDF635A" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637" w:rsidP="00987637"><w:pPr><w:pStyle w:val="Heading2"/></w:pPr><w:r><w:t>Introduction</w:t></w:r></w:p><w:p w14:paraId="2AF32AB2" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637"/><w:p w14:paraId="4DB9AE94" w14:textId="6C08452A" w:rsidR="00987637" w:rsidRPr="00987637" w:rsidRDefault="00C52D6C" w:rsidP="00987637"><w:r><w:t>This is the introduction para.</w:t></w:r><w:r w:rsidR="00A6772E" w:rsidRPr="00A6772E"><w:t xml:space="preserve"> </w:t></w:r><w:r w:rsidR="00A6772E"><w:t>Awesome.</w:t></w:r></w:p><w:p w14:paraId="676B3B48" w14:textId="77777777" w:rsidR="00987637" w:rsidRPr="00987637" w:rsidRDefault="00987637" w:rsidP="00987637"/><w:p w14:paraId="17CE18FE" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637" w:rsidP="00987637"><w:pPr><w:pStyle w:val="Heading2"/></w:pPr><w:r><w:t>Chapter 1</w:t></w:r></w:p><w:p w14:paraId="58A187F6" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637"/><w:p w14:paraId="2815D3B5" w14:textId="58856A78" w:rsidR="00C52D6C" w:rsidRPr="00987637" w:rsidRDefault="00C52D6C" w:rsidP="00C52D6C"><w:r><w:t xml:space="preserve">This is the </w:t></w:r><w:r><w:t xml:space="preserve">chapter 1 intro </w:t></w:r><w:r><w:t>para.</w:t></w:r><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r w:rsidR="00A6772E"><w:t>Awesome.</w:t></w:r></w:p><w:p w14:paraId="7010E2CD" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637" w:rsidP="00987637"/><w:p w14:paraId="52D9B766" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637" w:rsidP="00987637"><w:pPr><w:pStyle w:val="Heading3"/></w:pPr><w:r><w:t>Chapter 1.1</w:t></w:r></w:p><w:p w14:paraId="055DB7AC" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637" w:rsidP="00987637"/><w:p w14:paraId="22A2C473" w14:textId="1D3FE489" w:rsidR="00A6772E" w:rsidRPr="00987637" w:rsidRDefault="00A6772E" w:rsidP="00A6772E"><w:r><w:t xml:space="preserve">This is </w:t></w:r><w:r><w:t>section 1.1</w:t></w:r><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>text</w:t></w:r><w:r><w:t>. Awesome.</w:t></w:r></w:p><w:p w14:paraId="1F60E111" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637" w:rsidP="00987637"/><w:p w14:paraId="62A6FEE6" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637" w:rsidP="00987637"><w:pPr><w:pStyle w:val="Heading3"/></w:pPr><w:r><w:t xml:space="preserve">Chapter </w:t></w:r><w:r><w:t>1.2</w:t></w:r></w:p><w:p w14:paraId="3B8DA076" w14:textId="77777777" w:rsidR="00987637" w:rsidRPr="00987637" w:rsidRDefault="00987637" w:rsidP="00987637"/><w:p w14:paraId="43519FB7" w14:textId="311F726C" w:rsidR="00987637" w:rsidRDefault="00A6772E" w:rsidP="00987637"><w:r><w:t>This is section 1.</w:t></w:r><w:r><w:t>2</w:t></w:r><w:r><w:t xml:space="preserve"> text. Awesome.</w:t></w:r></w:p><w:p w14:paraId="2D6908F2" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637" w:rsidP="00987637"/><w:p w14:paraId="3189011C" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637" w:rsidP="00987637"><w:pPr><w:pStyle w:val="Heading2"/></w:pPr><w:r><w:t>Chapter 2</w:t></w:r></w:p><w:p w14:paraId="6FFC56CD" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637" w:rsidP="00987637"/><w:p w14:paraId="3E9AE930" w14:textId="329881E3" w:rsidR="00987637" w:rsidRPr="00987637" w:rsidRDefault="00A6772E" w:rsidP="00987637"><w:r><w:t xml:space="preserve">This is </w:t></w:r><w:r><w:t>the c</w:t></w:r><w:r><w:t xml:space="preserve">hapter </w:t></w:r><w:r><w:t>2</w:t></w:r><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t xml:space="preserve">text. </w:t></w:r><w:r><w:t>Awesome.</w:t></w:r></w:p><w:p w14:paraId="57F0B3DF" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637"/><w:p w14:paraId="48315F42" w14:textId="77777777" w:rsidR="00987637" w:rsidRDefault="00987637"/><w:sectPr w:rsidR="00987637" w:rsidSect="00751697"><w:pgSz w:w="11900" w:h="16840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/><w:cols w:space="720"/><w:docGrid w:linePitch="360"/></w:sectPr></w:body></w:document>

这在 xml 中读取:

library(xml2)
library(magrittr)
doc = read_xml('document.xml')

我不清楚从中提取内容的后续步骤。我试过了:

xml_find_all(doc, "/w:body/w:p/w:hyperlink/w:r/w:t") %>% xml_text()

但这returns没什么。感谢您的帮助。我的目标是以某种格式导出文本内容,以保留文档结构 - 也许是一个 csv,其列反映了从 TitleHeading 1 等到 Normal.

的树

----------------------------------------\

编辑:

示例文档结构如我所见:

Title
-- Heading 2
-- -- para
-- -- Chapter 1
-- -- -- para
-- -- -- Chapter 1.1
-- -- -- -- para
-- -- -- Chapter 1.2
-- -- -- -- para
-- -- Chapter 2
-- -- -- para

老实说,我在输出方面很灵活(json 很好),但它需要反映这种大致的文档结构。

您可能严重低估了任务的复杂性。

XPath 用于 selection。编写 XPath 表达式以从 OOXML 文档中提取一组节点将是一件简单的事情。例如,这个 XPath 2.0 表达式,

//w:p[w:pPr/w:pStyle/@w:val='Heading2']/string()

将 return 用于您的文档:

Introduction
Chapter 1
Chapter 2

如果你想做的不止select,如果你想变换,那么您将需要升级到 XSLT。 您还必须深入研究 5K+ 页的 OOXML 规范,以处理可以在 Word 文档中完成的所有工作。请注意,缩进级别,因为它们可以来自样式,所以通常很难覆盖。

XPath 对于 selection 来说非常强大。我将再为您提供一个 XPath 2.0 表达式,

//w:p[normalize-space()]/normalize-space()

您的文档将 return、

The Example Docx
Introduction
This is the introduction para. Awesome.
Chapter 1
This is the chapter 1 intro para. Awesome.
Chapter 1.1
This is section 1.1 text . Awesome.
Chapter 1.2
This is section 1. 2 text. Awesome.
Chapter 2
This is the c hapter 2 text. Awesome.

这似乎非常接近您几乎不需要任何开发工作的需求。如果您限制输入 space,您可能能够比一般情况更轻松地恢复层次结构,但处理一般情况非常复杂。

最后我采用了不同的方法,使用 pandoc - 使用 pandoc -s example.docx -o example.html 转换为 html。这会保留格式和链接等,并提供更清晰的输出。