Files
docx/docs/usage/templates.md
T
DolanandClaude Opus 5.5 2c4e5b43cb fix: patch placeholders in links, content controls and tracked changes (#3864)
* fix: patch placeholders in links, content controls and tracked changes

patchDocument and patchDetector only read runs that are direct children of
a paragraph, so a placeholder in a link, a content control, text typed with
Track Changes on, a smart tag or a field's result was left as it was, with
no error. Runs in those elements are now read and patched where they are.

Where the content can't go in what holds the placeholder, it goes where
Word can open it: a link is taken out of a tracked insertion (whose piece
after it gets an id of its own) or a link, a plain text content control
given more than text becomes a rich text one, and a content control
showing its placeholder text stops showing it.

Addresses #2679

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* test: cover every branch of patching runs in links and content controls

CI's coverage fell below 100%: an empty link, a content control without
properties, and runs that stay together when a tracked insertion is split
weren't tested. The name fallbacks in the replacer's lookups could never
be reached, so the lookups take an element's name as it is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* feat: find placeholders with custom delimiters with patchDetector, and say why a placeholder isn't replaced

patchDetector takes placeholderDelimiters, as patchDocument does, so it
finds the same placeholders. Before, it only knew {{ and }}, and found
nothing in a template with <<name>>.

templates.md has a section on a placeholder that isn't replaced: how to
find the patches whose placeholder isn't in the template with
patchDetector, as patchDocument leaves them out without an error, and the
usual reasons, such as a key written with its braces.

Addresses #2679

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* fix: unbind a content control a patch fills, so Word doesn't put the placeholder back

Word fills a control bound to custom XML or a document property
(w:dataBinding) in from its data when it opens a document. A patched
placeholder in one came back: Word for Mac showed {{name}} from the XML,
though the control held the patch's text. The binding is now removed from
a control a patch's content goes in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* perf: split a tracked change around a link in linear time

The stretches were built by copying the stretch so far for each element,
so a tracked insertion of n runs took n² steps to split. Each stretch is
now sliced from where it starts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* docs: list a placeholder only in alt text among the reasons a patch isn't applied

patchDetector finds placeholders in pictures' and charts' alt text, so
comparing its keys with the patches' doesn't show a text patch for one,
which leaves it as it is. It's now one of the listed reasons, saying so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* fix: don't let a start delimiter before a placeholder hide it from patchDetector

A key ran from the first start delimiter to the next end delimiter, so in
"<<unfinished <<name>>" patchDetector found "unfinished <<name", not
"name", which patchDocument patches. With one-character delimiters such as
< and >, any "<" in the text did this. A key now has no start delimiter in
it. The search from a start delimiter with no end after it also stops at
the next one, rather than going on to the end of the paragraph each time:
120,000 characters of unmatched "<" took 1.4 s, and now take no time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* test: check the link a patch puts in a content control without properties

The test only checked the control's text and its one child, so content
that lost the link would have passed it.

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-29 20:39:09 +00:00

37 KiB

Templates

Templates allow you to modify existing Word documents by replacing placeholder text with dynamic content. This is useful for generating documents from pre-designed layouts.

!> For detailed information on the patchDocument function, see the Patcher guide.

Overview

The template workflow consists of:

  1. Create a Word document (.docx) with placeholder tags like {{placeholder}}
  2. Use patchDocument to replace the placeholders with content
  3. Export the modified document

Creating a Template

Open your Word processor and create a document with placeholders:

Dear {{customer_name}},

Thank you for your order #{{order_number}}.

Your items will ship on {{ship_date}}.

Best regards,
{{company_name}}

?> Use double curly braces {{}} for placeholders. These will be replaced at runtime.

Spaces inside placeholders

People often type a placeholder with spaces inside its braces, such as {{ customer_name }}, which doesn't match the patch customer_name. Give patchDocument trimPlaceholders: true to patch it as if the spaces weren't there:

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

// The template, with spaces inside the braces of its placeholders
const template = new Document({
    sections: [
        {
            children: [new Paragraph("Dear {{ customer_name }},"), new Paragraph("Thank you for your order #{{order_number }}.")],
        },
    ],
});

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: await Packer.toBuffer(template),
    patches: {
        customer_name: { type: PatchType.PARAGRAPH, children: [new TextRun("John Smith")] },
        order_number: { type: PatchType.PARAGRAPH, children: [new TextRun("12345")] },
    },
    trimPlaceholders: true,
});

