Skip to content

Commit 03e81d1

Browse files
author
Steve Canny
committed
api: add Document.add_table()
1 parent 6855f9a commit 03e81d1

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

docx/api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ def add_picture(self, image_path_or_stream, width=None, height=None):
9797

9898
return picture
9999

100+
def add_table(self, rows, cols, style='LightShading-Accent1'):
101+
"""
102+
Add a table having row and column counts of *rows* and *cols*
103+
respectively and table style of *style*. If *style* is |None|, a
104+
table with no style is produced.
105+
"""
106+
table = self._document_part.add_table(rows, cols)
107+
if style:
108+
table.style = style
109+
return table
110+
100111
@property
101112
def body(self):
102113
"""

docx/parts/document.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ def add_paragraph(self):
3434
"""
3535
return self.body.add_paragraph()
3636

37+
def add_table(self, rows, cols):
38+
"""
39+
Return a table having *rows* rows and *cols* columns, newly appended
40+
to the main document story.
41+
"""
42+
raise NotImplementedError
43+
3744
@property
3845
def blob(self):
3946
return serialize_part_xml(self._element)

tests/test_api.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
Test suite for the docx.api module
55
"""
66

7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
711
import pytest
812

913
from docx.api import Document
1014
from docx.enum.text import WD_BREAK
1115
from docx.opc.constants import CONTENT_TYPE as CT
1216
from docx.package import Package
1317
from docx.parts.document import DocumentPart, InlineShapes
18+
from docx.table import Table
1419
from docx.text import Paragraph, Run
1520

1621
from .unitutil import (
@@ -89,6 +94,16 @@ def it_can_add_a_picture(self, add_picture_fixture):
8994
assert picture.height == expected_height
9095
assert picture is picture_
9196

97+
def it_can_add_a_table(self, add_table_fixture):
98+
document, rows, cols, style, document_part_, expected_style, table_ = (
99+
add_table_fixture
100+
)
101+
table = document.add_table(rows, cols, style)
102+
print(table)
103+
document_part_.add_table.assert_called_once_with(rows, cols)
104+
assert table.style == expected_style
105+
assert table == table_
106+
92107
def it_provides_access_to_the_document_body(self, document):
93108
body = document.body
94109
assert body is document._document_part.body
@@ -154,6 +169,15 @@ def add_styled_paragraph_fixture(self, document, p_):
154169
style = 'foobaresque'
155170
return document, style, p_
156171

172+
@pytest.fixture(params=[None, 'LightShading-Accent1', 'foobar'])
173+
def add_table_fixture(self, request, document, document_part_, table_):
174+
rows, cols = 4, 2
175+
style = expected_style = request.param
176+
return (
177+
document, rows, cols, style, document_part_, expected_style,
178+
table_
179+
)
180+
157181
@pytest.fixture
158182
def add_text_paragraph_fixture(self, document, p_, r_):
159183
text = 'foobar\rbarfoo'
@@ -174,11 +198,12 @@ def document(self, open_):
174198
return Document()
175199

176200
@pytest.fixture
177-
def document_part_(self, request, p_, paragraphs_):
201+
def document_part_(self, request, p_, paragraphs_, table_):
178202
document_part_ = instance_mock(
179203
request, DocumentPart, content_type=CT.WML_DOCUMENT_MAIN
180204
)
181205
document_part_.add_paragraph.return_value = p_
206+
document_part_.add_table.return_value = table_
182207
document_part_.paragraphs = paragraphs_
183208
return document_part_
184209

@@ -240,3 +265,7 @@ def save_fixture(self, request, open_, package_):
240265
file_ = instance_mock(request, str)
241266
document = Document()
242267
return document, package_, file_
268+
269+
@pytest.fixture
270+
def table_(self, request):
271+
return instance_mock(request, Table, style=None)

0 commit comments

Comments
 (0)