🌐 AI搜索 & 代理 主页
Skip to content

Commit 451747a

Browse files
committed
comments: add Document.comments
Provides access to the comments collection from the document object.
1 parent 5cb32d7 commit 451747a

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
8484
.. |CharacterStyle| replace:: :class:`.CharacterStyle`
8585
86+
.. |Comments| replace:: :class:`.Comments`
87+
8688
.. |Cm| replace:: :class:`.Cm`
8789
8890
.. |ColorFormat| replace:: :class:`.ColorFormat`

src/docx/document.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
if TYPE_CHECKING:
1717
import docx.types as t
18+
from docx.comments import Comments
1819
from docx.oxml.document import CT_Body, CT_Document
1920
from docx.parts.document import DocumentPart
2021
from docx.settings import Settings
@@ -106,6 +107,11 @@ def add_table(self, rows: int, cols: int, style: str | _TableStyle | None = None
106107
table.style = style
107108
return table
108109

110+
@property
111+
def comments(self) -> Comments:
112+
"""A |Comments| object providing access to comments added to the document."""
113+
return self._part.comments
114+
109115
@property
110116
def core_properties(self):
111117
"""A |CoreProperties| object providing Dublin Core properties of document."""

src/docx/parts/document.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from docx.shared import lazyproperty
1616

1717
if TYPE_CHECKING:
18+
from docx.comments import Comments
1819
from docx.enum.style import WD_STYLE_TYPE
1920
from docx.opc.coreprops import CoreProperties
2021
from docx.settings import Settings
@@ -42,6 +43,11 @@ def add_header_part(self):
4243
rId = self.relate_to(header_part, RT.HEADER)
4344
return header_part, rId
4445

46+
@property
47+
def comments(self) -> Comments:
48+
"""|Comments| object providing access to the comments added to this document."""
49+
raise NotImplementedError
50+
4551
@property
4652
def core_properties(self) -> CoreProperties:
4753
"""A |CoreProperties| object providing read/write access to the core properties

tests/test_document.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import pytest
1111

12+
from docx.comments import Comments
1213
from docx.document import Document, _Body
1314
from docx.enum.section import WD_SECTION
1415
from docx.enum.text import WD_BREAK
@@ -164,6 +165,12 @@ def it_can_save_the_document_to_a_file(self, document_part_: Mock):
164165

165166
document_part_.save.assert_called_once_with("foobar.docx")
166167

168+
def it_provides_access_to_the_comments(self, document_part_: Mock, comments_: Mock):
169+
document_part_.comments = comments_
170+
document = Document(cast(CT_Document, element("w:document")), document_part_)
171+
172+
assert document.comments is comments_
173+
167174
def it_provides_access_to_its_core_properties(
168175
self, document_part_: Mock, core_properties_: Mock
169176
):
@@ -281,6 +288,10 @@ def _block_width_prop_(self, request: FixtureRequest):
281288
def body_prop_(self, request: FixtureRequest):
282289
return property_mock(request, Document, "_body")
283290

291+
@pytest.fixture
292+
def comments_(self, request: FixtureRequest):
293+
return instance_mock(request, Comments)
294+
284295
@pytest.fixture
285296
def core_properties_(self, request: FixtureRequest):
286297
return instance_mock(request, CoreProperties)

0 commit comments

Comments
 (0)