Files
docx/docs
DolanandClaude Opus 5.5 eadca504e2 fix: define the TOC 1 to TOC 9 styles, so LibreOffice keeps the levels of a table of contents indented (#3819) (#3821)
* 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>
2026-10-23 04:09:31 +01:00
..
2020-09-05 18:40:40 +01:00
2020-09-05 18:51:50 +01:00

Welcome to docx

What is docx?

docx is a TypeScript/JavaScript library for generating Word documents (.docx files) programmatically. It provides a declarative API that works seamlessly in both Node.js and browser environments.

Why docx?

  • Declarative API - Define what you want (objects/config), not how to build it step-by-step
  • TypeScript First - Full type definitions with IntelliSense support
  • Universal - Works in Node.js, browsers, and serverless environments
  • No Dependencies - Zero external runtime dependencies
  • Full Featured - Tables, images, shapes, charts, headers, footers, styles, and more

Quick Navigation

I want to... Go to...
Get started quickly Quickstart Guide
Learn a specific feature Usage Guides
See working examples Demo Files
Browse the API API Documentation
Modify existing documents Patcher

Installation

npm install --save docx

Then you can require or import as usual:

const docx = require("docx");
import * as docx from "docx";
// or
import { Document, Packer, Paragraph, TextRun } from "docx";

Shapes, watermarks and charts come with the package too, from docx/shapes, docx/watermarks and docx/charts:

import { ShapeRun } from "docx/shapes";
import { TextWatermark } from "docx/watermarks";
import { ChartRun } from "docx/charts";

Basic Usage

import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun("Hello World"),
                        new TextRun({
                            text: " - Bold text",
                            bold: true,
                        }),
                    ],
                }),
            ],
        },
    ],
});

Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("My Document.docx", buffer);
});

clippy the assistant


Made with 💖