fix: ensure CommentReference character style is defined when adding a comment - #1610
Open
VenkateswarluNagineni wants to merge 1 commit into
Conversation
… comment (python-openxmlgh-1609) add_comment() writes <w:rStyle w:val="CommentReference"/> into the reference run but never adds the matching style definition to styles.xml. A default Document() template doesn't contain it either, so the reference mark ends up with a dangling rStyle and falls back to default run formatting instead of the intended 8-pt "annotation reference" appearance. Styles._ensure_comment_reference_style() is added to register the built-in style on first use. Document.add_comment() calls it before marking the comment range. The method is idempotent: repeated calls and documents that already carry the style from a prior Word session are left unchanged. The style definition matches what Word 365 writes: • name: "annotation reference" • styleId: "CommentReference" (matches the id the reference run uses) • basedOn: "DefaultParagraphFont" • uiPriority: 99 / semiHidden / unhideWhenUsed • rPr: sz 8 pt (16 half-points) Acceptance test: doc-add-comment.feature scenario verifies that styles.xml contains a CHARACTER style with styleId "CommentReference" after add_comment() is called on a fresh document. Fixes python-openxml#1609
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.
What this fixes
Closes #1609.
Document.add_comment()writes<w:rStyle w:val="CommentReference"/>into thereference run but never adds the matching style definition to
styles.xml. The defaultDocument()template doesn't contain it either, so the reference mark ends up with adangling
rStyleand falls back to default run formatting instead of the intended 8-pt"annotation reference" appearance. Word and LibreOffice tolerate this silently, but the
mark is visually wrong and opens a subtle trap for any caller that inspects styles.
Fix
Styles._ensure_comment_reference_style()registers the built-in style on first use.Document.add_comment()calls it before marking the comment range. The method isidempotent — documents that already carry the style (e.g., re-opened Word files) are
left unchanged.
The style definition matches what Word 365 writes:
w:nameannotation referencew:styleIdCommentReference(matches the id the reference run uses)w:basedOnDefaultParagraphFontw:uiPriority99w:semiHidden/w:unhideWhenUsedw:rPr/w:sz16(8 pt in half-points)Changes
src/docx/styles/styles.py: add_ensure_comment_reference_style()+Ptimportsrc/docx/document.py: callself.styles._ensure_comment_reference_style()inadd_comment()features/doc-add-comment.feature: new acceptance scenariofeatures/steps/comments.py:givenstep for fresh document without the style +thenassertion stepTesting
Acceptance tests require
behave(not available in this environment); the new scenario isreadable and can be verified by inspection.
Verification script: