MCPcopy Index your code
hub / github.com/python-openxml/python-docx / Comment

Class Comment

src/docx/comments.py:83–163  ·  view source on GitHub ↗

Proxy for a single comment in the document. Provides methods to access comment metadata such as author, initials, and date. A comment is also a block-item container, similar to a table cell, so it can contain both paragraphs and tables and its paragraphs can contain rich text, hyperlin

Source from the content-addressed store, hash-verified

81
82
83class Comment(BlockItemContainer):
84 """Proxy for a single comment in the document.
85
86 Provides methods to access comment metadata such as author, initials, and date.
87
88 A comment is also a block-item container, similar to a table cell, so it can contain both
89 paragraphs and tables and its paragraphs can contain rich text, hyperlinks and images,
90 although the common case is that a comment contains a single paragraph of plain text like a
91 sentence or phrase.
92
93 Note that certain content like tables may not be displayed in the Word comment sidebar due to
94 space limitations. Such "over-sized" content can still be viewed in the review pane.
95 """
96
97 def __init__(self, comment_elm: CT_Comment, comments_part: CommentsPart):
98 super().__init__(comment_elm, comments_part)
99 self._comment_elm = comment_elm
100
101 def add_paragraph(self, text: str = "", style: str | ParagraphStyle | None = None) -> Paragraph:
102 """Return paragraph newly added to the end of the content in this container.
103
104 The paragraph has `text` in a single run if present, and is given paragraph style `style`.
105 When `style` is |None| or ommitted, the "CommentText" paragraph style is applied, which is
106 the default style for comments.
107 """
108 paragraph = super().add_paragraph(text, style)
109
110 # -- have to assign style directly to element because `paragraph.style` raises when
111 # -- a style is not present in the styles part
112 if style is None:
113 paragraph._p.style = "CommentText" # pyright: ignore[reportPrivateUsage]
114
115 return paragraph
116
117 @property
118 def author(self) -> str:
119 """Read/write. The recorded author of this comment.
120
121 This field is required but can be set to the empty string.
122 """
123 return self._comment_elm.author
124
125 @author.setter
126 def author(self, value: str):
127 self._comment_elm.author = value
128
129 @property
130 def comment_id(self) -> int:
131 """The unique identifier of this comment."""
132 return self._comment_elm.id
133
134 @property
135 def initials(self) -> str | None:
136 """Read/write. The recorded initials of the comment author.
137
138 This attribute is optional in the XML, returns |None| if not set. Assigning |None| removes
139 any existing initials from the XML.
140 """

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…