Files
docx/docs/usage/styling-with-xml.md
T
DolanandClaude Opus 5.5 c169dd1a83 feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded (#3556)
* feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded

The docs' code examples were plain snippets, often fragments that couldn't run. A code block marked ```ts live is now
an example readers can see and change: the page runs it with docx, draws the document it makes next to the code with
docx-preview, and offers the .docx to download. Clicking the code opens an editor (Monaco) that knows docx's types,
the pages are drawn again as the code changes, and View closes the editor. Split, Code and Preview show the code and
the pages side by side, or one of them. The demos a page includes are live too.

Every example in the docs that builds or patches a document is now live: 443 of them, each a whole file with its
imports, so it runs as written. Code about other things, such as installing, bundlers or servers, stays plain.

Running them showed docs that were wrong, fixed here:
- Landscape: docx swaps a landscape page's width and height itself, so the docs' swapped sizes made a tall page
  marked as landscape
- Comments: `comments.children` takes plain objects, not `new Comment(...)`
- Custom properties are text only
- APIs that no longer exist: MathText, Row, `.then` on Packer.toStream, patchDocument(buffer, ...),
  createParagraph().heading1(), a border's `value`, TextRun's `.break()`
- The templates and patcher examples read files that aren't in the repository. They now patch the templates in
  demo/assets, or make their own

`npm run build.docs` copies docx's browser builds into docs/live-examples/lib, so the docs use the docx they
describe, and the Pages workflow runs it. The folder is gitignored, and cspell and ESLint skip it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* docs: the bullet examples draw a bullet, and the patcher's usage writes the patched document

"ὠ" is U+1F60, a Greek omega, since \u takes four hex digits, so the two live bullet examples on
the numbering page drew it as each item's bullet. They use "•" now. The patcher's usage example
now awaits patchDocument and writes the document it makes, as the example after it does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 13:00:29 +01:00

2.9 KiB

Styling with XML

Setup

  1. Create a new word document in Microsoft Word

  2. Customize the styles on the Ribbon Bar. For example, modify the Normal, Heading 1, Heading 2 like so:

    image image

    Note: Font and color selection from the theme are currently not supported.

  3. You can even create a totally new Style:

    image image

    Note: When selecting the style type, it is important to consider the component being used.

  4. Save

  5. Re-name the saved .docx file to .zip and un-zip

  6. Find styles.xml

    image

Usage

Read the styles using fs, and put it into the Document object in the constructor:

import { Document, Paragraph } from "docx";
import * as fs from "fs";

const styles = fs.readFileSync("./demo/assets/custom-styles.xml", "utf-8");
const doc = new Document({
    title: "Title",
    externalStyles: styles,
    sections: [
        {
            children: [new Paragraph("Some normal text, in the Normal style of styles.xml")],
        },
    ],
});

Your styles take the place of the library's default styles with the same ids, and your document defaults take the place of the library's. The library's default styles fill in the ones your styles.xml leaves out, such as Hyperlink or FootnoteText. A default style you set in styles.default, such as heading1 or document, takes the place of yours.

You can use paragraphs, HeadingLevel.HEADING_1, HeadingLevel.HEADING_2 etc and it will be styled according to your styles.xml created earlier. You can even use your new style you made with the style option:

import { Document, HeadingLevel, Paragraph } from "docx";
import * as fs from "fs";

const styles = fs.readFileSync("./demo/assets/custom-styles.xml", "utf-8");
const doc = new Document({
    title: "Title",
    externalStyles: styles,
    sections: [
        {
            children: [
                new Paragraph({
                    text: "Cool Heading Text",
                    heading: HeadingLevel.HEADING_1,
                }),
                new Paragraph({
                    text: 'This is a custom named style from the template "MyFancyStyle"',
                    style: "MyFancyStyle",
                }),
                new Paragraph("Some normal text"),
            ],
        },
    ],
});

Example: https://github.com/dolanmiu/docx/blob/master/demo/styles/xml-styles.ts