DOCX
Patching
Modify an existing .docx through canonical DocumentOptions commands
patchDocument parses an existing .docx into DocumentOptions, applies ordered commands, and regenerates the package. Canonical round-trip fields preserve package facts that are not part of the edited subtree.
#Commands
| Command | Purpose |
|---|---|
insert | Add canonical items to an array field or nested collection |
replace | Replace a canonical field/node or selected text |
update | Deep-merge object fields; arrays are replaced as a whole |
remove | Remove a canonical node or selected text |
move | Reorder an item in a canonical array |
import { patchDocument } from "@office-open/docx";
const result = await patchDocument({
data: template,
commands: [
{
type: "replace",
at: { field: "sections", route: [{ index: 0 }], text: { placeholder: "name" } },
value: "John Doe",
},
{
type: "insert",
at: { field: "sections", route: [{ index: 0 }, { collection: "children" }] },
position: "end",
value: [{ paragraph: { children: ["Appendix"] } }],
},
{ type: "update", at: { field: "title" }, set: "Quarterly Report" },
],
});
field is a DocumentOptions root field. route selects nested nodes by property, array index, or a native identity such as id, name, reference, or address. Text matches are singular unless all: true; a missing match throws PatchError.
#Options
| Option | Type | Default |
|---|---|---|
data | DataType | required |
commands | readonly DocumentPatchCommand[] | required |
outputType | OutputType | "uint8array" |
packageVariant | "standard" | "macro" | "template" | "macroTemplate" | "standard" |
placeholderDelimiters | { start, end } | { start: "{{", end: "}}" } |