Skip to content

Commit 954301d

Browse files
author
Steve Canny
committed
tbl: Add CT_Tc._grow_to()
1 parent 1f52c20 commit 954301d

File tree

3 files changed

+133
-9
lines changed

3 files changed

+133
-9
lines changed

docx/oxml/table.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,17 @@ def _grow_to(self, width, height, top_tc=None):
346346
horizontal spans and creating continuation cells to form vertical
347347
spans.
348348
"""
349-
raise NotImplementedError
349+
def vMerge_val(top_tc):
350+
if top_tc is not self:
351+
return ST_Merge.CONTINUE
352+
if height == 1:
353+
return None
354+
return ST_Merge.RESTART
355+
356+
top_tc = self if top_tc is None else top_tc
357+
self._span_to_width(width, top_tc, vMerge_val(top_tc))
358+
if height > 1:
359+
self._tc_below._grow_to(width, height-1, top_tc)
350360

351361
def _insert_tcPr(self, tcPr):
352362
"""
@@ -391,6 +401,19 @@ def raise_on_tee_shaped(a, b):
391401

392402
return top, left, bottom - top, right - left
393403

404+
def _span_to_width(self, grid_width, top_tc, vMerge):
405+
"""
406+
Incorporate and then remove `w:tc` elements to the right of this one
407+
until this cell spans *grid_width*. Raises |ValueError| if
408+
*grid_width* cannot be exactly achieved, such as when a merged cell
409+
would drive the span width greater than *grid_width* or if not enough
410+
grid columns are available to make this cell that wide. All content
411+
from incorporated cells is appended to *top_tc*. The val attribute of
412+
the vMerge element on the single remaining cell is set to *vMerge*.
413+
If *vMerge* is |None|, the vMerge element is removed if present.
414+
"""
415+
raise NotImplementedError
416+
394417
@property
395418
def _tbl(self):
396419
"""

tests/oxml/test_table.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from ..unitutil.cxml import element
1818
from ..unitutil.file import snippet_seq
19-
from ..unitutil.mock import instance_mock, method_mock, property_mock
19+
from ..unitutil.mock import call, instance_mock, method_mock, property_mock
2020

2121

2222
class DescribeCT_Row(object):
@@ -61,6 +61,11 @@ def it_raises_on_invalid_span(self, span_raise_fixture):
6161
with pytest.raises(InvalidSpanError):
6262
tc._span_dimensions(other_tc)
6363

64+
def it_can_grow_itself_to_help_merge(self, grow_to_fixture):
65+
tc, width, height, top_tc, expected_calls = grow_to_fixture
66+
tc._grow_to(width, height, top_tc)
67+
assert tc._span_to_width.call_args_list == expected_calls
68+
6469
def it_raises_on_tr_above(self, tr_above_raise_fixture):
6570
tc = tr_above_raise_fixture
6671
with pytest.raises(ValueError):
@@ -84,6 +89,28 @@ def extents_fixture(self, request):
8489
tc = tbl.tr_lst[row].tc_lst[col]
8590
return tc, attr_name, expected_value
8691

92+
@pytest.fixture(params=[
93+
(0, 0, 0, 2, 1),
94+
(0, 0, 1, 1, 2),
95+
(0, 1, 1, 2, 2),
96+
(1, 0, 0, 2, 2),
97+
(2, 0, 0, 2, 2),
98+
(2, 1, 2, 1, 2),
99+
])
100+
def grow_to_fixture(self, request, _span_to_width_):
101+
snippet_idx, row, col, width, height = request.param
102+
tbl = self._snippet_tbl(snippet_idx)
103+
tc = tbl.tr_lst[row].tc_lst[col]
104+
start = 0 if height == 1 else 1
105+
end = start + height
106+
expected_calls = [
107+
call(width, tc, None),
108+
call(width, tc, 'restart'),
109+
call(width, tc, 'continue'),
110+
call(width, tc, 'continue'),
111+
][start:end]
112+
return tc, width, height, None, expected_calls
113+
87114
@pytest.fixture
88115
def merge_fixture(
89116
self, tr_, _span_dimensions_, _tbl_, _grow_to_, top_tc_):
@@ -147,6 +174,10 @@ def _grow_to_(self, request):
147174
def _span_dimensions_(self, request):
148175
return method_mock(request, CT_Tc, '_span_dimensions')
149176

177+
@pytest.fixture
178+
def _span_to_width_(self, request):
179+
return method_mock(request, CT_Tc, '_span_to_width')
180+
150181
def _snippet_tbl(self, idx):
151182
"""
152183
Return a <w:tbl> element for snippet at *idx* in 'tbl-cells' snippet

