DOCX

Styles and Themes

Declarative style system for character and paragraph formatting

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" }]
          }
        }
      ]
    }
  ]
}

Style Types

TypeIDDescription
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": [] }]
}

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" }]
          }
        }
      ]
    }
  ]
}

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": [] }]
}

Style Options Reference

OptionTypeDescription
idstringUnique style identifier
namestringDisplay name
type"paragraph" | "character" | "table" | "numbering"Style type
basedOnstringParent style to inherit from
runobjectRun formatting (font, size, color, bold, etc.)
paragraphobjectParagraph formatting (spacing, alignment, etc.)
Copyright © 2026