WolfSDKWolfXLWolfPPTWolfDocx

WolfDocx - text replacement

Replace text in an existing Word document with Python

Swap one approved phrase in a paragraph and keep the bold, italic, and font settings around it. WolfDocx locates the phrase inside a discovered paragraph, patches only the text nodes it covers, and writes a new .docx when the result verifies.

Replace one phrase inside a paragraph

from wolfdocx.reporting import author_report, discover_report_targets, parse_report_request

found = discover_report_targets("board_memo.docx")
status = next(
    t for t in found["targets"]
    if t["kind"] == "text" and "Draft" in t["expected_old_text"]
)
report = author_report(parse_report_request({
    "source": "board_memo.docx",
    "destination": "board_memo_final.docx",
    "source_sha256": found["source_sha256"],
    "edits": [{**status, "expected_old_text": "Draft", "text": "Final"}],
}))
assert report.passed, report.code

The phrase may sit inside one run or span several simple runs. The replacement takes the formatting of the run holding the phrase's first character, and all other run and paragraph markup stays as it was. Headers and footers work the same way when you name their part, for example discover_report_targets(path, part="word/header1.xml").

When to use WolfDocx and when to use python-docx

  • python-docx exposes paragraphs and runs and has no built-in find and replace, so a phrase that Word split across runs needs custom code. WolfDocx handles that split for supported runs.
  • Use WolfDocx when every replacement must match the text you reviewed and anything unexpected should stop the write.
  • Use python-docx for general document generation and broad read and write access.

Limitations

  • The phrase must occur exactly once in the selected paragraph; ambiguous matches are refused.
  • Text that intersects fields, tracked revisions, comments, content controls, or unsupported inline structures is refused.
  • Leading or trailing spaces in a replacement require the text node to already preserve whitespace.
  • WolfDocx does not lay out pages and makes no general Word compatibility promise.

Get WolfDocx

WolfDocx Commercial is an organization-scoped subscription. Commercial package access uses the authenticated distribution channel. The package credential is issued on the checkout success page after payment and can be reissued through https://wolfxl.com/recover. There is no public free package.

Review the scoped before-and-after memo files in the reporting walkthrough, or ask about your document before purchasing if compatibility is uncertain.

Related tasks