* 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>
9.5 KiB
Themes
A document's theme gives it twelve colors, a font for headings and a font for body text. Word shows the theme's colors at the top of its color menus, and its fonts at the top of its font menu as "(Headings)" and "(Body)". Text and shapes that use the theme's colors and fonts change with the theme when a different one is chosen on Word's Design tab.
Every document docx writes has a theme (word/theme/theme1.xml): Office's theme from Office 2016 to 2021, with Calibri Light for headings and Calibri for body text, unless the theme option changes it.
A theme of your own
import { Document, Paragraph, TextRun } from "docx";
const doc = new Document({
theme: {
name: "Harbour",
colors: { dark2: "1B3A4B", accent1: "1F6F8B", accent2: "E07A5F" },
fonts: { headings: "Cambria", body: "Calibri" },
},
sections: [
{
children: [
new Paragraph({
children: [
new TextRun({
text: "In the theme's font for headings, and accent 1",
font: { theme: "headings" },
color: { theme: "accent1" },
}),
],
}),
new Paragraph({
children: [
new TextRun({
text: "In the theme's font for body text, and accent 2",
font: { theme: "body" },
color: { theme: "accent2" },
}),
],
}),
],
},
],
});
Colors and fonts that aren't given are Office's.
Theme Options
| Property | Type | Notes | Description |
|---|---|---|---|
name |
string |
Optional | The theme's name, as Word shows it. Default is "Office Theme" |
colors |
IThemeColorsOptions |
Optional | The theme's colors, each a 6-digit hex color such as "4472C4" |
fonts |
IThemeFontsOptions |
Optional | headings and body: a font name, or { latin, eastAsia, complexScript } for scripts |
Theme Colors
| Color | OOXML name | Default |
|---|---|---|
dark1 |
dk1 |
The system's window text color (black) |
light1 |
lt1 |
The system's window color (white) |
dark2 |
dk2 |
44546A |
light2 |
lt2 |
E7E6E6 |
accent1 |
accent1 |
4472C4 |
accent2 |
accent2 |
ED7D31 |
accent3 |
accent3 |
A5A5A5 |
accent4 |
accent4 |
FFC000 |
accent5 |
accent5 |
5B9BD5 |
accent6 |
accent6 |
70AD47 |
hyperlink |
hlink |
0563C1 |
followedHyperlink |
folHlink |
954F72 |
Text in the theme's fonts
A run's or a style's font can be one of the theme's fonts: { theme: "headings" } or { theme: "body" }. It's the theme's font for every script, as Word's own styles use it.
import { Document, HeadingLevel, Paragraph, TextRun } from "docx";
const doc = new Document({
theme: { fonts: { headings: "Cambria", body: "Calibri" } },
styles: {
default: {
// All text is in the theme's font for body text, unless it says otherwise
document: { run: { font: { theme: "body" }, size: 22 } },
heading1: { run: { font: { theme: "headings" }, size: 32, color: "1B3A4B" } },
},
},
sections: [
{
children: [
new Paragraph({ heading: HeadingLevel.HEADING_1, children: [new TextRun("In Cambria")] }),
new Paragraph({
children: [new TextRun("In Calibri, "), new TextRun({ text: "and Cambria", font: { theme: "headings" } })],
}),
],
},
],
});
The library's own styles don't use the theme's fonts, so text looks as it always has until a style or a run asks for them. Word's styles, such as those in externalStyles taken from a Word document, use them: Word's Normal style is in the theme's font for body text, and its headings in the font for headings. The theme is the one docx writes, not the theme of the document the styles came from, so give that theme's fonts and colors in theme to keep them.
Text, tables and borders in the theme's colors
Wherever text, underlines, borders and shading take a color, they can take one of the theme's colors instead: { theme: "accent1" }. They change color when the theme's colors change.
lighter and darker make the color lighter or darker, from 0 (unchanged) to 100 (white or black), as Word's color menus do: "Blue, Accent 1, Lighter 80%" is { theme: "accent1", lighter: 80 }, and "Blue, Accent 1, Darker 25%" is { theme: "accent1", darker: 25 }.
import { BorderStyle, Document, HeadingLevel, Paragraph, ShadingType, TextRun, UnderlineType } from "docx";
const doc = new Document({
theme: { colors: { accent1: "1F6F8B" } },
styles: {
default: {
heading1: { run: { color: { theme: "accent1", darker: 25 } } },
},
},
sections: [
{
children: [
new Paragraph({ heading: HeadingLevel.HEADING_1, children: [new TextRun("A heading in accent 1, darker 25%")] }),
new Paragraph({
// A rule under the paragraph, and a light background
border: { bottom: { style: BorderStyle.SINGLE, size: 6, color: { theme: "accent1" } } },
shading: { type: ShadingType.CLEAR, fill: { theme: "accent1", lighter: 80 } },
children: [
new TextRun({ text: "Accent text", color: { theme: "accent1" } }),
new TextRun({
text: " with a wavy underline",
underline: { type: UnderlineType.WAVE, color: { theme: "accent2" } },
}),
],
}),
],
},
],
});
| Option | Takes a theme color |
|---|---|
A run's or style's color |
color |
A run's or style's underline |
underline.color |
shading of runs, paragraphs, tables and table cells |
fill and color |
A run's border, and each side of the borders of paragraphs, tables, cells and pages |
color |
Document's background, the color of the page |
color |
The theme's color names are those in Theme Colors. Word's color menus call dark1 and light1 "Text 1" and "Background 1", and dark2 and light2 "Text 2" and "Background 2".
Word writes a theme color with the color it comes to, for applications that don't read the theme, and so does docx: { theme: "accent1", darker: 25 } in Office's theme is written as 2F5496, the color Word writes for "Blue, Accent 1, Darker 25%". For some colors, the color docx writes is one or two off Word's in a channel. With patchDocument, that color is worked out from the template's theme, or from Office's if the template has none. Word takes the color from the theme either way.
Shapes in the theme's colors
Shapes from docx/shapes can be filled and outlined in the theme's colors, lighter or darker if you like, as Word's color menus offer them. See Theme colours.
import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ShapeRun({
type: "roundedRectangle",
transformation: { width: 120, height: 60 },
fill: { theme: "accent1", lighter: 80 },
line: { theme: "accent1", darker: 25 },
}),
],
}),
],
},
],
});
Examples
A theme of its own
Source: https://github.com/dolanmiu/docx/blob/master/demo/styles/theme.ts
Text, tables and borders in the theme's colors
Source: https://github.com/dolanmiu/docx/blob/master/demo/styles/theme-colors.ts