fs.writeFileSync("output.docx", doc);
  • Only the spaces at the ends are left out, as JavaScript's trim() does, including tabs and non-breaking spaces. {{ first name }} is the placeholder of the patch first name.
  • It works for every kind of patch: the fields of a TABLE_ROWS patch, such as {{ items.name }}, and the alt text of a picture or a chart.
  • Only the placeholders of the patches given lose their spaces. Other text that looks like a placeholder, such as {{ not_patched }}, is left as it is.
  • patchDetector takes trimPlaceholders too, and then gives the key of {{ customer_name }} as customer_name.

A placeholder is patched wherever it is in a paragraph's text: in a link's text, in a content control, in text typed with Track Changes on, or in a field's result. The patch's content goes where the placeholder was, so the text of a link stays linked, and a tracked insertion stays tracked:

import * as fs from "fs";
import { Document, ExternalHyperlink, InsertedTextRun, Packer, Paragraph, patchDocument, PatchType, TextRun } from "docx";

// The template, made here as it would be in Word: a link whose text is a placeholder, and a placeholder typed with Track Changes on
const template = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun("Our website: "),
                        new ExternalHyperlink({
                            link: "https://example.com",
                            children: [new TextRun({ text: "{{site}}", style: "Hyperlink" })],
                        }),
                    ],
                }),
                new Paragraph({
                    children: [
                        new InsertedTextRun({
                            text: "Dear {{customer_name}},",
                            id: 1,
                            author: "Template author",
                            date: "2024-05-01T00:00:00Z",
                        }),
                    ],
                }),
            ],
        },
    ],
});

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: await Packer.toBuffer(template),
    patches: {
        site: { type: PatchType.PARAGRAPH, children: [new TextRun("example.com")] },
        customer_name: { type: PatchType.PARAGRAPH, children: [new TextRun("John Smith")] },
    },
});

fs.writeFileSync("output.docx", doc);

Where the content can't go in what holds the placeholder, it goes as close as it can, so Word opens the document:

  • A link in a tracked insertion: a tracked insertion can't hold a link, so the link goes between two parts of the insertion and isn't tracked, as when a link is added with Track Changes off.
  • A link in a link: the patch's link takes the place of the template's link where the placeholder was, and the text around it keeps the template's link.
  • A plain text content control: given anything but text, such as a link, a picture, a field or a symbol, it becomes a rich text content control, as Word won't open a plain text one holding it.
  • A content control showing its placeholder text: if {{name}} is the control's placeholder text, the control shows the patch's content as its own, with the control's formatting rather than the grey of placeholder text.
  • A content control bound to custom XML or a document property: the binding is removed, as Word fills a bound control in from its data when it opens the document, which would put the placeholder back.

Deleted text, which Word shows struck through when Track Changes is on, isn't the document's text, so a placeholder in it isn't patched.

Basic Usage

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

// The template, made here as it would be in Word
const template = new Document({
    sections: [
        {
            children: [
                new Paragraph("Dear {{customer_name}},"),
                new Paragraph("Thank you for your order #{{order_number}}."),
                new Paragraph("Your items will ship on {{ship_date}}."),
                new Paragraph("Best regards,"),
                new Paragraph("{{company_name}}"),
            ],
        },
    ],
});

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: await Packer.toBuffer(template), // or fs.readFileSync("template.docx")
    patches: {
        customer_name: {
            type: PatchType.PARAGRAPH,
            children: [new TextRun("John Smith")],
        },
        order_number: {
            type: PatchType.PARAGRAPH,
            children: [new TextRun("12345")],
        },
        ship_date: {
            type: PatchType.PARAGRAPH,
            children: [new TextRun("January 15, 2024")],
        },
        company_name: {
            type: PatchType.PARAGRAPH,
            children: [new TextRun("Acme Corp")],
        },
    },
});

