forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Phase D.10: Search and replace with formatting preservation #23
Copy link
Copy link
Open
Labels
Description
Add document-wide search and replace that preserves run formatting. Upstream #30 (39 comments).
API Design
- document.search(text) — returns list of SearchMatch objects
- document.replace(old_text, new_text) — replaces all occurrences, preserving the formatting of the first character's run
- SearchMatch: .paragraph, .paragraph_index, .run_indices, .start, .end
- Options: case_sensitive (default True), whole_word (default False)
Challenges
- Text may span multiple runs (e.g., "hel" in one run, "lo" in another)
- Replacement must merge/split runs appropriately
- Must preserve bold/italic/font of the original runs
Implementation
- Walk all paragraphs, reconstruct full text from runs with character-to-run mapping
- Find matches in the full text
- Map match positions back to runs
- Replace text in the affected runs, splitting if needed
Upstream issue: #30
Reactions are currently unavailable