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 Community is MIT-licensed and free, including commercial use. It edits existing .docx files in place in pure Python and does not render pages or convert to PDF. WolfDocx Commercial does not change those rights.

```
pip install wolfdocx
```

WolfDocx Commercial is an organization-scoped subscription. WolfDocx Community installs from public PyPI with pip install wolfdocx. WolfDocx Commercial installs from the authenticated WolfDocx package index with a per-purchase credential, issued on the checkout success page after payment and reissued through https://wolfxl.com/recover.

Review the scoped before-and-after memo files in the [reporting walkthrough](https://wolfsdk.com/blog/updating-existing-office-reporting-files), or [ask about your document](mailto:trial@wolfxl.com?subject=WolfDocx%20workflow%20fit) before purchasing if compatibility is uncertain.

[See WolfDocx Commercial](https://wolfdocx.com/)

### Related tasks

- [Update table cells in an existing Word document with Python](https://wolfdocx.com/update-docx-table-python)
- [Replace text in an existing Word document with Python](https://wolfdocx.com/replace-text-docx-python)
- [Fill a Word template table with rows from Python](https://wolfdocx.com/fill-word-template-python)
- [Word mail merge in Python: fill MERGEFIELD templates and repeating table rows](https://wolfdocx.com/mail-merge-docx-python)
- [Merge Word documents in Python without breaking numbering, headers, or styles](https://wolfdocx.com/merge-docx-python)
- [Update a Word table of contents in Python without opening Word](https://wolfdocx.com/update-toc-docx-python)
- [Convert DOCX to PDF in Python without Word or LibreOffice](https://wolfdocx.com/convert-docx-to-pdf-python)
- [A python-docx alternative for editing existing Word documents](https://wolfdocx.com/python-docx-alternative)
- [How can Python generate many legal contracts from one Word template?](https://wolfdocx.com/generate-contracts-docx-python)
- [Which Python workflow assembles one Word document from clauses and sections?](https://wolfdocx.com/document-assembly-python)
- [How do you prevent Word's unreadable content warning after a python-docx save?](https://wolfdocx.com/python-docx-unreadable-content)
- [Why does python-docx change Link to Previous on headers and footers?](https://wolfdocx.com/python-docx-header-footer-link-to-previous)
- [Can python-docx handle nested tables and complex headers?](https://wolfdocx.com/nested-tables-docx-python)
