PPTX
Patching
Modify existing .pptx files by replacing placeholders with new content
Patch an existing .pptx template by replacing {{placeholder}} tokens with new text runs.
patchPresentation
Replaces placeholders in an existing .pptx file:
import { patchPresentation, PatchType, TextRun, Presentation, Packer } from "@office-open/pptx";
import { writeFileSync } from "node:fs";
// Create a template with placeholders
const templatePres = new Presentation({
title: "Patch Demo",
slides: [
{
children: [
{
shape: {
x: 80,
y: 120,
width: 720,
height: 80,
textBody: {
children: [{ children: [{ text: "Hello {{name}}!" }] }],
},
},
},
],
},
],
});
const templateBuffer = await Packer.toBuffer(templatePres);
// Patch the template
const result = await patchPresentation({
outputType: "nodebuffer",
data: templateBuffer,
patches: {
name: {
type: PatchType.PARAGRAPH,
children: [new TextRun({ text: "World", bold: true, fontSize: 24 })],
},
},
});
writeFileSync("output.pptx", result);
PatchType
| Type | Description |
|---|---|
PatchType.PARAGRAPH | Replace the placeholder with inline run-level content |
The patch replaces placeholder text inside a paragraph with new runs. The original run's formatting properties (font, size, color, bold, etc.) are preserved by default.
patches: {
title: {
type: PatchType.PARAGRAPH,
children: [
new TextRun({ text: "Hello ", bold: true }),
new TextRun("World"),
],
},
}
Custom Delimiters
Default delimiters are {{ and }}. Change them with placeholderDelimiters:
await patchPresentation({
outputType: "nodebuffer",
data: templateBuffer,
patches: { name: { type: PatchType.PARAGRAPH, children: [new TextRun("John")] } },
placeholderDelimiters: { start: "<<", end: ">>" },
});
Options
| Option | Type | Default | Description |
|---|---|---|---|
outputType | string | — | Output format (see Export page) |
data | Buffer | Uint8Array | ... | — | Input .pptx file data |
patches | Record<string, IPatch> | — | Map of placeholder name to patch content |
keepOriginalStyles | boolean | true | Preserve original run formatting properties |
placeholderDelimiters | { start: string, end: string} | { {{, }} } | Custom placeholder delimiters |
Tips
- Placeholders span across split runs in PowerPoint — the library handles this automatically.
keepOriginalStylesdefaults totrue, preserving the template's run formatting (font, size, color, etc.).- All slides are processed — the same placeholder on multiple slides will be replaced everywhere.
- Image media files in
ppt/media/can be replaced by includingdata(Uint8Array) in the patch.