Package Exports
- @office-open/docx
Readme
@office-open/docx
Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
Features
- Document Generation - Create Word documents with sections, headers, footers, and page numbers
- Paragraphs & Text - Rich text support with bold, italic, underline, strikethrough, and more
- Tables - Full table support with merged cells, borders, and styles
- Images - Inline and floating images with sizing, positioning, and wrapping
- Hyperlinks - External and internal hyperlinks with custom styling
- Headers & Footers - First, last, even/odd page headers and footers
- Lists - Numbered and bulleted lists with multiple levels and custom formats
- Styles - Paragraph, character, and table styles with inheritance
- Table of Contents - Auto-generated table of contents with custom styling
- Footnotes & Endnotes - Comprehensive footnote and endnote support
- Charts - Bar, line, pie, area, and scatter charts with customization
- Math Equations - Full mathematical equation support via MathML
- SmartArt - Built-in SmartArt graphic generation
- Bibliography - Source management and citation support
- Comments - Document comments with author and date tracking
- Track Revisions - Insertions, deletions, and formatting changes
- Content Controls - Structured document tags (SDT) for form-like documents
- Text Boxes - Floating text boxes with content and styling
- Checkboxes - Form checkbox support in documents
- DrawingML - Shapes with fills, shadows, effects, and transformations
- Custom Fonts - Font embedding and custom font tables
- Template Patching - Patch existing DOCX templates via placeholder replacement
- Settings - Comprehensive document settings and compatibility options
Installation
# Install with npm
$ npm install @office-open/docx
# Install with pnpm
$ pnpm add @office-open/docxQuick Start
import { Document, Paragraph, TextRun, Packer } from "@office-open/docx";
import { writeFileSync } from "node:fs";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new TextRun("Hello World"),
new TextRun({
text: " - Bold text",
bold: true,
}),
],
}),
],
},
],
});
const buffer = await Packer.toBuffer(doc);
writeFileSync("My Document.docx", buffer);Examples
Check the demo folder for 100+ working examples covering every feature.
Benchmark
Performance comparison against original docx (9.6.1) package (Windows 11 / Node 22):
Object Creation (no pack)
| Scenario | @office-open/docx | docx | Speedup |
|---|---|---|---|
| Simple document (2 paragraphs) | 26.7K ops/s | 4.9K ops/s | 5.5x |
| Styled paragraphs (20 paragraphs) | 24.3K ops/s | 4.1K ops/s | 6.0x |
| Table (10x5 cells) | 17.8K ops/s | 3.0K ops/s | 5.9x |
| Full featured (header/footer/headings/table/paragraphs) | 13.1K ops/s | 2.4K ops/s | 5.5x |
Create + toBuffer (end-to-end)
| Scenario | @office-open/docx | docx | Speedup |
|---|---|---|---|
| Simple document (2 paragraphs) | 1,413 ops/s | 182 ops/s | 7.8x |
| Styled paragraphs (20 paragraphs) | 1,593 ops/s | 196 ops/s | 8.1x |
| Table (10x5 cells) | 1,167 ops/s | 174 ops/s | 6.7x |
| Full featured (header/footer/headings/table/paragraphs) | 784 ops/s | 162 ops/s | 4.8x |
Large Files — Create + toBuffer
| Scenario | @office-open/docx | docx | Speedup |
|---|---|---|---|
| 500 paragraphs | 182 ops/s | 84 ops/s | 2.2x |
| 100×10 table | 122 ops/s | 57 ops/s | 2.1x |
| 10 sections × 50 paragraphs | 267 ops/s | 103 ops/s | 2.6x |