fs.writeFileSync("output.docx", doc);

Patch Types

The examples below patch simple-template.docx, which has placeholders such as {{name}}, {{paragraph_replace}} and {{table}}.

PARAGRAPH Type

Use PatchType.PARAGRAPH to replace with inline content (TextRun, images, hyperlinks):

import * as fs from "fs";
import { patchDocument, PatchType, TextRun } from "docx";

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: fs.readFileSync("./demo/assets/simple-template.docx"),
    patches: {
        name: {
            type: PatchType.PARAGRAPH,
            children: [new TextRun("Hello "), new TextRun({ text: "World", bold: true })],
        },
    },
});

fs.writeFileSync("My Document.docx", doc);

DOCUMENT Type

Use PatchType.DOCUMENT to replace with block-level content (paragraphs, tables):

import * as fs from "fs";
import { Paragraph, patchDocument, PatchType, Table, TableCell, TableRow } from "docx";

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: fs.readFileSync("./demo/assets/simple-template.docx"),
    patches: {
        paragraph_replace: {
            type: PatchType.DOCUMENT,
            children: [
                new Paragraph("First paragraph"),
                new Paragraph("Second paragraph"),
                new Table({
                    rows: [
                        new TableRow({
                            children: [
                                new TableCell({ children: [new Paragraph("First cell")] }),
                                new TableCell({ children: [new Paragraph("Second cell")] }),
                            ],
                        }),
                    ],
                }),
            ],
        },
    },
});

fs.writeFileSync("My Document.docx", doc);

TABLE_ROWS Type

Use PatchType.TABLE_ROWS to fill a table designed in the template with any number of rows. The table keeps its look, such as its header, borders and shading, and each row of data gets its own copy of the template's row.

The row to repeat holds the patch's fields: placeholders made of the patch's key, a dot and the field's name, such as {{items.name}} and {{items.price}} for the patch items. Each of rows gives the patches for one copy, by the name of the field they patch. A field's patch is a PARAGRAPH or DOCUMENT patch, as above.

import * as fs from "fs";
import { Document, IPatch, Packer, Paragraph, patchDocument, PatchType, ShadingType, Table, TableCell, TableRow, TextRun } from "docx";

// The template, made here as it would be in Word: a header, and a row with the fields
const header = (text: string) =>
    new TableCell({
        shading: { type: ShadingType.CLEAR, fill: "1F4E79" },
        children: [new Paragraph({ children: [new TextRun({ text, bold: true, color: "FFFFFF" })] })],
    });

