Styles and Themes
Define reusable styles to keep your document formatting consistent and maintainable.
Defining Styles
{
"styles": {
"default": {
"document": {
"run": {
"font": "Calibri",
"size": 22
},
"paragraph": {
"spacing": { "line": 276 }
}
}
},
"paragraphStyles": [
{
"id": "MyHeading",
"name": "My Heading",
"basedOn": "Heading1",
"run": {
"font": "Arial",
"size": 36,
"color": "2E74B5"
}
}
],
"characterStyles": [
{
"id": "CodeInline",
"name": "Code Inline",
"run": {
"font": "Consolas",
"size": 20,
"shading": { "fill": "F2F2F2" }
}
}
]
},
"sections": [
{
"children": [
{ "paragraph": { "style": "MyHeading", "children": ["Styled Heading"] } },
{
"paragraph": {
"children": ["Normal text with ", { "text": "inline code", "style": "CodeInline" }]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
styles: {
default: {
document: {
run: {
font: "Calibri",
size: 22,
},
paragraph: {
spacing: { line: 276 },
},
},
},
paragraphStyles: [
{
id: "MyHeading",
name: "My Heading",
basedOn: "Heading1",
run: {
font: "Arial",
size: 36,
color: "2E74B5",
},
},
],
characterStyles: [
{
id: "CodeInline",
name: "Code Inline",
run: {
font: "Consolas",
size: 20,
shading: { fill: "F2F2F2" },
},
},
],
},
sections: [
{
children: [
{ paragraph: { style: "MyHeading", children: ["Styled Heading"] } },
{
paragraph: {
children: ["Normal text with ", { text: "inline code", style: "CodeInline" }],
},
},
],
},
],
});
Style Types
| Type | ID | Description |
|---|---|---|
| Paragraph | "paragraph" | Applies to entire paragraphs |
| Character | "character" | Applies to runs within paragraphs |
| Table | "table" | Applies to tables |
| Numbering | "numbering" | Applies to list numbering |
Table Styles
Define reusable table styles with styles.tableStyles[]. Each style can set base table/row/cell properties plus conditional formatting for specific regions (first row, banded rows, etc.):
import { generateDocument } from "@office-open/docx";
await generateDocument({
styles: {
tableStyles: [
{
id: "MyGrid",
name: "My Grid",
basedOn: "TableNormal",
conditionalFormats: [
{ type: "firstRow", run: { bold: true } },
{ type: "band1Horz", cell: { shading: { fill: "F2F2F2" } } },
],
},
],
},
sections: [
{
children: [{ table: { style: "MyGrid", rows: [{ cells: [{ children: ["A"] }] }] } }],
},
],
});
Conditional Format Types (type)
wholeTable · firstRow · lastRow · firstCol · lastCol · band1Vert · band2Vert · band1Horz · band2Horz · neCell · nwCell · seCell · swCell
Each conditional format accepts optional paragraph, run, table, row, and cell property blocks. Table styles round-trip through parsing as structured data.
Default Styles
Override the document defaults with the default property:
{
"styles": {
"default": {
"document": {
"run": { "font": "Calibri", "size": 22, "color": "333333" },
"paragraph": { "spacing": { "line": 276 } }
},
"heading1": {
"run": { "font": "Calibri Light", "size": 44, "color": "1F3763" }
}
}
},
"sections": [{ "children": [] }]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
styles: {
default: {
document: {
run: {
font: "Calibri",
size: 22, // 11pt in half-points
color: "333333",
},
paragraph: {
spacing: { line: 276 }, // 1.15 line spacing
},
},
heading1: {
run: {
font: "Calibri Light",
size: 44,
color: "1F3763",
},
},
},
},
sections: [{ children: [] }],
});
Using Styles
Apply styles to paragraphs and runs:
{
"styles": {
"paragraphStyles": [
{ "id": "MyHeading", "name": "My Heading", "run": { "font": "Arial", "size": 36 } }
],
"characterStyles": [
{ "id": "CodeInline", "name": "Code Inline", "run": { "font": "Consolas", "size": 20 } }
]
},
"sections": [
{
"children": [
{ "paragraph": { "style": "MyHeading", "children": ["Styled Heading"] } },
{
"paragraph": {
"children": ["Normal text with ", { "text": "inline code", "style": "CodeInline" }]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
// Paragraph style
{ paragraph: { style: "MyHeading", children: [...] } }
// Character style on a run
{ text: "styled", style: "CodeInline" }
External Styles
Import an entire styles.xml from an existing document via the top-level externalStyles option (a raw XML string). Built-in styles you omit are still emitted by the default factory; styles you define here override them by styleId.
{
"externalStyles": "<?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:pPr><w:outlineLvl w:val=\"0\"/></w:pPr><w:rPr><w:color w:val=\"2E74B5\"/></w:rPr></w:style></w:styles>",
"sections": [{ "children": [] }]
}
import { generateDocument } from "@office-open/docx";
// externalStyles is the raw XML of a <w:styles> part — e.g. read from an
// existing document's word/styles.xml.
await generateDocument({
externalStyles: `<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:pPr><w:outlineLvl w:val="0"/></w:pPr>
<w:rPr><w:color w:val="2E74B5"/></w:rPr>
</w:style>
</w:styles>`,
sections: [
// ...
],
});
Style Options Reference
| Option | Type | Description |
|---|---|---|
id | string | Unique style identifier |
name | string | Display name |
type | "paragraph" | "character" | "table" | "numbering" | Style type |
basedOn | string | Parent style to inherit from |
run | object | Run formatting (font, size, color, bold, etc.) |
paragraph | object | Paragraph formatting (spacing, alignment, etc.) |