* fix: define the TOC 1 to TOC 9 styles, so LibreOffice keeps the levels of a table of contents indented (#3819) Since 9.9.0 a table of contents is written with its entries, in the styles TOC1 to TOC9, which docx never defined. Opening it, LibreOffice gives its Contents 2 to Contents 9 styles no indent, so once it updates the index every level is flush left. 9.8.1 wrote no entries, and LibreOffice kept its own indents. docx now writes the styles as Word adds them: based on Normal, 5 points of space after, and each level indented by Normal's font size more than the one above (200 twips a level at docx's default 10 points, 220 at 11 points, as Word for Mac writes them). They are hidden until used, as Word's are. A style with their id replaces them, as for the other default styles, and so does a style with their name, such as a template's "toc 1" with a localized id, so no two styles share a name. The entries no longer need an indent of their own, which assumed 220 twips. Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: keep the text-and-spacing layout demo's Tab stops heading clear of the bottom of a page The TOC styles give the table of contents' entries 5 points of space after, as Word's do, instead of the demo's 8, so the text after the table moved up 33 points. That left the Tab stops heading and its first line ending less than a point below the bottom margin of page 9. Word and docx/layout fit them there; LibreOffice, which compare-layout.sh checks against, moves them to page 10. Word and LibreOffice put every other line of the page in the same place. The hanging-indent paragraph above the heading has 80 words instead of 90, a line fewer, so the heading fits with room to spare. All 368 headings of the layout demos are now on LibreOffice's page, and Word puts this demo's on the same pages. Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: keep docx's TOC styles beside a character style with their name Only a paragraph style named "toc 1" to "toc 9" takes the place of docx's TOC1 to TOC9. A table of contents' entries are paragraphs, which can't take a character style, so a character style named "toc 2" left the entries naming a TOC2 that docx no longer wrote. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: indent the TOC styles patchDocument adds by the template's Normal font size Word indents each level of a table of contents by the size of the Normal style's font, and a Document does too, but the TOC1 to TOC9 styles that patchDocument adds to a template without them always stepped by docx's 10 points. They now follow the template's Normal: its own size, or else the document's default size, such as the 12 points Word for Mac saves. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: take the TOC indent from the template's style that patchDocument bases the TOC styles on The TOC styles patchDocument adds are based on the template's style with Normal's id, or else on its style named Normal. Their indent now follows the font size of that same style, so a template whose Normal has another name still gets its size. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Easily generate and modify .docx files with JS/TS. Works for Node and on the Browser.
Demo
Browser
Here are examples of docx being used with basic HTML/JS in a browser environment:
Here are examples of docx working in Angular:
Here are examples of docx working in React:
- https://stackblitz.com/edit/react-docx
- https://stackblitz.com/edit/react-docx-images (adding images to Word Document)
Here is an example of docx working in Vue.js:
Node
Press endpoint on the RunKit website:
- https://runkit.com/dolanmiu/docx-demo1 - Simple paragraph and text
- https://runkit.com/dolanmiu/docx-demo2 - Advanced Paragraphs and text
- https://runkit.com/dolanmiu/docx-demo3 - Bullet points
- https://runkit.com/dolanmiu/docx-demo4 - Simple table
- https://runkit.com/dolanmiu/docx-demo5 - Images
- https://runkit.com/dolanmiu/docx-demo6 - Margins
- https://runkit.com/dolanmiu/docx-demo7 - Landscape
- https://runkit.com/dolanmiu/docx-demo8 - Header and Footer
- https://runkit.com/dolanmiu/docx-demo10 - My CV generated with docx
More here
How to use & Documentation
Please refer to the documentation at https://docx.js.org/ for details on how to use this library, examples and much more!
Shapes, watermarks and charts
Shapes, groups of shapes and diagrams with connectors come with docx, from docx/shapes, watermarks from docx/watermarks, and native Word charts from docx/charts. They are left out of docx itself, so documents that don't use them don't carry their code:
import { Document, Header, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
import { ShapeRun } from "docx/shapes";
import { TextWatermark } from "docx/watermarks";
const doc = new Document({
sections: [
{
headers: { default: new Header({ children: [new Paragraph({ children: [new TextWatermark({ text: "DRAFT" })] })] }) },
children: [
new Paragraph({ children: [new ShapeRun({ type: "ellipse", transformation: { width: 100, height: 60 } })] }),
new Paragraph({
children: [new ChartRun({ type: "column", categories: ["Q1", "Q2"], series: [{ name: "Sales", values: [10, 20] }] })],
}),
],
},
],
});
In a page without a bundler, load dist/shapes.umd.cjs, dist/watermarks.umd.cjs or dist/charts.umd.cjs after dist/index.umd.cjs. They add the docxShapes, docxWatermarks and docxCharts globals. See the shapes, watermarks and charts documentation.
HTML
HTML, such as what people write in a rich-text editor, becomes a document's paragraphs, lists, tables, images, links, equations and footnotes with docx/html, written in the document's own styles:
import { Document } from "docx";
import { htmlToContent } from "docx/html";
const doc = new Document({
sections: [{ children: htmlToContent("<h1>Report</h1><p>Sales were <b>up 12%</b>.</p><ul><li>EMEA</li><li>APAC</li></ul>") }],
});
In a page without a bundler, load dist/html.umd.cjs after dist/index.umd.cjs and dist/math.umd.cjs. It adds the docxHtml global. See the HTML documentation.
Macros
VBA macros, written from their code, make a document a macro-enabled document (.docm), with docx/macros:
import { Document, Paragraph } from "docx";
import { createMacros } from "docx/macros";
const doc = new Document({
macros: createMacros({
modules: [{ name: "Greetings", code: 'Sub SayHello()\n MsgBox "Hello"\nEnd Sub' }],
}),
sections: [{ children: [new Paragraph("Run SayHello from View > Macros")] }],
});
In a page without a bundler, load dist/macros.umd.cjs after dist/index.umd.cjs. It adds the docxMacros global. See the macros documentation.
Embedded Files
Workbooks, documents, presentations and any other file, such as a sound, a video or a PDF, are embedded in a document as OLE objects, which open from it, with docx/embedded-files:
import * as fs from "fs";
import { Document, Paragraph } from "docx";
import { EmbeddedFileRun } from "docx/embedded-files";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new EmbeddedFileRun({
fileName: "budget.xlsx",
data: fs.readFileSync("./budget.xlsx"),
preview: { type: "png", data: fs.readFileSync("./budget.png") },
transformation: { width: 240, height: 120 },
}),
],
}),
],
},
],
});
In a page without a bundler, load dist/embedded-files.umd.cjs after dist/index.umd.cjs. It adds the docxEmbeddedFiles global. See the embedded files documentation.
Citations and Bibliography
Sources, citations of them in the text and a bibliography, in one of Word's styles of citation, such as APA, MLA or Chicago, written as Word writes them, with docx/bibliography:
import { Document, Paragraph, TextRun } from "docx";
import { Bibliography, Citation, CitationStyle, SourceType, createBibliography } from "docx/bibliography";
const doc = new Document({
bibliography: createBibliography({
style: CitationStyle.APA,
sources: [
{
tag: "Bak10",
type: SourceType.BOOK,
authors: [{ lastName: "Baker", firstName: "Anna" }],
title: "The Art of Pizza",
year: 2010,
city: "Naples",
publisher: "Oven Press",
},
],
}),
sections: [
{
children: [
new Paragraph({ children: [new TextRun("Pizza has been baked in Naples for centuries "), new Citation("Bak10")] }),
new Bibliography({ title: "References" }),
],
},
],
});
In a page without a bundler, load dist/bibliography.umd.cjs after dist/index.umd.cjs. It adds the docxBibliography global. See the citations and bibliography documentation.
Playground
Experience docx in action through Docx.js Editor, an interactive playground where you can code and preview the results in real-time.
Examples
Check the demo folder for examples.
Contributing
Read the contribution guidelines here.
Used by
...and many more!
Made with 💖


