const template = new Document({
    sections: [
        {
            children: [
                new Table({
                    columnWidths: [4000, 2000],
                    rows: [
                        new TableRow({ tableHeader: true, children: [header("Fruit"), header("Price")] }),
                        new TableRow({
                            children: [
                                new TableCell({ children: [new Paragraph("{{items.name}}")] }),
                                new TableCell({ children: [new Paragraph("£{{items.price}}")] }),
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

const text = (value: string): IPatch => ({ type: PatchType.PARAGRAPH, children: [new TextRun(value)] });

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: await Packer.toBuffer(template), // or fs.readFileSync("template.docx")
    patches: {
        items: {
            type: PatchType.TABLE_ROWS,
            rows: [
                { name: text("Apples"), price: text("0.40") },
                { name: text("Pears"), price: text("0.55") },
                { name: text("Figs"), price: text("0.30") },
            ],
        },
    },
});

fs.writeFileSync("My Document.docx", doc);
  • Other placeholders: The rows are repeated before the other patches are applied, so a placeholder of another patch in the repeated row, such as {{currency}}, is patched in every copy.
  • More than one row for each item: Rows next to each other that hold the fields are repeated together, such as a row for an item and a row under it for its notes.
  • Missing fields: A field that a row has no patch for, or whose patch is undefined, is left empty.
  • No rows: With no rows, the rows are removed and the rest of the table, such as its header, is kept. A table left with no rows at all is removed.
  • Tables in tables: A field's patch can be a TABLE_ROWS patch too, to repeat rows of a table in the repeated row. Its fields are then such as {{orders.items.name}}. When they are in the repeated row itself, rather than in a table in it, the row is repeated for each item, with its order's fields, and an order without items has no row.
  • Bookmarks and ids: Like content pasted in Word, only the first copy keeps the row's bookmarks, and the ids Word gives its paragraphs, rows and content controls, as these must be unique in a document.

Advanced Patches

Images

import * as fs from "fs";
import { ImageRun, patchDocument, PatchType } from "docx";

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: fs.readFileSync("./demo/assets/simple-template.docx"),
    patches: {
        image_test: {
            type: PatchType.PARAGRAPH,
            children: [
                new ImageRun({
                    type: "png",
                    data: fs.readFileSync("./demo/assets/images/dog.png"),
                    transformation: { width: 100, height: 90 },
                }),
            ],
        },
    },
});

fs.writeFileSync("My Document.docx", doc);

Replacing a Picture

To give a picture the template already has a new image, such as a placeholder photo or logo, put the placeholder in the picture's alt text rather than in the text, and patch it with an ImagePatch. The picture keeps its size, position, text wrapping, border, shape and effects, so it can be laid out and styled in Word:

  1. In Word, insert a placeholder picture, and size, place and style it as the document needs.
  2. Right-click the picture, choose Edit Alt Text, and type a placeholder as its description, such as {{photo}}.
  3. Patch the template with an ImagePatch for the placeholder's key, giving the new image as you would to an ImageRun:
import * as fs from "fs";
import { Document, ImagePatch, ImageRun, Packer, Paragraph, patchDocument } from "docx";

// The template, made here as it would be in Word: a picture with a border, with {{photo}} as its alt text's description
const template = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ImageRun({
                            type: "gif",
                            data: fs.readFileSync("./demo/assets/images/pizza.gif"),
                            transformation: { width: 200, height: 200 },
                            outline: { type: "solidFill", solidFillType: "rgb", value: "1F4E79", width: 38100 },
                            altText: { name: "Photo", description: "{{photo}}" },
                        }),
                    ],
                }),
            ],
        },
    ],
});

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: await Packer.toBuffer(template),
    patches: {
        photo: new ImagePatch({
            type: "png",
            data: fs.readFileSync("./demo/assets/images/dog.png"),
            altText: { description: "A puppy" },
        }),
    },
});

fs.writeFileSync("My Document.docx", doc);

An ImagePatch only looks in pictures' alt text, so {{photo}} typed in the document's text stays as it is. Every picture whose alt text holds the placeholder gets the new image, wherever it is: in the document, a table, a header, a footer or a footnote. patchDetector lists the placeholders in pictures' alt text along with those in text.

Property Type Notes Description
type "png", "jpg", "gif", "bmp" or "svg" Required The new image's format, as for an ImageRun
data Buffer, Uint8Array, ArrayBuffer or string Required The new image, or a base64 data URI of it
fallback { type, data } Optional For an SVG image, the PNG, JPEG, GIF or BMP image that applications that can't show SVG show instead. Required for "svg"
transformation { width: number; height: number } Optional The picture's new size, in pixels. Default is the template picture's size
altText { description?: string; title?: string } Optional The picture's alt text. Default is the template's, with the placeholder taken out. An empty description or title removes it

The new image fills the picture, so it is stretched to the template picture's proportions. Give a new image with other proportions a transformation that keeps its own:

import * as fs from "fs";
import { Document, ImagePatch, ImageRun, Packer, Paragraph, patchDocument } from "docx";

// A square placeholder picture
const template = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ImageRun({
                            type: "gif",
                            data: fs.readFileSync("./demo/assets/images/pizza.gif"),
                            transformation: { width: 200, height: 200 },
                            altText: { name: "Photo", description: "{{photo}}" },
                        }),
                    ],
                }),
            ],
        },
    ],
});

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: await Packer.toBuffer(template),
    patches: {
        // The cat is 625 by 417 pixels: 300 by 200 keeps its proportions
        photo: new ImagePatch({
            type: "jpg",
            data: fs.readFileSync("./demo/assets/images/cat.jpg"),
            transformation: { width: 300, height: 200 },
        }),
    },
});