tests/test_files/snippets/tbl-cells.txt

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
<!-- 3 x 3 uniform table -->
1+
<!--
2+
idx == 0
3+
3 x 3 uniform table
4+
+~~~+~~~+~~~+
5+
| | | |
6+
+~~~+~~~+~~~+
7+
| | | |
8+
+~~~+~~~+~~~+
9+
| | | |
10+
+~~~+~~~+~~~+
11+
-->
212
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
313
<w:tblGrid>
414
<w:gridCol/>
@@ -22,7 +32,17 @@
2232
</w:tr>
2333
</w:tbl>
2434

25-
<!-- 3 x 3 with horizontal span -->
35+
<!--
36+
idx == 1
37+
3 x 3 with horizontal span
38+
+~~~+~~~+~~~+
39+
| | |
40+
+~~~+~~~+~~~+
41+
| | | |
42+
+~~~+~~~+~~~+
43+
| | | |
44+
+~~~+~~~+~~~+
45+
-->
2646
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
2747
<w:tblGrid>
2848
<w:gridCol/>
@@ -49,7 +69,17 @@
4969
</w:tr>
5070
</w:tbl>
5171

52-
<!-- 3 x 3 with vertical span -->
72+
<!--
73+
idx == 2
74+
3 x 3 with vertical span
75+
+~~~+~~~+~~~+
76+
| | | |
77+
+~~~+ +~~~+
78+
| | | |
79+
+~~~+~~~+~~~+
80+
| | | |
81+
+~~~+~~~+~~~+
82+
-->
5383
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
5484
<w:tblGrid>
5585
<w:gridCol/>
@@ -81,7 +111,17 @@
81111
</w:tr>
82112
</w:tbl>
83113

84-
<!-- 3 x 3 with combo span -->
114+
<!--
115+
idx == 3
116+
3 x 3 with combo span
117+
+~~~+~~~+~~~+
118+
| | |
119+
+ +~~~+
120+
| | |
121+
+~~~+~~~+~~~+
122+
| | | |
123+
+~~~+~~~+~~~+
124+
-->
85125
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
86126
<w:tblGrid>
87127
<w:gridCol/>
@@ -113,7 +153,17 @@
113153
</w:tr>
114154
</w:tbl>
115155

116-
<!-- 3 x 3 with horz, vert, and combo span -->
156+
<!--
157+
idx == 4
158+
3 x 3 with horz, vert, and combo span
159+
+~~~+~~~+~~~+
160+
| | |
161+
+~~~+~~~+~~~+
162+
| | |
163+
+ + +
164+
| | |
165+
+~~~+~~~+~~~+
166+
-->
117167
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
118168
<w:tblGrid>
119169
<w:gridCol/>
@@ -156,7 +206,17 @@
156206
</w:tr>
157207
</w:tbl>
158208

159-
<!-- 3 x 3 with middle row spanned -->
209+
<!--
210+
idx == 5
211+
3 x 3 with middle row spanned
212+
+~~~+~~~+~~~+
213+
| | | |
214+
+~~~+~~~+~~~+
215+
| |
216+
+~~~+~~~+~~~+
217+
| | | |
218+
+~~~+~~~+~~~+
219+
-->
160220
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
161221
<w:tblGrid>
162222
<w:gridCol/>
@@ -182,7 +242,17 @@
182242
</w:tr>
183243
</w:tbl>
184244

185-
<!-- 3 x 3 with middle column spanned -->
245+
<!--
246+
idx == 6
247+
3 x 3 with middle column spanned
248+
+~~~+~~~+~~~+
249+
| | | |
250+
+~~~+ +~~~+
251+
| | | |
252+
+~~~+ +~~~+
253+
| | | |
254+
+~~~+~~~+~~~+
255+
-->
186256
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
187257
<w:tblGrid>
188258
<w:gridCol/>

0 commit comments

Comments
 (0)