Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2214533

Browse files
author
Steve Canny
committed
tbl: add _Cell.add_table()
1 parent 56a5af9 commit 2214533

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

docx/oxml/table.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class CT_Tc(BaseOxmlElement):
165165
"""
166166
tcPr = ZeroOrOne('w:tcPr') # bunches of successors, overriding insert
167167
p = OneOrMore('w:p')
168+
tbl = OneOrMore('w:tbl')
168169

169170
def _insert_tcPr(self, tcPr):
170171
"""
@@ -175,13 +176,16 @@ def _insert_tcPr(self, tcPr):
175176
self.insert(0, tcPr)
176177
return tcPr
177178

179+
def _new_tbl(self):
180+
return CT_Tbl.new()
181+
178182
def clear_content(self):
179183
"""
180184
Remove all content child elements, preserving the ``<w:tcPr>``
181185
element if present. Note that this leaves the ``<w:tc>`` element in
182186
an invalid state because it doesn't contain at least one block-level
183-
element. It's up to the caller to add a ``<w:p>`` or ``<w:tbl>``
184-
child element.
187+
element. It's up to the caller to add a ``<w:p>``child element as the
188+
last content element.
185189
"""
186190
new_children = []
187191
tcPr = self.tcPr

docx/table.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ def add_paragraph(self, text='', style=None):
114114
"""
115115
return super(_Cell, self).add_paragraph(text, style)
116116

117+
def add_table(self, rows, cols):
118+
"""
119+
Return a table newly added to this cell after any existing cell
120+
content, having *rows* rows and *cols* columns. An empty paragraph is
121+
added after the table because Word requires a paragraph element as
122+
the last element in every cell.
123+
"""
124+
new_table = super(_Cell, self).add_table(rows, cols)
125+
self.add_paragraph()
126+
return new_table
127+
117128
@property
118129
def paragraphs(self):
119130
"""

tests/test_table.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ def it_can_add_a_paragraph(self, add_paragraph_fixture):
157157
assert cell._tc.xml == expected_xml
158158
assert isinstance(p, Paragraph)
159159

160+
def it_can_add_a_table(self, add_table_fixture):
161+
cell, expected_xml = add_table_fixture
162+
table = cell.add_table(rows=0, cols=0)
163+
assert cell._tc.xml == expected_xml
164+
assert isinstance(table, Table)
165+
160166
def it_provides_access_to_the_paragraphs_it_contains(
161167
self, paragraphs_fixture):
162168
cell = paragraphs_fixture
@@ -198,6 +204,21 @@ def add_paragraph_fixture(self, request):
198204
expected_xml = xml(after_tc_cxml)
199205
return cell, expected_xml
200206

207+
@pytest.fixture(params=[
208+
('w:tc', 'w:tc/(w:tbl'),
209+
('w:tc/w:p', 'w:tc/(w:p, w:tbl'),
210+
])
211+
def add_table_fixture(self, request):
212+
tc_cxml, after_tc_cxml = request.param
213+
# the table has some overhead elements, also a blank para after since
214+
# it's in a cell.
215+
after_tc_cxml += (
216+
'/(w:tblPr/w:tblW{w:type=auto,w:w=0},w:tblGrid),w:p)'
217+
)
218+
cell = _Cell(element(tc_cxml), None)
219+
expected_xml = xml(after_tc_cxml)
220+
return cell, expected_xml
221+
201222
@pytest.fixture
202223
def paragraphs_fixture(self):
203224
return _Cell(element('w:tc/(w:p, w:p)'), None)

0 commit comments

Comments
 (0)