fs.writeFileSync("My Document.docx", doc);

What changes, and what stays:

  • The image. The new image is added to the document, and the template's is removed, unless another picture shows it. A picture linked to a file gets the new image embedded.
  • The look. The picture's position, text wrapping, rotation, border, shape and effects, such as its transparency, are the template's. Word's crop of the template's image is removed, and so are Word's artistic effects, which keep a copy of the template's image.
  • The alt text. The placeholder is taken out of the description and title, unless altText is given. Describe the new image in altText, so screen readers can read it.

patchDocument throws an error that names the placeholder for a drawing that isn't a picture, such as a chart, a shape, a group of drawings or a drawing canvas. Ungroup a picture in Word to patch it. To give a chart new data, use ChartDataPatch (see Charts in Templates).

import * as fs from "fs";
import { ExternalHyperlink, patchDocument, PatchType, TextRun } from "docx";

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: fs.readFileSync("./demo/assets/simple-template.docx"),
    patches: {
        item_1: {
            type: PatchType.PARAGRAPH,
            children: [
                new ExternalHyperlink({
                    children: [
                        new TextRun({
                            text: "Visit our website",
                            style: "Hyperlink",
                        }),
                    ],
                    link: "https://example.com",
                }),
            ],
        },
    },
});

fs.writeFileSync("My Document.docx", doc);

Tables

import * as fs from "fs";
import { Paragraph, patchDocument, PatchType, Table, TableCell, TableRow } from "docx";

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: fs.readFileSync("./demo/assets/simple-template.docx"),
    patches: {
        table: {
            type: PatchType.DOCUMENT,
            children: [
                new Table({
                    rows: [
                        new TableRow({
                            children: [
                                new TableCell({ children: [new Paragraph("Item")] }),
                                new TableCell({ children: [new Paragraph("Price")] }),
                            ],
                        }),
                        new TableRow({
                            children: [
                                new TableCell({ children: [new Paragraph("Widget")] }),
                                new TableCell({ children: [new Paragraph("$9.99")] }),
                            ],
                        }),
                    ],
                }),
            ],
        },
    },
});

fs.writeFileSync("My Document.docx", doc);

Footnotes and endnotes

A patch refers to a footnote with a FootnoteReferenceRun, and the footnotes go in footnotes, as they do in a Document (see Footnotes). Endnotes work the same way, with EndnoteReferenceRun and endnotes:

import * as fs from "fs";
import { EndnoteReferenceRun, FootnoteReferenceRun, Paragraph, patchDocument, PatchType, TextRun } from "docx";

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: fs.readFileSync("./demo/assets/simple-template.docx"),
    patches: {
        name: {
            type: PatchType.PARAGRAPH,
            children: [new TextRun("John Doe"), new FootnoteReferenceRun(1)],
        },
        paragraph_replace: {
            type: PatchType.DOCUMENT,
            children: [
                new Paragraph({
                    children: [new TextRun("The report is due on Friday."), new EndnoteReferenceRun(1)],
                }),
            ],
        },
    },
    footnotes: {
        1: { children: [new Paragraph("Our new head of sales.")] },
    },
    endnotes: {
        1: { children: [new Paragraph("Send it to the whole team.")] },
    },
});

fs.writeFileSync("My Document.docx", doc);

The ids only link each reference to its note. In the patched document, each note is given an id that none of the template's own notes have, and Word numbers the notes in the order they appear. Each reference a patch inserts gets a note of its own, so a placeholder that is in the template twice gets two. Only the notes that a patch refers to are written.

A reference run can be given its note's content instead of an id, as in a Document, such as new FootnoteReferenceRun({ children: [new Paragraph("Our new head of sales.")] }), and then needs nothing in footnotes. The HTML of htmlToContent and htmlToRuns brings its footnotes this way. Word has notes only in a document's body, so a reference run with its own note is left out of a patch in a header or footer.

