DOCX
@office-open/docx
Generate, parse, and patch .docx documents with a declarative TypeScript API
Installation
pnpm add @office-open/docx
npm install @office-open/docx
yarn add @office-open/docx
bun add @office-open/docx
Quick Start
{
"sections": [
{
"children": [{ "paragraph": { "children": ["Hello World"] } }]
}
]
}
import { generateDocument } from "@office-open/docx";
const buffer = await generateDocument({
sections: [
{
children: [{ paragraph: { children: ["Hello World"] } }],
},
],
});
Main Components
| Component | Description |
|---|---|
sections | Root document options with sections and properties |
paragraph | Text 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 cells | Structured tables with merged cells and borders |
image | Embedded images with transformation and positioning |
chart | Bar, line, pie, area, and scatter charts |
smartArt | Predefined SmartArt diagrams |
headers / footers | Section headers and footers |
footnoteReference, endnoteReference | Footnotes and endnotes |
tableOfContents | Auto-generated table of contents |
bookmark | Internal bookmarks and cross-references |
math | Mathematical equations via Office MathML (OMML) |
styles | Declarative style definitions |
hyperlink | External and internal hyperlinks |
comment, commentReference | Document comments with author and date tracking |
textbox | Floating text boxes with content and styling |
checkBox | Form checkbox support in documents |
sdt | Structured document tags (SDT) for form-like documents |
insertion, deletion | Track changes — insertions and deletions |
numbering | Numbered and bulleted lists with multiple levels |
symbol, pageBreak, columnBreak | Special characters, page breaks, and column breaks |
section properties | Page 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.
| Function | Returns | Use Case |
|---|---|---|
generateDocument(opts) | Buffer | Node.js file I/O |
generateDocument(opts, { type: "blob" }) | Blob | Browser downloads |
generateDocument(opts, { type: "base64" }) | string | Data URLs, API payloads |
generateDocument(opts, { type: "string" }) | string | Debugging, inspection |
generateDocumentStream(opts) | ReadableStream<Uint8Array> | Streaming large files |