search
search
¶
Text search and replace across the runs of a paragraph.
The public entry points are Paragraph.replace_text, BlockItemContainer.replace_text and Document.replace_text; this module holds the machinery they share.
Matching is done against the paragraph's text as a whole, so a match is found whether or
not Word happened to split it across runs. Replacement is performed by
docx.oxml.text.isolate.replace_range, which preserves the formatting of the
surrounding text.
compile_pattern
¶
Return the compiled pattern matching old.
A literal old is escaped, so text containing regex metacharacters — a "$" in a
price, the "." in a file name — matches itself rather than being interpreted.
Source code in src/docx/text/search.py
replace_in_paragraph
¶
replace_in_paragraph(
p: CT_P,
pattern: Pattern[str],
new: str,
count: int = -1,
regex: bool = False,
) -> int
Replace up to count matches of pattern in p with new, returning how many.
count of -1 replaces every match. When regex is True, new may refer to capture
groups as \1 or \g<name>; otherwise it is used literally.
Each replacement changes the paragraph's text, so the next match is searched for
against the updated text, starting past the text just written. That means a
replacement containing the pattern is not re-matched, and replace_text("a", "aa")
terminates.