Rename any .docx to .zip and open it. Inside is a small filesystem of XML documents, images and relationship files. Understanding that structure turns a Word document from an opaque blob into something you can inspect, repair, generate programmatically and audit for information you did not mean to send.
The structure
A .docx is a ZIP archive following the Office Open XML specification. word/document.xml holds the text, word/media/ holds every image at full resolution, docProps/ holds metadata, and _rels/ files describe how the pieces connect.
The archive layout
The relationship system
OOXML never references another part by path. It uses a relationship ID resolved through a _rels file:
The indirection buys real things. A part can be renamed or moved without touching every reference to it. External links โ a hyperlink to a website โ use the same mechanism as internal ones, so a consumer handles both identically. And a security boundary exists: an application can inspect every external target in one place before deciding whether to follow any of them.
โ ๏ธ Editing by hand means editing two files
Adding an image is not one operation. You must add the file to word/media/, add a <Relationship> entry with a new unique ID, reference that ID in document.xml, and ensure [Content_Types].xml declares the image's extension. Miss any one and Word reports the document as corrupt.
Inside document.xml
| Element | Means |
|---|---|
w:p | Paragraph |
w:r | Run โ a span sharing identical formatting |
w:t | Text |
w:pPr / w:rPr | Paragraph / run properties |
w:tbl, w:tr, w:tc | Table, row, cell |
w:br | Line or page break |
w:ins / w:del | Tracked insertion / deletion |
w:commentRangeStart | Comment anchor |
๐จ The run-splitting problem
Word splits text into a new run at every change of formatting โ and also at changes in spell-check state, language tagging, revision marks and proofing status. The result is that a visually continuous sentence is frequently spread across several runs:
A search-and-replace over the raw XML for "confidential" finds nothing. Any tool that redacts or substitutes text must normalise runs first โ merge adjacent runs with identical properties โ or it will silently miss matches. This is a well-known source of failed redaction in automated document processing.
What the document is carrying
That is a meaningful amount of information to attach to a document sent outside an organisation: who wrote it, who last touched it, when it started, how many revisions it went through, and how many hours were spent on it.
| What persists | Where |
|---|---|
| Author and last editor | docProps/core.xml |
| Total editing time, company | docProps/app.xml |
| Tracked changes with author names | word/document.xml |
| Comments and their authors | word/comments.xml |
| Full-resolution images | word/media/ |
| Text that was cropped from an image | word/media/ โ see below |
| Earlier versions of edited text | word/document.xml if changes are tracked |
๐จ Word crops are only a display setting
Cropping an image in Word does not modify the image. It records a display rectangle and stores the complete original in word/media/. Anyone who opens the archive sees everything that was cropped away.
The same applies to a black rectangle drawn over text to redact it โ the shape sits above the text and the text remains in document.xml, fully selectable. This has caused a long series of real disclosures in legal and government documents.
To actually remove content: crop in an image editor before inserting, delete the text rather than covering it, and run Document Inspector (File โ Info โ Check for Issues) before sending anything sensitive.
Recovering a corrupt document
This is one of the most useful practical consequences of the ZIP structure. When Word declares a file unrepairable, usually only one part is damaged โ the rest is intact and readable.
If document.xml itself is damaged, an XML parser will report the exact line and column of the failure โ frequently a truncated tag at the very end, which can be closed by hand. Repairing the last few characters of a 200,000-character file often restores the whole document.
Generating documents programmatically
Because the format is a documented ZIP of XML, producing Word documents from code needs no Word installation.
| Language | Library |
|---|---|
| Python | python-docx, docxtpl |
| JavaScript | docx, docxtemplater |
| Java | Apache POI |
| C# | Open XML SDK |
| PHP | PhpWord |
โ Template, do not construct
Building a document element by element produces valid XML and usually ugly output โ matching a designed template's styles, spacing and headers from code is a great deal of work.
The reliable approach is to have someone design the document in Word with placeholders, then substitute values into a copy. The formatting is already correct because a person made it correct, and the code only handles data.
One caveat, and it is the run-splitting problem again: a placeholder like {{name}} may be split across runs by Word's proofing state. Template libraries handle this by normalising runs first โ which is exactly why writing your own substitution with a regular expression fails intermittently and confusingly.
The same structure elsewhere
| Format | Main content file | Notable |
|---|---|---|
.xlsx | xl/worksheets/sheet1.xml | Text lives in xl/sharedStrings.xml, referenced by index |
.pptx | ppt/slides/slide1.xml | One file per slide, plus layouts and masters |
.odt | content.xml | OpenDocument โ flatter, arguably cleaner |
.epub | OEBPS/content.opf | Chapters are ordinary HTML files |
The sharedStrings.xml design in Excel is worth noting: every distinct string in the workbook is stored once, and cells reference it by index. A spreadsheet with a hundred thousand rows of repeated category names stores each name once. It is a small piece of deduplication that makes a large difference on real data.
Working with document formats?
Convert between Markdown, HTML and other formats, or merge and split PDFs โ all in your browser, nothing uploaded.
Open the Markdown Converter โSummary
- A .docx is a ZIP of XML. Rename a copy and open it.
- Parts reference each other by relationship ID, resolved through
_relsfiles. - Text is split into runs at every formatting and proofing boundary, which breaks naive search and replace.
- Images are stored at full resolution regardless of displayed size โ usually the reason a document is large.
- Word crops and black boxes hide nothing. The original data is in the archive.
- Metadata includes author, editor, revision count and editing time. Run Document Inspector before sending.
- Corrupt documents are frequently recoverable by extracting the archive directly.
- Template rather than construct when generating documents from code.
Frequently Asked Questions
Is a .docx file really a ZIP archive?
Yes. Rename a copy to .zip and any archiver will open it. Inside you will find XML files describing the text and formatting, a media folder containing every image at full resolution, and property files holding metadata. The same is true of .xlsx, .pptx, .odt and .epub.
How do I recover text from a corrupt Word document?
Rename a copy to .zip, open it, and read word/document.xml in a text editor โ the text is there among the tags. Every image is in word/media/ as an ordinary file. This frequently recovers content from documents Word itself declares unrepairable, because only one part of the archive is usually damaged.
Why does searching document.xml for a phrase often fail?
Because Word splits text into runs at every formatting boundary, and also at points where spell-check state, language tagging or revision marks change. A sentence that looks continuous can be split across several run elements with tags between the words, so a plain text search for the whole phrase finds nothing.
What metadata does a Word document contain?
docProps/core.xml holds the author, last modified by, creation and modification times and revision count. docProps/app.xml adds the total editing time, word and page counts, the application version and often a company name. Tracked changes and comments persist in document.xml until explicitly removed.
Why is my Word document so large?
Almost always images. Word stores pasted pictures in word/media/ at full resolution regardless of the size they are displayed at, so a screenshot scaled down on the page still occupies its original bytes. Open the archive and check the folder โ it is usually the overwhelming majority of the file.