DOCX

@office-open/docx

Generate, parse, and patch .docx documents with a declarative TypeScript API

Installation

pnpm add @office-open/docx

Quick Start

{
  "sections": [
    {
      "children": [{ "paragraph": { "children": ["Hello World"] } }]
    }
  ]
}

Main Components

ComponentDescription
sectionsRoot document options with sections and properties
paragraphText paragraph with formatting, alignment, and spacing
text runs (strings or { text, bold, ... } objects)Inline text with font, size, color, and style
table with rows and cellsStructured tables with merged cells and borders
imageEmbedded images with transformation and positioning
chartBar, line, pie, area, and scatter charts
smartArtPredefined SmartArt diagrams
headers / footersSection headers and footers
footnoteReference, endnoteReferenceFootnotes and endnotes
tableOfContentsAuto-generated table of contents
bookmarkInternal bookmarks and cross-references
mathMathematical equations via Office MathML (OMML)
stylesDeclarative style definitions
hyperlinkExternal and internal hyperlinks
comment, commentReferenceDocument comments with author and date tracking
textboxFloating text boxes with content and styling
checkBoxForm checkbox support in documents
sdtStructured document tags (SDT) for form-like documents
insertion, deletionTrack changes — insertions and deletions
numberingNumbered and bulleted lists with multiple levels
symbol, pageBreak, columnBreakSpecial characters, page breaks, and column breaks
section propertiesPage size, margins, orientation, columns, and borders

Patching

Modify existing .docx templates by replacing {{placeholder}} tokens with new content:

import { patchDocument } from "@office-open/docx";

const result = await patchDocument({
  outputType: "nodebuffer",
  data: templateBuffer,
  placeholders: {
    name: { type: "paragraph", children: [{ text: "John Doe" }] },
  },
});

See for full documentation.

Export Formats

Every async function has a synchronous counterpart (e.g., generateDocumentSync). See the page for the full list.

FunctionReturnsUse Case
generateDocument(opts)BufferNode.js file I/O
generateDocument(opts, { type: "blob" })BlobBrowser downloads
generateDocument(opts, { type: "base64" })stringData URLs, API payloads
generateDocument(opts, { type: "string" })stringDebugging, inspection
generateDocumentStream(opts)ReadableStream<Uint8Array>Streaming large files
Copyright © 2026