Skip to content

Commit 0c293d5

Browse files
author
Steve Canny
committed
blkct: add BlockItemContainer.add_table()
* remove _Body.add_table(), inheriting that method from BlockItemContainer * refactor Describe_Body.it_can_add_a_table() to use cxml instead of XML builders * retain test for _Body.add_table() to test integration with required oxml elements.
1 parent e2d6311 commit 0c293d5

File tree

4 files changed

+55
-73
lines changed

4 files changed

+55
-73
lines changed

docx/blkcntnr.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,17 @@ def add_paragraph(self, text='', style=None):
3737
if style is not None:
3838
paragraph.style = style
3939
return paragraph
40+
41+
def add_table(self, rows, cols):
42+
"""
43+
Return a newly added table having *rows* rows and *cols* cols,
44+
appended to the content in this container.
45+
"""
46+
from .table import Table
47+
tbl = self._element.add_tbl()
48+
table = Table(tbl, self)
49+
for i in range(cols):
50+
table.add_column()
51+
for i in range(rows):
52+
table.add_row()
53+
return table

docx/parts/document.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,6 @@ def __init__(self, body_elm, parent):
123123
super(_Body, self).__init__(body_elm, parent)
124124
self._body = body_elm
125125

126-
def add_table(self, rows, cols):
127-
"""
128-
Return a table having *rows* rows and *cols* cols, newly appended to
129-
the main document story.
130-
"""
131-
tbl = self._body.add_tbl()
132-
table = Table(tbl, self)
133-
for i in range(cols):
134-
table.add_column()
135-
for i in range(rows):
136-
table.add_row()
137-
return table
138-
139126
def clear_content(self):
140127
"""
141128
Return this |_Body| instance after clearing it of all content.

tests/parts/test_document.py

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
from docx.text import Paragraph, Run
2222

2323
from ..oxml.parts.unitdata.document import a_body, a_document
24-
from ..oxml.unitdata.table import (
25-
a_gridCol, a_tbl, a_tblGrid, a_tblPr, a_tblW, a_tc, a_tr
26-
)
27-
from ..oxml.unitdata.text import a_p, a_sectPr
24+
from ..oxml.unitdata.text import a_p
2825
from ..unitutil.cxml import element, xml
2926
from ..unitutil.mock import (
3027
instance_mock, class_mock, loose_mock, method_mock, property_mock
@@ -295,9 +292,9 @@ def it_can_add_a_paragraph(self, add_paragraph_fixture):
295292
assert isinstance(p, Paragraph)
296293

297294
def it_can_add_a_table(self, add_table_fixture):
298-
body, expected_xml = add_table_fixture
299-
table = body.add_table(rows=1, cols=1)
300-
assert body._body.xml == expected_xml
295+
body, rows, cols, expected_xml = add_table_fixture
296+
table = body.add_table(rows, cols)
297+
assert body._element.xml == expected_xml
301298
assert isinstance(table, Table)
302299

303300
def it_can_clear_itself_of_all_content_it_holds(self, clear_fixture):
@@ -335,19 +332,21 @@ def add_paragraph_fixture(self, request):
335332
expected_xml = xml(after_cxml)
336333
return body, expected_xml
337334

338-
@pytest.fixture(params=[(0, False), (0, True), (1, False), (1, True)])
335+
@pytest.fixture(params=[
336+
('w:body', 0, 0, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
337+
'tblGrid)'),
338+
('w:body', 1, 0, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
339+
'tblGrid,w:tr)'),
340+
('w:body', 0, 1, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
341+
'tblGrid/w:gridCol)'),
342+
('w:body', 1, 1, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
343+
'tblGrid/w:gridCol,w:tr/w:tc/w:p)'),
344+
])
339345
def add_table_fixture(self, request):
340-
p_count, has_sectPr = request.param
341-
body_bldr = self._body_bldr(p_count=p_count, sectPr=has_sectPr)
342-
body = _Body(body_bldr.element, None)
343-
344-
tbl_bldr = self._tbl_bldr()
345-
body_bldr = self._body_bldr(
346-
p_count=p_count, tbl_bldr=tbl_bldr, sectPr=has_sectPr
347-
)
348-
expected_xml = body_bldr.xml()
349-
350-
return body, expected_xml
346+
body_cxml, rows, cols, after_cxml = request.param
347+
body = _Body(element(body_cxml), None)
348+
expected_xml = xml(after_cxml)
349+
return body, rows, cols, expected_xml
351350

352351
@pytest.fixture(params=[
353352
('w:body', 'w:body'),
@@ -369,47 +368,6 @@ def paragraphs_fixture(self):
369368
def tables_fixture(self):
370369
return _Body(element('w:body/(w:tbl, w:tbl)'), None)
371370

372-
# fixture components ---------------------------------------------
373-
374-
def _body_bldr(self, p_count=0, tbl_bldr=None, sectPr=False):
375-
body_bldr = a_body().with_nsdecls()
376-
for i in range(p_count):
377-
body_bldr.with_child(a_p())
378-
if tbl_bldr is not None:
379-
body_bldr.with_child(tbl_bldr)
380-
if sectPr:
381-
body_bldr.with_child(a_sectPr())
382-
return body_bldr
383-
384-
def _tbl_bldr(self, rows=1, cols=1):
385-
tblPr_bldr = (
386-
a_tblPr().with_child(
387-
a_tblW().with_type("auto").with_w(0))
388-
)
389-
390-
tblGrid_bldr = a_tblGrid()
391-
for i in range(cols):
392-
tblGrid_bldr.with_child(a_gridCol())
393-
394-
tbl_bldr = a_tbl()
395-
tbl_bldr.with_child(tblPr_bldr)
396-
tbl_bldr.with_child(tblGrid_bldr)
397-
for i in range(rows):
398-
tr_bldr = self._tr_bldr(cols)
399-
tbl_bldr.with_child(tr_bldr)
400-
401-
return tbl_bldr
402-
403-
def _tc_bldr(self):
404-
return a_tc().with_child(a_p())
405-
406-
def _tr_bldr(self, cols):
407-
tr_bldr = a_tr()
408-
for i in range(cols):
409-
tc_bldr = self._tc_bldr()
410-
tr_bldr.with_child(tc_bldr)
411-
return tr_bldr
412-
413371

414372
class DescribeInlineShapes(object):
415373

tests/test_blkcntnr.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pytest
1010

1111
from docx.blkcntnr import BlockItemContainer
12+
from docx.table import Table
1213
from docx.text import Paragraph
1314

1415
from .unitutil.cxml import element, xml
@@ -22,6 +23,12 @@ def it_can_add_a_paragraph(self, add_paragraph_fixture):
2223
assert blkcntnr._element.xml == expected_xml
2324
assert isinstance(paragraph, Paragraph)
2425

26+
def it_can_add_a_table(self, add_table_fixture):
27+
blkcntnr, rows, cols, expected_xml = add_table_fixture
28+
table = blkcntnr.add_table(rows, cols)
29+
assert blkcntnr._element.xml == expected_xml
30+
assert isinstance(table, Table)
31+
2532
# fixtures -------------------------------------------------------
2633

2734
@pytest.fixture(params=[
@@ -39,3 +46,19 @@ def add_paragraph_fixture(self, request):
3946
blkcntnr = BlockItemContainer(element(blkcntnr_cxml), None)
4047
expected_xml = xml(after_cxml)
4148
return blkcntnr, text, style, expected_xml
49+
50+
@pytest.fixture(params=[
51+
('w:body', 0, 0, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
52+
'tblGrid)'),
53+
('w:body', 1, 0, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
54+
'tblGrid,w:tr)'),
55+
('w:body', 0, 1, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
56+
'tblGrid/w:gridCol)'),
57+
('w:body', 1, 1, 'w:body/w:tbl/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:'
58+
'tblGrid/w:gridCol,w:tr/w:tc/w:p)'),
59+
])
60+
def add_table_fixture(self, request):
61+
blkcntnr_cxml, rows, cols, after_cxml = request.param
62+
blkcntnr = BlockItemContainer(element(blkcntnr_cxml), None)
63+
expected_xml = xml(after_cxml)
64+
return blkcntnr, rows, cols, expected_xml

0 commit comments

Comments
 (0)