A .docx file is a ZIP archive that follows the Open Packaging Conventions
(OPC), Part 2 of ECMA-376 and ISO/IEC 29500. Each ZIP member is a part. Parts do
not find each other by name: a part’s relationships say which others it uses, and
[Content_Types].xml says what each part is.
Parts and part names
A part name is the member’s path with a leading /, such as
/word/document.xml. Part names are compared without regard to the case of
ASCII letters, and may contain percent-encoded characters. ZIP folders have no meaning in
a package: only the parts do.
Word keeps a conventional layout, but other writers may use other names, so a reader finds the parts through the relationships and not by name. The usual parts of a document are:
| Part | Holds |
|---|---|
[Content_Types].xml | The media type of every other part. |
_rels/.rels | The package’s relationships: the main document and the properties. |
word/document.xml | The main document: text, tables, section properties. See document.xml. |
word/_rels/document.xml.rels | The main document’s relationships. |
word/styles.xml | Styles. |
word/numbering.xml | Numbering definitions for lists and headings. |
word/settings.xml, word/webSettings.xml, word/fontTable.xml, word/theme/theme1.xml | Settings, fonts and the theme. |
word/footnotes.xml, word/endnotes.xml | Footnotes and endnotes. |
word/comments.xml | Comments. |
word/header1.xml, word/footer1.xml | Headers and footers. |
word/media/ | Pictures and other media. |
word/embeddings/ | Embedded files: other Office documents and OLE objects. |
word/vbaProject.bin | The macro project of a .docm or .dotm file. |
word/glossary/document.xml | Building blocks: reusable pieces of content, with its own styles, settings and fonts. |
customXml/item1.xml | XML data stored with the document, often for content controls. |
docProps/core.xml, docProps/app.xml, docProps/custom.xml | Document properties. |
docProps/thumbnail.jpeg | A small picture of the first page, when one was saved. |
[Content_Types].xml
This part, at the root of the archive, gives the media type of every part. A
Default element gives the type for an extension. An Override
gives the type of one part and takes priority over a Default.
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
<Default Extension="png" ContentType="image/png"/>
<Override PartName="/word/document.xml"
ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
<Override PartName="/word/styles.xml"
ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>
</Types>
The main part’s content type tells the four kinds of Word file apart:
| Extension | Main part content type |
|---|---|
.docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml |
.docm | application/vnd.ms-word.document.macroEnabled.main+xml |
.dotx | application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml |
.dotm | application/vnd.ms-word.template.macroEnabledTemplate.main+xml |
Other WordprocessingML parts have types of the form
application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml:
styles, settings, webSettings,
fontTable, numbering, footnotes,
endnotes, comments, header and
footer. The glossary document is
…wordprocessingml.document.glossary+xml.
Relationships
A part’s relationships are in a part beside it: the relationships of
/word/document.xml are in /word/_rels/document.xml.rels. Those
of the package itself are in /_rels/.rels.
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
Target="styles.xml"/>
<Relationship Id="rId2"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
Target="https://www.example.com/" TargetMode="External"/>
</Relationships>
| Attribute | Meaning |
|---|---|
Id | Unique within this relationships part. XML in the source part refers to it, as r:id="rId1". |
Type | A URI naming the kind of relationship. |
Target | The target part, relative to the source part’s folder, or absolute if it starts with /. For an external target, a URI. |
TargetMode | Internal (the default) or External: the target is outside the package. |
The relationship types used in Word files, all with the prefix
http://schemas.openxmlformats.org/officeDocument/2006/relationships/
unless shown otherwise:
| Type | From | To |
|---|---|---|
officeDocument | Package | The main document. |
http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties | Package | Core properties. |
extended-properties | Package | Application properties. |
custom-properties | Package | Custom properties. |
http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail | Package | Thumbnail image. |
styles, numbering, settings, webSettings, fontTable, theme | Main document | The part of that kind. |
header, footer | Main document | One header or footer. |
footnotes, endnotes, comments | Main document | The notes or comments part. |
image | Any part with text | A picture. May be external. |
hyperlink | Any part with text | A link target, usually external. |
oleObject, package | Any part with text | An embedded object or embedded Office file. |
customXml | Main document | A custom XML data part. |
glossaryDocument | Main document | The building blocks document. |
attachedTemplate | Settings | The template the document was based on, usually external. |
http://schemas.microsoft.com/office/2006/relationships/vbaProject | Main document | The macro project. |
Because the target of a relationship can be any URI, a document can point outside itself: a linked picture, a remote template. The viewer on this site lists such targets and never fetches them.
Package rules in detail
These rules come from ECMA-376 Part 2 (Open Packaging Conventions), clauses 6.2.2, 6.5.3 and 7.2.3 to 7.3.
| Subject | Rule |
|---|---|
| Part name syntax (6.2.2.2) | One or more segments, each a / followed by a non-empty IRI segment. A segment must not contain a percent-encoded / or \, must not contain percent-encoded unreserved characters, and must not end with a dot. /_rels/.rels is reserved, and so is any part whose last segment ends in .rels and whose second-to-last segment is _rels. |
| Part name equality (6.2.2.3) | ASCII case-insensitive. Two parts may not have equivalent names, and no part name may be derivable from another by adding / and more text: a package cannot hold both /a and /a/b. Names that differ only by Unicode normalisation (NFC or NFD) should not both appear. |
| ZIP item names (7.3.3 to 7.3.5) | ASCII only. The item name is the part name without the leading /, with every non-ASCII character percent-encoded. A reader reverses this: decode, then add the /. |
| ZIP limits (7.3.6) | A central directory file header must not exceed 65 535 bytes. ZIP encryption is not allowed, and no compression algorithm other than Deflate is allowed. |
| Media Types stream (7.2.3.2, 7.3.7) | An item named [Content_Types].xml, with a Types root and Default and Override children. Every part except relationships parts needs a matching Default or Override. At most one Default per extension and one Override per part name. Order does not matter. Extension and part name are compared ASCII case-insensitively. |
| Finding a media type (7.2.3.5) | Look for an Override with the part name first. If none, use the Default for the extension after the last dot of the last segment. |
| Relationship attributes (6.5.3.4) | Id (unique in the part, an xsd:ID), Type and Target are required. TargetMode is optional and defaults to Internal. Relationship types are compared as plain case-sensitive strings. |
| Relationship targets (6.5.3.4, 6.5.4) | An internal target is a relative reference to a part, resolved against the source part’s own name; for the package relationships part the source is the package root. An external target is a relative reference or an absolute IRI, resolved against the location of the package. Two relationships may share a target if their Id values differ (6.5.4.5). |
Divergence from spec
Part 2 was read for this section. Apache POI, docx4j and LibreOffice packaging code has not been compared line by line; python-docx has (below). The explorer’s handling of packages without a readable central directory (see below) is not described by Part 2, which maps parts to ZIP items (7.3.1 to 7.3.5) and says nothing about recovery.
- python-docx (
docx/opc/packuri.py,pkgreader.py,phys_pkg.py, master, read). A part name must begin with a slash and nothing else is checked (packuri 20–24), so the rules of Part 2 8.1.1 on empty segments, a trailing slash, a trailing dot or percent-encoding are not enforced. A relative target is resolved by joining it to the source part’s directory and normalising withposixpath.abspath(26–31); a target that climbs above the root is clamped at the root, not rejected. The extension is whatever follows the last dot of the file name; the content type is theOverridefor the part name if there is one, else theDefaultfor the extension, else aKeyErrorends the load (pkgreader 95–105), so a part without a content type is fatal. Both lookups use a case-insensitive dictionary. The package is read by walking relationships from the package relationships part, so a part that nothing relates to is never loaded (pkgreader 64–83), a part reached twice is read once, and external relationships are skipped. The ZIP is read with Python’szipfile: a member is looked up by its exact name (phys_pkg 78–83), so a relationship target whose case differs from the stored name fails; a missing relationships item means no relationships (94–101); a directory of an unpacked package is also accepted (35–68). Nothing in this code reads the interleaved or split forms of Part 2 7.3.5 beyond whatzipfiledoes, and no digital signature ormc:AlternateContentis looked at.
Reading a package
- Read the ZIP central directory and find
[Content_Types].xml. - Read
_rels/.relsand follow theofficeDocumentrelationship to the main part. - Check that part’s content type, and that its root element is
w:document. - Read the main part’s relationships for its styles, numbering, headers, pictures and so on, and follow only those it uses.
Members of a package are normally Deflate-compressed or stored, and the package format
does not encrypt anything. A password-protected Word file is not a ZIP archive at all: it
is a Compound File that holds the encrypted package in a stream named
EncryptedPackage.
Encrypted packages
The compound file’s EncryptionInfo stream starts with a version, major then minor
(16 bits each). Version 4.4 is agile encryption: four reserved flag bytes, then an XML
description with keyData (cipher, chaining mode, key bits, hash) and the password
key encryptor’s encryptedKey (spin count). Versions 2.2, 3.2 and 4.2 are standard
encryption: flags, the size of the header, the header (flags, algorithm id such as 0x660E for
AES-128, 0x660F AES-192, 0x6610 AES-256, 0x6801 RC4, hash id, key size, provider, provider
name) and the password verifier. Versions 3.3 and 4.3 are extensible encryption, a named
provider. EncryptedPackage begins with the 8-byte length of the package once
decrypted, then the enciphered ZIP archive. The explorer shows these fields and says the file is
encrypted; it never asks for a password and does not decrypt.
Damaged and cut-short packages
If the ZIP central directory is missing or wrong, the explorer finds the members from their local headers. When one of them is the main document part the file opens as a Word document and the damage appears in the issues list; if no Word part is found it is not accepted as a Word document and the ZIP viewer shows it instead.
The ZIP structure itself is described on the ZIP archive pages.