Skip to content

Commit fe4b7b8

Browse files
author
Steve Canny
committed
doc: add DocumentPart.tables
1 parent 885e332 commit fe4b7b8

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

docx/parts/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def tables(self):
117117
document, in document order. Note that tables within revision marks
118118
such as ``<w:ins>`` or ``<w:del>`` do not appear in this list.
119119
"""
120-
raise NotImplementedError
120+
return self.body.tables
121121

122122

123123
class _Body(object):

features/api-add-table.feature

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ Feature: Add a table
33
As a programmer using the basic python-docx API
44
I need a method that adds a table at the end of the document
55

6-
@wip
76
Scenario: Add a table specifying only row and column count
87
Given a document
98
When I add a 2 x 2 table specifying only row and column count
109
Then the document contains a 2 x 2 table
1110
And the table style is 'LightShading-Accent1'
1211

13-
@wip
1412
Scenario: Add a table specifying style
1513
Given a document
1614
When I add a 2 x 2 table specifying style 'foobar'

features/steps/api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def then_document_contains_2x2_table(context):
105105
table = document.tables[-1]
106106
assert isinstance(table, Table)
107107
assert len(table.rows) == 2
108-
assert len(table.cols) == 2
109-
context.table = table
108+
assert len(table.columns) == 2
109+
context.table_ = table
110110

111111

112112
@then('the last paragraph contains only a page break')
@@ -189,5 +189,5 @@ def then_style_of_last_paragraph_is_style(context, style):
189189

190190
@then('the table style is \'{style}\'')
191191
def then_table_style_is_style(context, style):
192-
table = context.table
192+
table = context.table_
193193
assert table.style == style

tests/parts/test_document.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ def it_provides_access_to_the_document_paragraphs(
114114
paragraphs = document_part.paragraphs
115115
assert paragraphs is paragraphs_
116116

117+
def it_provides_access_to_the_document_tables(self, tables_fixture):
118+
document_part, tables_ = tables_fixture
119+
tables = document_part.tables
120+
assert tables is tables_
121+
117122
def it_provides_access_to_the_inline_shapes_in_the_document(
118123
self, inline_shapes_fixture):
119124
document, InlineShapes_, body_elm = inline_shapes_fixture
@@ -306,6 +311,16 @@ def serialize_part_xml_(self, request):
306311
def table_(self, request):
307312
return instance_mock(request, Table)
308313

314+
@pytest.fixture
315+
def tables_(self, request):
316+
return instance_mock(request, list)
317+
318+
@pytest.fixture
319+
def tables_fixture(self, document_part_body_, body_, tables_):
320+
document_part = DocumentPart(None, None, None, None)
321+
body_.tables = tables_
322+
return document_part, tables_
323+
309324

310325
class Describe_Body(object):
311326

0 commit comments

Comments
 (0)