Skip to content

Commit 558a124

Browse files
author
Steve Canny
committed
cell: rewrite _Cell.add_table()
1 parent ebd5e85 commit 558a124

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

docx/table.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .blkcntnr import BlockItemContainer
1010
from .enum.style import WD_STYLE_TYPE
1111
from .oxml.simpletypes import ST_Merge
12-
from .shared import lazyproperty, Parented
12+
from .shared import Inches, lazyproperty, Parented
1313

1414

1515
class Table(Parented):
@@ -201,9 +201,10 @@ def add_table(self, rows, cols):
201201
added after the table because Word requires a paragraph element as
202202
the last element in every cell.
203203
"""
204-
new_table = super(_Cell, self).add_table(rows, cols, 914400)
204+
width = self.width if self.width is not None else Inches(1)
205+
table = super(_Cell, self).add_table(rows, cols, width)
205206
self.add_paragraph()
206-
return new_table
207+
return table
207208

208209
def merge(self, other_cell):
209210
"""

features/cel-add-table.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Add a table into a table cell
44
I need a way to add a table to a table cell
55

66

7-
@wip
87
Scenario: Add a table into a table cell
98
Given a table cell
109
When I add a 2 x 2 table into the first cell

tests/test_files/snippets/new-tbl.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,46 @@
3636
</w:tc>
3737
</w:tr>
3838
</w:tbl>
39+
40+
<w:tc xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
41+
<w:p/>
42+
<w:tbl>
43+
<w:tblPr>
44+
<w:tblW w:type="auto" w:w="0"/>
45+
<w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0" w:lastRow="0" w:noHBand="0" w:noVBand="1" w:val="04A0"/>
46+
</w:tblPr>
47+
<w:tblGrid>
48+
<w:gridCol w:w="720"/>
49+
<w:gridCol w:w="720"/>
50+
</w:tblGrid>
51+
<w:tr>
52+
<w:tc>
53+
<w:tcPr>
54+
<w:tcW w:type="dxa" w:w="720"/>
55+
</w:tcPr>
56+
<w:p/>
57+
</w:tc>
58+
<w:tc>
59+
<w:tcPr>
60+
<w:tcW w:type="dxa" w:w="720"/>
61+
</w:tcPr>
62+
<w:p/>
63+
</w:tc>
64+
</w:tr>
65+
<w:tr>
66+
<w:tc>
67+
<w:tcPr>
68+
<w:tcW w:type="dxa" w:w="720"/>
69+
</w:tcPr>
70+
<w:p/>
71+
</w:tc>
72+
<w:tc>
73+
<w:tcPr>
74+
<w:tcW w:type="dxa" w:w="720"/>
75+
</w:tcPr>
76+
<w:p/>
77+
</w:tc>
78+
</w:tr>
79+
</w:tbl>
80+
<w:p/>
81+
</w:tc>

tests/test_table.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,8 @@ def it_can_add_a_paragraph(self, add_paragraph_fixture):
338338

339339
def it_can_add_a_table(self, add_table_fixture):
340340
cell, expected_xml = add_table_fixture
341-
table = cell.add_table(rows=0, cols=1)
342-
assert cell._tc.xml == expected_xml
341+
table = cell.add_table(rows=2, cols=2)
342+
assert cell._element.xml == expected_xml
343343
assert isinstance(table, Table)
344344

345345
def it_can_merge_itself_with_other_cells(self, merge_fixture):
@@ -363,21 +363,10 @@ def add_paragraph_fixture(self, request):
363363
expected_xml = xml(after_tc_cxml)
364364
return cell, expected_xml
365365

366-
@pytest.fixture(params=[
367-
('w:tc', 'w:tc/(w:tbl'),
368-
('w:tc/w:p', 'w:tc/(w:p, w:tbl'),
369-
])
366+
@pytest.fixture
370367
def add_table_fixture(self, request):
371-
tc_cxml, after_tc_cxml = request.param
372-
# the table has some overhead elements, also a blank para after since
373-
# it's in a cell.
374-
after_tc_cxml += (
375-
'/(w:tblPr/(w:tblW{w:type=auto,w:w=0},w:tblLook{w:firstColumn=1,'
376-
'w:firstRow=1,w:lastColumn=0,w:lastRow=0,w:noHBand=0,w:noVBand=1'
377-
',w:val=04A0}),w:tblGrid/w:gridCol{w:w=1440}),w:p)'
378-
)
379-
cell = _Cell(element(tc_cxml), None)
380-
expected_xml = xml(after_tc_cxml)
368+
cell = _Cell(element('w:tc/w:p'), None)
369+
expected_xml = snippet_seq('new-tbl')[1]
381370
return cell, expected_xml
382371

383372
@pytest.fixture

0 commit comments

Comments
 (0)