Skip to content

Commit cab50c5

Browse files
committed
comments: add Comment.initials
1 parent 8af46fe commit cab50c5

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

features/cmt-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Feature: Get comment properties
1414
Then comment.author is the author of the comment
1515

1616

17-
@wip
1817
Scenario: Comment.initials
1918
Given a Comment object
2019
Then comment.initials is the initials of the comment author

src/docx/comments.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,12 @@ def author(self) -> str:
6262
def comment_id(self) -> int:
6363
"""The unique identifier of this comment."""
6464
return self._comment_elm.id
65+
66+
@property
67+
def initials(self) -> str | None:
68+
"""Read/write. The recorded initials of the comment author.
69+
70+
This attribute is optional in the XML, returns |None| if not set. Assigning |None| removes
71+
any existing initials from the XML.
72+
"""
73+
return self._comment_elm.initials

src/docx/oxml/comments.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from docx.oxml.simpletypes import ST_DecimalNumber, ST_String
6-
from docx.oxml.xmlchemy import BaseOxmlElement, RequiredAttribute, ZeroOrMore
6+
from docx.oxml.xmlchemy import BaseOxmlElement, OptionalAttribute, RequiredAttribute, ZeroOrMore
77

88

99
class CT_Comments(BaseOxmlElement):
@@ -36,3 +36,6 @@ class CT_Comment(BaseOxmlElement):
3636

3737
id: int = RequiredAttribute("w:id", ST_DecimalNumber) # pyright: ignore[reportAssignmentType]
3838
author: str = RequiredAttribute("w:author", ST_String) # pyright: ignore[reportAssignmentType]
39+
initials: str | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
40+
"w:initials", ST_String
41+
)

tests/test_comments.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ def it_knows_its_author(self, comments_part_: Mock):
107107

108108
assert comment.author == "Steve Canny"
109109

110+
def it_knows_the_initials_of_its_author(self, comments_part_: Mock):
111+
comment_elm = cast(CT_Comment, element("w:comment{w:id=42,w:initials=SJC}"))
112+
comment = Comment(comment_elm, comments_part_)
113+
114+
assert comment.initials == "SJC"
115+
110116
# -- fixtures --------------------------------------------------------------------------------
111117

112118
@pytest.fixture

0 commit comments

Comments
 (0)