Replace Text in DOCX and PPTX
replace_text(needle, replacement) walks every <w:t> element in a DOCX (body, headers, footers) and every <a:t> across every slide and notes-slide in a PPTX. It returns the number of replacements made.
This is the right tool for templating: define {{placeholders}} in your source document, fill them at runtime.
Single-pass replace
Python
from office_oxide import EditableDocument
with EditableDocument.open("contract.docx") as ed:
n = ed.replace_text("{{client_name}}", "Acme Corp")
print(f"{n} replacements")
ed.save("contract_acme.docx")
Rust
use office_oxide::edit::EditableDocument;
let mut ed = EditableDocument::open("contract.docx")?;
let n = ed.replace_text("{{client_name}}", "Acme Corp");
println!("{n} replacements");
ed.save("contract_acme.docx")?;
JavaScript
import { EditableDocument } from 'office-oxide';
using ed = EditableDocument.open('contract.docx');
const n = ed.replaceText('{{client_name}}', 'Acme Corp');
console.log(`${n} replacements`);
ed.save('contract_acme.docx');
Go
ed, _ := officeoxide.OpenEditable("contract.docx")
defer ed.Close()
n, _ := ed.ReplaceText("{{client_name}}", "Acme Corp")
fmt.Printf("%d replacements\n", n)
ed.Save("contract_acme.docx")
C#
using var ed = EditableDocument.Open("contract.docx");
long n = ed.ReplaceText("{{client_name}}", "Acme Corp");
Console.WriteLine($"{n} replacements");
ed.Save("contract_acme.docx");
Multi-key fill-in
Apply many replacements before saving — much cheaper than re-opening the file each time.
Python
fields = {
"{{client_name}}": "Acme Corp",
"{{contract_date}}": "2026-04-19",
"{{amount}}": "$120,000",
"{{tier}}": "Enterprise",
}
with EditableDocument.open("contract.docx") as ed:
for needle, value in fields.items():
ed.replace_text(needle, value)
ed.save("contract_acme.docx")
Rust
let fields = [
("{{client_name}}", "Acme Corp"),
("{{contract_date}}", "2026-04-19"),
("{{amount}}", "$120,000"),
("{{tier}}", "Enterprise"),
];
let mut ed = EditableDocument::open("contract.docx")?;
for (needle, value) in fields {
ed.replace_text(needle, value);
}
ed.save("contract_acme.docx")?;
JavaScript
const fields = {
'{{client_name}}': 'Acme Corp',
'{{contract_date}}': '2026-04-19',
'{{amount}}': '$120,000',
'{{tier}}': 'Enterprise',
};
using ed = EditableDocument.open('contract.docx');
for (const [k, v] of Object.entries(fields)) ed.replaceText(k, v);
ed.save('contract_acme.docx');
PPTX templating
Same API, same semantics — replace_text walks every slide and every notes-slide.
Python
with EditableDocument.open("deck_template.pptx") as ed:
ed.replace_text("{{quarter}}", "Q4 2026")
ed.replace_text("{{growth}}", "+18.4%")
ed.save("q4_deck.pptx")
Notes about run boundaries
DOCX and PPTX split text into “runs” — contiguous spans of identically-styled characters. Word may render {{name}} as three separate runs internally if the user pasted or auto-corrected mid-token. replace_text handles cross-run matches transparently: it merges adjacent runs of the same style window before searching.
If your placeholders use special characters or styled spans, prefer simple ASCII tokens like {{name}} over Word’s “smart quotes” — that minimizes the cross-run merging cost.
XLSX is different
replace_text returns 0 on XLSX because spreadsheet text lives in the shared-strings table and cell formulas, not in <w:t> / <a:t> elements. Use set_cell instead.
See also
- Set XLSX cells — for spreadsheet edits
- Editing overview — what
EditableDocumentpreserves - Batch processing — running templates across many records