Skip to content

Commit a809d6c

Browse files
committed
comments: add Comment.text
1 parent e3a321d commit a809d6c

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

features/doc-add-comment.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Add a comment to a document
44
I need a way to add a comment specifying both its content and its reference
55

66

7-
@wip
87
Scenario: Document.add_comment(runs, text, author, initials)
98
Given a document having a comments part
109
When I assign comment = document.add_comment(runs, "A comment", "John Doe", "JD")

src/docx/comments.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ def initials(self) -> str | None:
144144
def initials(self, value: str | None):
145145
self._comment_elm.initials = value
146146

147+
@property
148+
def text(self) -> str:
149+
"""The text content of this comment as a string.
150+
151+
Only content in paragraphs is included and of course all emphasis and styling is stripped.
152+
153+
Paragraph boundaries are indicated with a newline ("\n")
154+
"""
155+
return "\n".join(p.text for p in self.paragraphs)
156+
147157
@property
148158
def timestamp(self) -> dt.datetime | None:
149159
"""The date and time this comment was authored.

tests/test_comments.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,26 @@ def it_knows_the_date_and_time_it_was_authored(self, comments_part_: Mock):
209209

210210
assert comment.timestamp == dt.datetime(2023, 10, 1, 12, 34, 56, tzinfo=dt.timezone.utc)
211211

212+
@pytest.mark.parametrize(
213+
("cxml", "expected_value"),
214+
[
215+
("w:comment{w:id=42}", ""),
216+
('w:comment{w:id=42}/w:p/w:r/w:t"Comment text."', "Comment text."),
217+
(
218+
'w:comment{w:id=42}/(w:p/w:r/w:t"First para",w:p/w:r/w:t"Second para")',
219+
"First para\nSecond para",
220+
),
221+
(
222+
'w:comment{w:id=42}/(w:p/w:r/w:t"First para",w:p,w:p/w:r/w:t"Second para")',
223+
"First para\n\nSecond para",
224+
),
225+
],
226+
)
227+
def it_can_summarize_its_content_as_text(
228+
self, cxml: str, expected_value: str, comments_part_: Mock
229+
):
230+
assert Comment(cast(CT_Comment, element(cxml)), comments_part_).text == expected_value
231+
212232
def it_provides_access_to_the_paragraphs_it_contains(self, comments_part_: Mock):
213233
comment_elm = cast(
214234
CT_Comment,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy