DolanandClaude Opus 5.5 3aaeff48b8 fix: start a numbered heading's table of contents entry with its number, with page numbers from docx/layout (#1130) (#3800)
* fix: start a numbered heading's table of contents entry with its number, with page numbers from docx/layout (#1130)

When Word fills in a table of contents, the entry of a numbered heading
starts with the heading's number, then a tab, with a tab stop past the
number. docx wrote the entries without the numbers. With page numbers
from docx/layout, the table is written clean, so Word shows it as it is
written and doesn't update it: the numbers were never shown. Word for
Mac opened such a document with "Main 2" where its own entry is
"1 Main 2".

A page number estimator can now work out the list numbers of the
paragraphs bookmarks start (listNumbers), which docx asks for once it
has bookmarked the headings, before it writes the entries.
estimatePageNumbers, and estimatePageNumbersWith's estimators, read
them as docx/layout numbers its lists, without laying out the pages. A
numbered heading's entry then starts with its number and a tab, or its
number and a space when its list follows the number with a space or
nothing, as Word writes it, and the pages are laid out with them. A
number docx/layout doesn't write as Word does is left out.

Word places the tab stop by its Normal template, not the document:
past the number, measured in Aptos 12 pt, by 240 twips, and 240 more a
level below the first, at the next multiple of 240 up to 1920, and
where it falls past that. docx places it the same way. The probes are
scripts/layout-probes/word-toc-numbers.ts and the documents Word saved
once it had updated their tables. After entries at levels 7 to 9, Word
places the stops after them on multiples of 200 instead, which docx
doesn't follow.

Checked in Word for Mac 16.114: Word opens the docs' example without
asking to update it, with "1 Introduction 2" to "2.1 Results 3", and
writes the same entries, tab stops and page numbers when it updates
the table itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: don't name the TOC number probes .word.docx, which the layout comparison lays out

scripts/compare-layout.sh lays out every scripts/layout-probes/*.word.docx
and compares the page of each heading with the pages Word marked in it,
failing for a committed one whose marks don't have all its pages. The
TOC number probes' documents are what Word saved once it had updated
their tables, saved before Word had laid out all their pages, and
aren't for laying out: the shape demos workflow failed on
word-toc-numbers.word.docx, "its marks have 2 of its 3 pages".

They are now word-toc-numbers.docx and word-toc-numbers-font.docx, as
the other probes' documents are named.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 23:49:36 +01:00
2021-12-02 14:57:35 -06:00
2023-03-18 21:27:08 +00:00
2022-11-26 19:33:58 +00:00
2020-08-01 17:49:25 +01:00
2016-03-26 23:43:57 +00:00
2024-10-14 00:09:03 +01:00

clippy the assistant

Easily generate and modify .docx files with JS/TS. Works for Node and on the Browser.


NPM version Downloads per month GitHub Action Workflow Status Known Vulnerabilities PRs Welcome codecov Docx.js Editor

drawing

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:

Here is an example of docx working in Vue.js:

Node

Press endpoint on the RunKit website:

RunKit Instructions

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.

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

drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing

...and many more!


patreon browserstack

Made with 💖

S
Description
Lustro github.com/dolanmiu/docx
Readme MIT
414 MiB
Languages
TypeScript 96.8%
Python 3.1%