File tree Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,11 @@ def __len__(self) -> int:
29
29
"""The number of comments in this collection."""
30
30
return len (self ._comments_elm .comment_lst )
31
31
32
+ def get (self , comment_id : int ) -> Comment | None :
33
+ """Return the comment identified by `comment_id`, or |None| if not found."""
34
+ comment_elm = self ._comments_elm .get_comment_by_id (comment_id )
35
+ return Comment (comment_elm , self ._comments_part ) if comment_elm is not None else None
36
+
32
37
33
38
class Comment (BlockItemContainer ):
34
39
"""Proxy for a single comment in the document.
Original file line number Diff line number Diff line change @@ -19,6 +19,11 @@ class CT_Comments(BaseOxmlElement):
19
19
20
20
comment = ZeroOrMore ("w:comment" )
21
21
22
+ def get_comment_by_id (self , comment_id : int ) -> CT_Comment | None :
23
+ """Return the `w:comment` element identified by `comment_id`, or |None| if not found."""
24
+ comment_elms = self .xpath (f"(./w:comment[@w:id='{ comment_id } '])[1]" )
25
+ return comment_elms [0 ] if comment_elms else None
26
+
22
27
23
28
class CT_Comment (BaseOxmlElement ):
24
29
"""`w:comment` element, representing a single comment.
Original file line number Diff line number Diff line change
1
+ # pyright: reportPrivateUsage=false
2
+
1
3
"""Unit test suite for the docx.comments module."""
2
4
3
5
from __future__ import annotations
@@ -63,6 +65,26 @@ def it_is_iterable_over_the_comments_it_contains(self, package_: Mock):
63
65
with pytest .raises (StopIteration ):
64
66
next (comment_iter )
65
67
68
+ def it_can_get_a_comment_by_id (self , package_ : Mock ):
69
+ comments_elm = cast (
70
+ CT_Comments ,
71
+ element ("w:comments/(w:comment{w:id=1},w:comment{w:id=2},w:comment{w:id=3})" ),
72
+ )
73
+ comments = Comments (
74
+ comments_elm ,
75
+ CommentsPart (
76
+ PackURI ("/word/comments.xml" ),
77
+ CT .WML_COMMENTS ,
78
+ comments_elm ,
79
+ package_ ,
80
+ ),
81
+ )
82
+
83
+ comment = comments .get (2 )
84
+
85
+ assert type (comment ) is Comment , "expected a `Comment` object"
86
+ assert comment ._comment_elm is comments_elm .comment_lst [1 ]
87
+
66
88
# -- fixtures --------------------------------------------------------------------------------
67
89
68
90
@pytest .fixture
You can’t perform that action at this time.
0 commit comments