A template that has no footnotes or endnotes yet is given what they need, including the styles that show their numbers as superscript.

Lists

A paragraph in a patch is put in a numbered list with numbering, and the lists go in numbering, as they do in a Document (see Bullets and Numbering). A paragraph with bullet is put in a bullet list, as in a Document:

import * as fs from "fs";
import { LevelFormat, Paragraph, patchDocument, PatchType } from "docx";

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: fs.readFileSync("./demo/assets/simple-template.docx"),
    numbering: {
        config: [
            {
                reference: "steps",
                levels: [
                    { level: 0, format: LevelFormat.DECIMAL, text: "%1.", style: { paragraph: { indent: { left: 720, hanging: 360 } } } },
                    {
                        level: 1,
                        format: LevelFormat.LOWER_LETTER,
                        text: "%2)",
                        style: { paragraph: { indent: { left: 1440, hanging: 360 } } },
                    },
                ],
            },
        ],
    },
    patches: {
        paragraph_replace: {
            type: PatchType.DOCUMENT,
            children: [
                new Paragraph({ text: "Open the box", numbering: { reference: "steps", level: 0 } }),
                new Paragraph({ text: "Check that every part is there", numbering: { reference: "steps", level: 1 } }),
                new Paragraph({ text: "Put it together", numbering: { reference: "steps", level: 0 } }),
                new Paragraph({ text: "Keep the receipt", bullet: { level: 0 } }),
            ],
        },
    },
});

fs.writeFileSync("My Document.docx", doc);

Each list that the patches use is added to the template's lists, with an id that none of them has, so the template's own lists stay as they are. A list is numbered on from one patch to the next, as it is in a Document, and a paragraph with another instance starts it again. A template that has no lists yet is given what they need.

To add an item to a list that is already in the template, put its placeholder in a paragraph of the list, and patch it with PatchType.PARAGRAPH. The paragraph stays in the list, and the items after it are numbered on from it. For an item that is optional, a PatchType.DOCUMENT patch with no children removes the paragraph:

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

// A template with a list, whose second item is a placeholder
const item = (text: string): Paragraph => new Paragraph({ text, numbering: { reference: "packing-list", level: 0 } });
const template = await Packer.toBuffer(
    new Document({
        numbering: { config: [{ reference: "packing-list", levels: [{ level: 0, format: LevelFormat.DECIMAL, text: "%1." }] }] },
        sections: [{ children: [item("Water bottle"), item("{{lunch}}"), item("Raincoat")] }],
    }),
);

const bringsLunch = true;
const doc = await patchDocument({
    outputType: "nodebuffer",
    data: template,
    patches: {
        lunch: bringsLunch
            ? { type: PatchType.PARAGRAPH, children: [new TextRun("A packed lunch")] }
            : { type: PatchType.DOCUMENT, children: [] },
    },
});

fs.writeFileSync("My Document.docx", doc);

Headings and styles

A paragraph in a patch is made a heading with heading, as in a Document:

import * as fs from "fs";
import { HeadingLevel, Paragraph, patchDocument, PatchType } from "docx";

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: fs.readFileSync("./demo/assets/simple-template.docx"),
    patches: {
        paragraph_replace: {
            type: PatchType.DOCUMENT,
            children: [
                new Paragraph({ text: "Introduction", heading: HeadingLevel.HEADING_1 }),
                new Paragraph({ text: "Background", heading: HeadingLevel.HEADING_2 }),
                new Paragraph("The project started in the spring."),
            ],
        },
    },
});

fs.writeFileSync("My Document.docx", doc);

A heading has the look of the template's heading style. Word only saves the styles that a document uses, so a template may not have it yet, and Word would show the heading as body text. A heading style that the template doesn't have, or another of docx's styles such as ListParagraph or Hyperlink, is added to it as a Document writes it. The TOC1 to TOC9 styles of the entries of a table of contents indent each level by the size of the template's Normal font, as Word's do. To give the headings a look of your own, change the heading style in the template in Word, which saves it with the template.

