Office文档结构

我们常用的Office Word格式分为doc和docx两种格式,doc是Word 97-2003版本使用的格式,Word2007及其之后的版本,默认格式是docx。

doc使用的是office 97-03的存储规范:OLE。它是一种对象链接和嵌入的技术,该技术可以包含文本,图形,电子表格甚至其他二进制数据。
docx使用OpenXML(OOXML)协议,它是微软在Office 2007中提出的一种新的文档格式,Office 2007及其以后的Word、Excel、PowerPoint默认均采用OpenXML格式。

一个只包含一张图片的docx文件,使用zip解压到文件夹中,他的目录结构如下:
在这里插入图片描述
在这里插入图片描述

[Content_Types].xml:[Content_Types].xml 文件的作用就是规定整个压缩包内不同类型文件的处理方式。具体来说,它定义了包内各个文件的内容类型(MIME类型),使得应用程序能够正确识别和处理包内的不同文件。

这个文件描述的是整个文档内容的类型,把各个xml文件组合成一个整体。以下面的[Content_Types].xml为例,

<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="png" ContentType="image/png"/>
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
<Override PartName="/docProps/custom.xml" ContentType="application/vnd.openxmlformats-officedocument.custom-properties+xml"/>
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
<Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"/>
<Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/>
<Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>
<Override PartName="/word/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>
</Types>

默认内容类型 (Default)覆盖内容类型 (Override)的区别:默认内容类型 (Default)定义文件扩展名的默认内容类型。当包中存在具有特定扩展名的文件时,应用该默认内容类型。覆盖内容类型 (Override)为特定路径的文件定义内容类型,覆盖默认内容类型定义。即使文件的扩展名与某个 元素匹配, 元素也可以为该特定文件定义不同的内容类型。

1、根元素 (Types): 这是定义包内内容类型的根元素。

命名空间: http://schemas.openxmlformats.org/package/2006/content-types

2、默认内容类型 (Default):

Extension: 文件扩展名。
ContentType: 对应扩展名的内容类型。

<Default Extension="png" ContentType="image/png"/>
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>

docProps文件夹:这个文件夹中的xml记录了docx文档的主要属性信息

_rels 文件夹存放了所有指定的rels文件,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
    <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
    <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
    <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties" Target="docProps/custom.xml"/>
</Relationships>

这个 XML 文件定义了 .docx 文件包内不同部件之间的关系:

word/document.xml: 主文档内容。
docProps/core.xml: 核心属性文件,包含如标题、作者、创建日期等元数据。
docProps/app.xml: 扩展属性文件,包含如应用程序特定的元数据(如总页数、总字数等)。
docProps/custom.xml: 自定义属性文件,包含用户定义的额外元数据。
通过这些关系定义,应用程序可以正确地找到和处理这些文件,从而确保文档内容和属性的完整性和一致性。

docx文件的解析流程
读取[Content_Types].xml 文件,获得所有文件的类型;
读取 _rels\.rels 这个Relationship 文件,获取document.xml 文件的位置,即 word\document.xml;
读取 word\document.xml 文件以及其关联的 Relationship 文件word\_rels\document.xml.rels,得到该Word所有文件的存储位置,如word中插图所在文件夹为word\media。

参考:

office文档结构解析
office 复合文档数据结构解析“初探”
Anatomy of a WordProcessingML File

  • 13
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值