我们常用的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。
如果文档当中有加入了页眉或页脚,那么每个页眉页脚文件也拥有自己的word\_rels\footer.xml.rels
或word\_rels\header.xml.rels
,用于指定页眉页脚与其他文件的依赖关系
文档处理中的常见单位:
pt :主要用于字体大小和排版尺寸,如边距和间距的设定。1 pt= 1/72 inch
twip :twip 是 pt 的更精细单位,用于文档布局中的精确测量。 1 twip = 1/20 pt
emu:图像、图形对象的尺寸和位置定义。在 Office Open XML(如 .docx、.pptx)格式中,图像和形状的位置和尺寸经常以 emu 为单位。 1 pt = 12700emu
参考:
office文档结构解析
office 复合文档数据结构解析“初探”
Anatomy of a WordProcessingML File