A template saved by Word in another language has its styles under other ids, such as berschrift1 for Heading 1 in German. A heading is given the template's style of the same name, such as heading 1, so it has the template's look there too.

A style of your own, given with style, has to be in the template.

Page numbers

A template's table of contents, page references and numbers of pages show the page numbers the template was saved with, which the patches can change. Give patchDocument estimatePageNumbers from docx/layout as its pageNumbers, and it lays out the filled-in document's pages as Word would, and writes their numbers:

import * as fs from "fs";
import { patchDocument, PatchType, TextRun } from "docx";
import { estimatePageNumbers } from "docx/layout";

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: fs.readFileSync("Report template.docx"),
    patches: {
        name: { type: PatchType.PARAGRAPH, children: [new TextRun("John Doe")] },
    },
    pageNumbers: estimatePageNumbers,
});

Its page references and tables of contents are written clean, so Word shows the numbers as they are, rather than asking to update the fields when it opens the document. See Layout for what it follows, and what it leaves blank.

Preserving Styles

Set keepOriginalStyles: true to preserve the formatting of the placeholder text:

import * as fs from "fs";
import { patchDocument, PatchType, TextRun } from "docx";

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: fs.readFileSync("./demo/assets/simple-template-3.docx"),
    keepOriginalStyles: true, // Preserve template formatting
    patches: {
        salutation: {
            type: PatchType.PARAGRAPH,
            children: [new TextRun("Mr.")],
        },
        "first-name": {
            type: PatchType.PARAGRAPH,
            children: [new TextRun("John")],
        },
    },
});

fs.writeFileSync("My Document.docx", doc);

The text of a link, such as an ExternalHyperlink's, is formatted as the placeholder is too. Formatting that a run sets itself, such as the link's color, is kept.

Headers and Footers

Placeholders in headers and footers are also replaced:

import * as fs from "fs";
import { patchDocument, PatchType, TextRun } from "docx";

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: fs.readFileSync("./demo/assets/simple-template.docx"),
    patches: {
        header_adjective: {
            type: PatchType.PARAGRAPH,
            children: [new TextRun("great header")],
        },
        footer_text: {
            type: PatchType.PARAGRAPH,
            children: [new TextRun("Confidential")],
        },
    },
});

fs.writeFileSync("My Document.docx", doc);

Document Properties

Give patchDocument the document's properties, such as its title, author, tags and comments, which Word shows in File > Info:

import * as fs from "fs";
import { patchDocument, PatchType, TextRun } from "docx";

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: fs.readFileSync("./demo/assets/simple-template.docx"),
    patches: {
        name: {
            type: PatchType.PARAGRAPH,
            children: [new TextRun("John Doe")],
        },
    },
    properties: {
        title: "Letter to John Doe",
        creator: "Dolan Miu",
        keywords: "letter, customer",
        description: "Made from simple-template.docx",
    },
});

fs.writeFileSync("My Document.docx", doc);

They have the names that Document gives them (see Document Properties): title, subject, creator (the author), keywords (the tags), description (the comments), lastModifiedBy and revision. Each one replaces the template's, and an empty string clears it. The template's other properties, such as when it was created, are kept.

Complete Example

Invoice template:

import * as fs from "fs";
import { Document, Packer, Paragraph, patchDocument, PatchType, Table, TableCell, TableRow, TextRun } from "docx";

// Invoice data
const invoice = {
    number: "INV-2024-001",
    date: "January 10, 2024",
    customer: "Acme Corporation",
    items: [
        { name: "Widget A", qty: 5, price: 10.0 },
        { name: "Widget B", qty: 3, price: 15.0 },
    ],
    total: 95.0,
};

