Files
docx/docs/usage/styling-with-xml.md
T
DolanandClaude Opus 5.5 e0ff13ee3d chore: organise the demos into a folder for each topic (#3552)
* chore: organise the demos into a folder for each topic

The demos were one flat list of 124 numbered files, which made it hard to
find the one you wanted. Each is now in a folder for its topic, such as
demo/tables/basic-table.ts, with a name that says what it shows instead of
a number.

npm run demo lists every demo and narrows the list as you type. It also
takes a demo's path or name, a topic, or search words.

The demos' images moved to demo/assets/images, as images is now a topic.
The docs' links, the Demos workflow, the shape render script and its
reference images use the new paths and names.

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

* fix: list only a topic's own demos, and drop stale output file names

npm run demo text also offered the text-boxes demos and others with
"text" in their name, so a topic now searches for "text/". Three demos
logged their old numbered file name after writing My Document.docx.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

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

2.2 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:

const styles = fs.readFileSync("./styles.xml", "utf-8");
const doc = new docx.Document({
    title: "Title",
    externalStyles: styles,
});

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, heading1(), heading2() etc and it will be styled according to your styles.xml created earlier. You can even use your new style you made by calling the style method:

doc.createParagraph("Cool Heading Text").heading1();

const paragraph = new docx.Paragraph('This is a custom named style from the template "Cool New Style"');
paragraph.style("Cool New Style");
doc.add(paragraph);

doc.createParagraph("Some normal text");

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