* chore: organise the demos into a folder for each topic The demos were one flat list of 124 numbered files, which made it hard to find the one you wanted. Each is now in a folder for its topic, such as demo/tables/basic-table.ts, with a name that says what it shows instead of a number. npm run demo lists every demo and narrows the list as you type. It also takes a demo's path or name, a topic, or search words. The demos' images moved to demo/assets/images, as images is now a topic. The docs' links, the Demos workflow, the shape render script and its reference images use the new paths and names. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ * fix: list only a topic's own demos, and drop stale output file names npm run demo text also offered the text-boxes demos and others with "text" in their name, so a topic now searches for "text/". Three demos logged their old numbered file name after writing My Document.docx. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
3.8 KiB
Patcher
The patcher allows you to modify existing documents, and add new content to them.
!> The Patcher requires an understanding of Paragraphs.
Usage
import * as fs from "fs";
import { patchDocument } from "docx";
patchDocument(fs.readFileSync("My Document.docx"), {
patches: {
// Patches here
},
});
Patches
The patcher takes in a patches object, which is a map of string to Patch:
interface Patch {
type: PatchType;
children: FileChild[] | ParagraphChild[];
}
| Property | Type | Notes | Possible Values |
|---|---|---|---|
| type | PatchType |
Required | DOCUMENT, PARAGRAPH |
| children | FileChild[] or ParagraphChild[] |
Required | The contents to replace with. A FileChild is a Paragraph or Table, whereas a ParagraphChild is typical Paragraph children. |
The patcher also takes in a keepOriginalStyles boolean, which will preserve the styles of the patched text when set to true.
A patch can also be for a drawing whose alt text holds the placeholder, rather than for text: ChartDataPatch from docx/charts gives a chart made in Word new data, keeping its look. See Charts in Templates.
How to patch existing document
- Open your existing word document in your favorite Word Processor
- Write tags in the document where you want to patch in a mustache style notation. For example,
{{my_patch}}and{{my_second_patch}}. - Run the patcher with the patches as a key value pair.
Example
Word Document
Patcher
?> Notice how there is no handlebar notation in the key.
The patch can be as simple as a string, or as complex as a table. Images, hyperlinks, charts from docx/charts (see Charts in Templates), and other complex elements within the docx library are also supported.
patchDocument(fs.readFileSync("My Document.docx"), {
patches: {
my_patch: {
type: PatchType.PARAGRAPH,
children: [new TextRun("Sir. "), new TextRun("John Doe"), new TextRun("(The Conqueror)")],
},
my_second_patch: {
type: PatchType.DOCUMENT,
children: [
new Paragraph("Lorem ipsum paragraph"),
new Paragraph("Another paragraph"),
new Paragraph({
children: [
new TextRun("This is a "),
new ExternalHyperlink({
children: [
new TextRun({
text: "Google Link",
}),
],
link: "https://www.google.co.uk",
}),
new ImageRun({
type: "png",
data: fs.readFileSync("./demo/assets/images/dog.png"),
transformation: { width: 100, height: 100 },
}),
],
}),
],
},
},
});
Demo
Source: https://github.com/dolanmiu/docx/blob/master/demo/templates/patch-document.ts
