Skip to content

Commit 60e5b53

Browse files
author
Steve Canny
committed
doc: add Document.save()
1 parent 8e2f61a commit 60e5b53

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

docx/document.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ def part(self):
132132
"""
133133
return self._part
134134

135+
def save(self, path_or_stream):
136+
"""
137+
Save this document to *path_or_stream*, which can be either a path to
138+
a filesystem location (a string) or a file-like object.
139+
"""
140+
self._part.save(path_or_stream)
141+
135142
@property
136143
def _body(self):
137144
"""

docx/parts/document.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ def numbering_part(self):
113113
self.relate_to(numbering_part, RT.NUMBERING)
114114
return numbering_part
115115

116+
def save(self, path_or_stream):
117+
"""
118+
Save this document to *path_or_stream*, which can be either a path to
119+
a filesystem location (a string) or a file-like object.
120+
"""
121+
raise NotImplementedError
122+
116123
@lazyproperty
117124
def sections(self):
118125
"""

tests/test_document.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ def it_can_add_a_table(self, add_table_fixture):
7979
assert table == table_
8080
assert table.style == style
8181

82+
def it_can_save_the_document_to_a_file(self, save_fixture):
83+
document, file_ = save_fixture
84+
document.save(file_)
85+
document._part.save.assert_called_once_with(file_)
86+
8287
def it_provides_access_to_its_core_properties(self, core_props_fixture):
8388
document, core_properties_ = core_props_fixture
8489
core_properties = document.core_properties
@@ -201,6 +206,12 @@ def part_fixture(self, document_part_):
201206
document = Document(None, document_part_)
202207
return document, document_part_
203208

209+
@pytest.fixture
210+
def save_fixture(self, document_part_):
211+
document = Document(None, document_part_)
212+
file_ = 'foobar.docx'
213+
return document, file_
214+
204215
# fixture components ---------------------------------------------
205216

206217
@pytest.fixture

0 commit comments

Comments
 (0)