Skip to content

add_comment() references the "CommentReference" character style without defining it #1609

Description

@Dmitry-Kov

Document.add_comment() (via Run.mark_comment_range) writes the comment reference run with rStyle w:val="CommentReference", but nothing adds that style to styles.xml. A default Document() template doesn't contain it either. Word and LibreOffice both tolerate a dangling rStyle silently and format the reference mark with the run's default properties instead of the built-in 8pt "annotation reference" style, so the mark just looks slightly wrong with no warning anywhere.

Repro, python-docx 1.2.0:

from docx import Document
import zipfile

d = Document()
p = d.add_paragraph("The fee is EUR 12,000 per milestone.")
d.add_comment(runs=p.runs[0], text="Confirm this figure", author="Reviewer")
d.save("out.docx")

z = zipfile.ZipFile("out.docx")
doc = z.read("word/document.xml").decode()
sty = z.read("word/styles.xml").decode()
print('rStyle in document.xml:', doc.count('w:val="CommentReference"'))
print('style defined in styles.xml:', sty.count('w:styleId="CommentReference"'))

# rStyle in document.xml: 1
# style defined in styles.xml: 0

Suggested fix: when adding a comment, add the style definition if the document lacks it. What Word writes is:

<w:style w:type="character" w:styleId="CommentReference">
  <w:name w:val="annotation reference"/>
  <w:basedOn w:val="DefaultParagraphFont"/>
  <w:uiPriority w:val="99"/>
  <w:semiHidden/><w:unhideWhenUsed/>
  <w:rPr><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr>
</w:style>

Alternatively, omit the rStyle altogether when the style isn't defined — a reference mark with default formatting is at least self-consistent.

Filing this separately per the discussion in #1605. The same gap existed in adeu and was fixed in their 3.0.3.

python-docx 1.2.0, Python 3.9.6, macOS

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions