Files
docx/docs
DolanandClaude Fable 5.1 19087e8b79 fix: name images in the package as Word does, image1.png and so on (#3872)
* fix: name images in the package as Word does, image1.png and so on

Images were written to word/media under a SHA-1 hash of their data,
such as 4bf7547c9322fd2143bd3e8dfa84109d9d3fed14.png. Word for the web
has shown an empty box in place of an image named like that, and shown
it once the file was renamed image1.png as Word names its own, as
https://github.com/dolanmiu/docx/issues/496 reports.

The hash is still the key an image is added to the media under, so an
image used twice is stored once and its placeholders are unchanged, but
the file is written as image1.png, image2.jpg and so on, numbered in the
order images are added. The numbers come from the document's
PackageParts, which an ImagePatch's parts already take theirs from, so
an image never takes a part's name, nor a part an image's, and in
patchDocument new images are numbered after the template's own.

Closes #496
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QzDw6ZwCkrdX4ZsWzeVQNC

* docs: say images are numbered by type, image1.png and image1.jpg

A document's images take their names from PackageParts.takePath,
which numbers by the whole file name, so a PNG and a JPEG are
image1.png and image1.jpg, not image2.jpg as the docs and the Media
comment said. Word for the web shows images named this way, as the
images demo showed, and an ImagePatch's images were already numbered
so. The docs now say that, and a Media without a path taker numbers
its images by type too, so it names them as a document does.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QzDw6ZwCkrdX4ZsWzeVQNC

* test: a document numbers its images by type, in the order they are added

Covers a PNG, a JPEG and a second PNG written through File, which
takePath names image1.png, image1.jpg and image2.png.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QzDw6ZwCkrdX4ZsWzeVQNC

* test: check the relationships to a document's images, numbered by type

The mixed-extension test checked the media files and their bytes, but
not the relationships that point the drawings at them, which is the
XML the names matter in.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QzDw6ZwCkrdX4ZsWzeVQNC

* test: check each of a document's images is written under its own number

Swapping the two PNGs' bytes passed the mixed-extension test, though
its name says images are numbered in the order they are added.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QzDw6ZwCkrdX4ZsWzeVQNC

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-31 14:10:01 +00: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 💖