Skip to content

Commit 8f184cc

Browse files
committed
comments: add DocumentPart.comments
Provide a way to get a `Comments` object from the `DocumentPart`.
1 parent 451747a commit 8f184cc

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/docx/parts/comments.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""Contains comments added to the document."""
2+
3+
from __future__ import annotations
4+
5+
from docx.comments import Comments
6+
from docx.parts.story import StoryPart
7+
8+
9+
class CommentsPart(StoryPart):
10+
"""Container part for comments added to the document."""
11+
12+
@property
13+
def comments(self) -> Comments:
14+
"""A |Comments| proxy object for the `w:comments` root element of this part."""
15+
raise NotImplementedError

src/docx/parts/document.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from docx.document import Document
88
from docx.opc.constants import RELATIONSHIP_TYPE as RT
9+
from docx.parts.comments import CommentsPart
910
from docx.parts.hdrftr import FooterPart, HeaderPart
1011
from docx.parts.numbering import NumberingPart
1112
from docx.parts.settings import SettingsPart
@@ -46,7 +47,7 @@ def add_header_part(self):
4647
@property
4748
def comments(self) -> Comments:
4849
"""|Comments| object providing access to the comments added to this document."""
49-
raise NotImplementedError
50+
return self._comments_part.comments
5051

5152
@property
5253
def core_properties(self) -> CoreProperties:
@@ -124,6 +125,14 @@ def styles(self):
124125
document."""
125126
return self._styles_part.styles
126127

128+
@property
129+
def _comments_part(self) -> CommentsPart:
130+
"""A |CommentsPart| object providing access to the comments added to this document.
131+
132+
Creates a default comments part if one is not present.
133+
"""
134+
raise NotImplementedError
135+
127136
@property
128137
def _settings_part(self) -> SettingsPart:
129138
"""A |SettingsPart| object providing access to the document-level settings for

tests/parts/test_document.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
import pytest
66

7+
from docx.comments import Comments
78
from docx.enum.style import WD_STYLE_TYPE
89
from docx.opc.constants import CONTENT_TYPE as CT
910
from docx.opc.constants import RELATIONSHIP_TYPE as RT
1011
from docx.opc.coreprops import CoreProperties
1112
from docx.opc.packuri import PackURI
1213
from docx.package import Package
14+
from docx.parts.comments import CommentsPart
1315
from docx.parts.document import DocumentPart
1416
from docx.parts.hdrftr import FooterPart, HeaderPart
1517
from docx.parts.numbering import NumberingPart
@@ -109,6 +111,17 @@ def it_can_save_the_package_to_a_file(self, package_: Mock):
109111

110112
package_.save.assert_called_once_with("foobar.docx")
111113

114+
def it_provides_access_to_the_comments_added_to_the_document(
115+
self, _comments_part_prop_: Mock, comments_part_: Mock, comments_: Mock, package_: Mock
116+
):
117+
comments_part_.comments = comments_
118+
_comments_part_prop_.return_value = comments_part_
119+
document_part = DocumentPart(
120+
PackURI("/word/document.xml"), CT.WML_DOCUMENT, element("w:document"), package_
121+
)
122+
123+
assert document_part.comments is comments_
124+
112125
def it_provides_access_to_the_document_settings(
113126
self, _settings_part_prop_: Mock, settings_part_: Mock, settings_: Mock, package_: Mock
114127
):
@@ -282,6 +295,22 @@ def and_it_creates_a_default_styles_part_if_not_present(
282295

283296
# -- fixtures --------------------------------------------------------------------------------
284297

298+
@pytest.fixture
299+
def comments_(self, request: FixtureRequest) -> Mock:
300+
return instance_mock(request, Comments)
301+
302+
@pytest.fixture
303+
def CommentsPart_(self, request: FixtureRequest) -> Mock:
304+
return class_mock(request, "docx.parts.document.CommentsPart")
305+
306+
@pytest.fixture
307+
def comments_part_(self, request: FixtureRequest) -> Mock:
308+
return instance_mock(request, CommentsPart)
309+
310+
@pytest.fixture
311+
def _comments_part_prop_(self, request: FixtureRequest) -> Mock:
312+
return property_mock(request, DocumentPart, "_comments_part")
313+
285314
@pytest.fixture
286315
def core_properties_(self, request: FixtureRequest):
287316
return instance_mock(request, CoreProperties)

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