// The template, made here as it would be in Word
const cell = (text: string) => new TableCell({ children: [new Paragraph(text)] });
const template = new Document({
    sections: [
        {
            children: [
                new Paragraph("Invoice {{invoice_number}}"),
                new Paragraph("Date: {{invoice_date}}"),
                new Paragraph("Bill to: {{customer_name}}"),
                new Table({
                    rows: [
                        new TableRow({ tableHeader: true, children: [cell("Item"), cell("Qty"), cell("Price")] }),
                        new TableRow({
                            children: [cell("{{line_items.name}}"), cell("{{line_items.qty}}"), cell("${{line_items.price}}")],
                        }),
                    ],
                }),
                new Paragraph("Total: {{total}}"),
            ],
        },
    ],
});

const doc = await patchDocument({
    outputType: "nodebuffer",
    data: await Packer.toBuffer(template), // or fs.readFileSync("invoice-template.docx")
    patches: {
        invoice_number: {
            type: PatchType.PARAGRAPH,
            children: [new TextRun(invoice.number)],
        },
        invoice_date: {
            type: PatchType.PARAGRAPH,
            children: [new TextRun(invoice.date)],
        },
        customer_name: {
            type: PatchType.PARAGRAPH,
            children: [new TextRun(invoice.customer)],
        },
        line_items: {
            type: PatchType.TABLE_ROWS,
            rows: invoice.items.map((item) => ({
                name: { type: PatchType.PARAGRAPH, children: [new TextRun(item.name)] },
                qty: { type: PatchType.PARAGRAPH, children: [new TextRun(String(item.qty))] },
                price: { type: PatchType.PARAGRAPH, children: [new TextRun(item.price.toFixed(2))] },
            })),
        },
        total: {
            type: PatchType.PARAGRAPH,
            children: [
                new TextRun({
                    text: `$${invoice.total.toFixed(2)}`,
                    bold: true,
                }),
            ],
        },
    },
});

fs.writeFileSync("invoice-output.docx", doc);

A placeholder isn't replaced

patchDocument doesn't throw for a patch whose placeholder isn't in the template, as one set of patches can be given to templates that don't all have every placeholder. The placeholder is left as it is. To find such patches, compare their keys with the placeholders patchDetector finds, giving it the same placeholderDelimiters and trimPlaceholders as patchDocument:

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

// The template, with one placeholder typed in single braces
const template = await Packer.toBuffer(
    new Document({
        sections: [{ children: [new Paragraph("Dear {{customer_name}},"), new Paragraph("Your order: {order_number}")] }],
    }),
);

const patches = {
    customer_name: { type: PatchType.PARAGRAPH, children: [new TextRun("John Smith")] },
    order_number: { type: PatchType.PARAGRAPH, children: [new TextRun("12345")] },
};

// The patches whose placeholder isn't in the template. Here it's ["order_number"], as the template has {order_number}
const found = await patchDetector({ data: template });
const notFound = Object.keys(patches).filter((key) => !found.includes(key));
console.log("No placeholder for:", notFound);

const doc = await patchDocument({ outputType: "nodebuffer", data: template, patches });

fs.writeFileSync("output.docx", doc);

A patch whose placeholder isn't found is usually one of these:

  • The key has the delimiters: the key of {{customer_name}} is customer_name, not "{{customer_name}}", which is looked for as {{{{customer_name}}}}.
  • The key isn't spelled as the placeholder is: keys match exactly, including upper and lower case.
  • The template has other delimiters, such as {name}, <<name>> or [name]: give patchDocument and patchDetector the template's delimiters, such as placeholderDelimiters: { start: "<<", end: ">>" }.
  • Spaces inside the delimiters, such as {{ name }}: see Spaces inside placeholders.
  • The placeholder is only in a picture's or a chart's alt text: only an ImagePatch or a ChartDataPatch patches it, and a text patch leaves it as it is. patchDetector finds placeholders in alt text too, so the check above doesn't list this patch.

A placeholder in deleted text, which Word shows struck through when Track Changes is on, isn't patched or found, nor is one in a chart's own text, such as its title.

Tips

  • Placeholder naming: Use descriptive names like customer_name instead of x1
  • Testing: Open the template in Word to verify placeholders are formatted as plain text
  • Complex layouts: Design the layout in Word, then add placeholders where content varies
  • Reusable templates: Store templates in a dedicated folder for easy access

Demo

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/templates/patch-document.ts