Skip to content

Commit b00a46b

Browse files
author
Steve Canny
committed
doc: add Document.add_table()
1 parent e130662 commit b00a46b

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

docx/document.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ def add_section(self, start_type=WD_SECTION.NEW_PAGE):
8888
new_sectPr.start_type = start_type
8989
return Section(new_sectPr)
9090

91+
def add_table(self, rows, cols, style='Light Shading Accent 1'):
92+
"""
93+
Add a table having row and column counts of *rows* and *cols*
94+
respectively and table style of *style*. If *style* is |None|, a
95+
table with no style is produced.
96+
"""
97+
table = self._body.add_table(rows, cols)
98+
table.style = style
99+
return table
100+
91101
@property
92102
def part(self):
93103
"""

tests/test_document.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from docx.enum.text import WD_BREAK
1616
from docx.parts.document import DocumentPart
1717
from docx.shape import InlineShape
18+
from docx.table import Table
1819
from docx.text.paragraph import Paragraph
1920
from docx.text.run import Run
2021

@@ -70,6 +71,13 @@ def it_can_add_a_section(self, add_section_fixture):
7071
Section_.assert_called_once_with(sectPr)
7172
assert section is section_
7273

74+
def it_can_add_a_table(self, add_table_fixture):
75+
document, rows, cols, style, table_ = add_table_fixture
76+
table = document.add_table(rows, cols, style)
77+
document._body.add_table.assert_called_once_with(rows, cols)
78+
assert table == table_
79+
assert table.style == style
80+
7381
def it_provides_access_to_the_document_part(self, part_fixture):
7482
document, part_ = part_fixture
7583
assert document.part is part_
@@ -140,6 +148,13 @@ def add_section_fixture(self, request, Section_):
140148
section_ = Section_.return_value
141149
return document, start_type, Section_, section_, expected_xml
142150

151+
@pytest.fixture
152+
def add_table_fixture(self, body_prop_, table_):
153+
document = Document(None, None)
154+
rows, cols, style = 4, 2, 'Light Shading Accent 1'
155+
body_prop_.return_value.add_table.return_value = table_
156+
return document, rows, cols, style, table_
157+
143158
@pytest.fixture
144159
def body_fixture(self, _Body_, body_):
145160
document_elm = element('w:document/w:body')
@@ -167,8 +182,8 @@ def body_(self, request):
167182
return instance_mock(request, _Body)
168183

169184
@pytest.fixture
170-
def body_prop_(self, request):
171-
return property_mock(request, Document, '_body')
185+
def body_prop_(self, request, body_):
186+
return property_mock(request, Document, '_body', return_value=body_)
172187

173188
@pytest.fixture
174189
def document_part_(self, request):
@@ -189,3 +204,7 @@ def run_(self, request):
189204
@pytest.fixture
190205
def Section_(self, request):
191206
return class_mock(request, 'docx.document.Section')
207+
208+
@pytest.fixture
209+
def table_(self, request):
210+
return instance_mock(request, Table, style='UNASSIGNED')

0 commit comments

Comments
 (0)