Repository navigation
Keep comment anchors when assigning Paragraph.text - #1645
Open
Dmitry-Kov wants to merge 1 commit into
Open
Dmitry-Kov wants to merge 1 commit into
Dmitry-Kov wants to merge 1 commit into
Conversation
Assigning `Paragraph.text` calls `.clear()`, which removed the paragraph's `w:commentRangeStart` / `w:commentRangeEnd` markers and the run holding `w:commentReference`. The comment stayed in comments.xml anchored to nothing, and Word dropped it from view without a warning (python-openxml#1604). Keep those markers and put them back around the new run: range starts before it, range ends and reference runs after it, each in document order. Only markers that were in this paragraph come back, so a comment range that starts or ends in another paragraph is neither duplicated nor cut short. Markers inside a hyperlink, which the setter also removes, are kept as well. The range now covers the whole new run, not its original span. Footnote references and `w:ins` / `w:del` are still removed; python-openxml#1604 stays open for those. Takes over python-openxml#1605 by Gyanu Mayank, closed unmerged with an invitation for anyone to pick it up. Co-authored-by: Gyanu Mayank <gyanumayank0@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Takes over #1605 by @gyanu2507, which was closed unmerged with an open invitation to pick it up. The commit keeps their credit with a
Co-authored-bytrailer. This fixes the comment half of #1604. Footnotes and tracked changes are still affected, so #1604 should stay open.Problem
Paragraph.text = ...callsclear(), which removes every child of the paragraph exceptw:pPr. That includesw:commentRangeStart/w:commentRangeEnd(these are paragraph children, not run content) and the run holdingw:commentReference. The<w:comment>stays incomments.xmlanchored to nothing. Word opens the file without a warning and doesn't show the comment.Change
Before clearing, the setter collects the paragraph's comment markers. It then puts them back around the new run: each
w:commentRangeStartbefore it, and eachw:commentRangeEndand comment-reference run after it, both groups in document order. The original elements are moved, not rebuilt, so whatever Word wrote on them (including a localized referencerStyle) is kept.A comment that covered part of the paragraph now covers the whole new run, not its original word span. For a full-text replacement that's the only outcome that makes sense, and it matches what Word does when you retype a commented passage. The docstring says so.
Differences from #1605:
commentRangeStartit found. So for a comment spanning two paragraphs (add_comment()supports this), assigning text to the first paragraph gave the comment a secondcommentRangeEndandcommentReference. Assigning text to the second paragraph still left it orphaned.w:hyperlinkare kept too.add_comment()places them there when the anchor run is inside a hyperlink, and the setter removes the hyperlink.ruff format.Not fixed here (#1604 stays open)
footnotes.xml/endnotes.xmlwith nothing referencing it.w:ins/w:delaround the runs are still removed.add_comment()writesrStyle="CommentReference"without defining that style (add_comment() references the "CommentReference" character style without defining it #1609).Tests
it_keeps_only_the_comment_markers_it_contains_when_text_is_assignedis parametrized over:w:pPrstaying firstpar-set-text.feature, run against the existing Word-authoredcomments-rich-para.docx.master.pytest: 1615 passed.behave: 651 scenarios passed.ruff checkandruff format --checkare clean, and pyright reports nothing new forparagraph.py(Python 3.9, deps fromuv.lock).Checked against the saved files
Before/after pairs produced through
paragraph.textand checked withooxml-integrity check out.docx --against in.docx(0.9.0). Disclosure: I maintain that tool.master(1.2.0)comments-rich-para.docx, text set on the commented paragraphcommentRangeEndwithout startcommentRangeEnd+commentReferencefor the same id (FID002 anchors 1 → 2)The only other findings are FID014, which is the intended text change, and STY001 (#1609), which is already present in the python-docx-generated source files.
🤖 Generated with Claude Code