Files
docx/docs/usage/styling-with-xml.md
DolanandClaude Opus 5.5 66eaf1c12e feat: read the theme of a Word document or template with readTheme, and theme fonts for other scripts (#878) (#3839)
* feat: Word's themes, colors and fonts, and the theme of a Word document or template, from docx/theme (#878)

docx/theme is a new optional entry, so the main bundle stays as it is:

- Themes: the 29 themes of Word's Design > Themes gallery, each with its
  name, colors, and fonts for headings and body text, as IThemeOptions.
- ColorSchemes and FontSchemes: the 24 color sets and 26 font sets of the
  Design > Colors and Design > Fonts galleries, to mix with a theme as
  Word's Design tab does.
- readTheme: the theme of a .docx, .docm, .dotx, .dotm, .pptx, .xlsx or
  .thmx file, or of a theme1.xml, or colors or fonts Word saved on their
  own. Styles taken from a template with externalStyles then keep the
  template's theme fonts and colors (#878).

The presets are generated from Word for Mac's own theme files by
scripts/generate-word-themes.ts, which reads each one with readTheme.

In docx, a theme font can now have a font for each script (scripts, by
ISO 15924 code), in place of Office's for those scripts, so Word's themes
keep their own fonts for Japanese, Chinese and other scripts. docx also
exports parseXml and unzipFiles, which it already had, so docx/theme
reads files without bundling a second XML parser and unzipper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* refactor: read a template's theme with readTheme from docx, sharing the patcher's theme reader, without docx/theme (#878)

- Word's themes, colour sets and font sets (Themes, ColorSchemes, FontSchemes), the
  script that generated them and the docx/theme entry are dropped. #878 asks only for
  the theme of the template a document's styles come from.
- readTheme moves into the core and is exported from docx. The patcher reads the
  colours of a template's theme with the same code, so parseXml and unzipFiles stay
  internal.
- The paths of a package's parts and its relationships' targets (relationshipsPathOf,
  resolveTarget and the others) move from the patcher to src/util/package-paths.ts,
  so the theme reader in src/file can use them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* fix: read a theme in UTF-16, and inflate only a package's relationships and its theme in readTheme

- A theme, or the XML of one, that starts with UTF-16's byte order mark is read as
  UTF-16, little or big endian, as XML may be written.
- readTheme inflates a package's relationships, and then only the theme they refer to,
  so a large or damaged document, presentation or workbook part doesn't take memory or
  stop it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* fix: find a theme whose relationship's target is in another case than its part, in readTheme and the patcher

The Open Packaging Conventions compare part names without regard to case, so a
relationship's target such as "Theme/THEME1.xml" names word/theme/theme1.xml.
readTheme threw that the file has no theme, and patchDocument used Office's colours.
isSamePart, in src/util/package-paths.ts, compares them as drawing-patch already finds
a relationship's target.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 23:02:10 +00:00

6.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: Fonts and colors picked from the theme, such as "(Headings)" or "Accent 1", come from the theme of the document docx writes. See Theme fonts and colors.

  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, and so does a style in styles.paragraphStyles or styles.characterStyles with the same id. Styles with new ids are added alongside 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"),
            ],
        },
    ],
});

Theme fonts and colors

Styles made in Word often name the theme's fonts and colors rather than a font or a color: "(Headings)" is written as w:asciiTheme="majorHAnsi", and "Accent 1" as w:themeColor="accent1". Word looks them up in the document's theme, which is the one docx writes, not the theme of the document your styles.xml came from. It's Office's theme, with Calibri Light for headings and Calibri for body text, unless the theme option changes it.

To keep the fonts and colors of the document the styles came from, give it that document's theme, read with readTheme. It reads the document itself, or its word/theme/theme1.xml:

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

const doc = new Document({
    externalStyles: fs.readFileSync("./demo/assets/custom-styles.xml", "utf-8"),
    // The theme of the document the styles came from. Here, that of another document made in Word, with Cambria for headings
    theme: readTheme(fs.readFileSync("./demo/assets/field-trip.docx")),
    sections: [
        {
            children: [
                // The styles' Heading 1 is in the theme's sixth accent color, darker 25%
                new Paragraph({ text: "A heading in the theme's accent 6", heading: HeadingLevel.HEADING_1 }),
                // Their Normal style is in the theme's font for body text
                new Paragraph("Body text in the theme's font for body text"),
            ],
        },
    ],
});

Or give its theme's fonts and colors in theme yourself. They're in that document's word/theme/theme1.xml: the typeface of a:latin in a:majorFont is the font for headings, the one in a:minorFont is the font for body text, and a:clrScheme has the colors.

import { Document, HeadingLevel, Paragraph } from "docx";

// Heading 1 in Word's "(Headings)" font
const styles = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:style w:type="paragraph" w:styleId="Heading1">
        <w:name w:val="heading 1"/>
        <w:basedOn w:val="Normal"/>
        <w:next w:val="Normal"/>
        <w:qFormat/>
        <w:pPr><w:keepNext/><w:outlineLvl w:val="0"/></w:pPr>
        <w:rPr>
            <w:rFonts w:asciiTheme="majorHAnsi" w:eastAsiaTheme="majorEastAsia" w:hAnsiTheme="majorHAnsi" w:cstheme="majorBidi"/>
            <w:b/>
            <w:caps/>
            <w:sz w:val="28"/>
        </w:rPr>
    </w:style>
</w:styles>`;

const doc = new Document({
    externalStyles: styles,
    // The fonts in the theme1.xml of the document the styles came from
    theme: { fonts: { headings: "Georgia", body: "Georgia" } },
    sections: [
        {
            children: [new Paragraph({ text: "In Georgia", heading: HeadingLevel.HEADING_1 })],
        },
    ],
});

Page sizes and margins aren't styles, so they aren't in styles.xml. Give them in each section's properties, as Page Layout shows.

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