Skip to content

Commit 885e332

Browse files
author
Steve Canny
committed
api: add Document.tables
1 parent 03e81d1 commit 885e332

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

docx/api.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def paragraphs(self):
127127
"""
128128
A list of |Paragraph| instances corresponding to the paragraphs in
129129
the document, in document order. Note that paragraphs within revision
130-
marks such as inserted or deleted do not appear in this list.
130+
marks such as ``<w:ins>`` or ``<w:del>`` do not appear in this list.
131131
"""
132132
return self._document_part.paragraphs
133133

@@ -138,6 +138,15 @@ def save(self, path_or_stream):
138138
"""
139139
self._package.save(path_or_stream)
140140

141+
@property
142+
def tables(self):
143+
"""
144+
A list of |Table| instances corresponding to the tables in the
145+
document, in document order. Note that tables within revision marks
146+
such as ``<w:ins>`` or ``<w:del>`` do not appear in this list.
147+
"""
148+
return self._document_part.tables
149+
141150
@staticmethod
142151
def _open(docx):
143152
"""

docx/parts/document.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def add_table(self, rows, cols):
3939
Return a table having *rows* rows and *cols* columns, newly appended
4040
to the main document story.
4141
"""
42-
raise NotImplementedError
42+
return self.body.add_table(rows, cols)
4343

4444
@property
4545
def blob(self):
@@ -110,6 +110,15 @@ def part(self):
110110
"""
111111
return self
112112

113+
@property
114+
def tables(self):
115+
"""
116+
A list of |Table| instances corresponding to the tables in the
117+
document, in document order. Note that tables within revision marks
118+
such as ``<w:ins>`` or ``<w:del>`` do not appear in this list.
119+
"""
120+
raise NotImplementedError
121+
113122

114123
class _Body(object):
115124
"""

tests/parts/test_document.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ def it_can_add_a_paragraph(self, add_paragraph_fixture):
7676
body_.add_paragraph.assert_called_once_with()
7777
assert p is p_
7878

79+
def it_can_add_a_table(self, add_table_fixture):
80+
document_part, rows, cols, body_, table_ = add_table_fixture
81+
table = document_part.add_table(rows, cols)
82+
body_.add_table.assert_called_once_with(rows, cols)
83+
assert table is table_
84+
7985
def it_can_add_an_image_part_to_the_document(
8086
self, get_or_add_image_fixture):
8187
(document, image_descriptor_, image_parts_, relate_to_, image_part_,
@@ -129,14 +135,21 @@ def add_paragraph_fixture(self, document_part_body_, body_, p_):
129135
document_part = DocumentPart(None, None, None, None)
130136
return document_part, body_, p_
131137

138+
@pytest.fixture
139+
def add_table_fixture(self, document_part_body_, body_, table_):
140+
document_part = DocumentPart(None, None, None, None)
141+
rows, cols = 2, 4
142+
return document_part, rows, cols, body_, table_
143+
132144
@pytest.fixture
133145
def _Body_(self, request):
134146
return class_mock(request, 'docx.parts.document._Body')
135147

136148
@pytest.fixture
137-
def body_(self, request, p_):
149+
def body_(self, request, p_, table_):
138150
body_ = instance_mock(request, _Body)
139151
body_.add_paragraph.return_value = p_
152+
body_.add_table.return_value = table_
140153
return body_
141154

142155
@pytest.fixture
@@ -289,6 +302,10 @@ def serialize_part_xml_(self, request):
289302
request, 'docx.parts.document.serialize_part_xml'
290303
)
291304

305+
@pytest.fixture
306+
def table_(self, request):
307+
return instance_mock(request, Table)
308+
292309

293310
class Describe_Body(object):
294311

tests/test_api.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ def it_provides_access_to_the_document_paragraphs(
118118
paragraphs = document.paragraphs
119119
assert paragraphs is paragraphs_
120120

121+
def it_provides_access_to_the_document_tables(self, tables_fixture):
122+
document, tables_ = tables_fixture
123+
tables = document.tables
124+
assert tables is tables_
125+
121126
def it_can_save_the_package(self, save_fixture):
122127
document, package_, file_ = save_fixture
123128
document.save(file_)
@@ -198,13 +203,14 @@ def document(self, open_):
198203
return Document()
199204

200205
@pytest.fixture
201-
def document_part_(self, request, p_, paragraphs_, table_):
206+
def document_part_(self, request, p_, paragraphs_, table_, tables_):
202207
document_part_ = instance_mock(
203208
request, DocumentPart, content_type=CT.WML_DOCUMENT_MAIN
204209
)
205210
document_part_.add_paragraph.return_value = p_
206211
document_part_.add_table.return_value = table_
207212
document_part_.paragraphs = paragraphs_
213+
document_part_.tables = tables_
208214
return document_part_
209215

210216
@pytest.fixture
@@ -269,3 +275,11 @@ def save_fixture(self, request, open_, package_):
269275
@pytest.fixture
270276
def table_(self, request):
271277
return instance_mock(request, Table, style=None)
278+
279+
@pytest.fixture
280+
def tables_(self, request):
281+
return instance_mock(request, list)
282+
283+
@pytest.fixture
284+
def tables_fixture(self, document, tables_):
285+
return document, tables_

0 commit comments

Comments
 (0)