Skip to content

Commit 382d43e

Browse files
committed
feat(table): add _Cell.grid_span
1 parent 6d49a69 commit 382d43e

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

features/steps/table.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
WD_TABLE_DIRECTION,
1414
)
1515
from docx.shared import Inches
16-
from docx.table import Table, _Column, _Columns, _Row, _Rows
16+
from docx.table import Table, _Cell, _Column, _Columns, _Row, _Rows
1717

1818
from helpers import test_docx
1919

@@ -37,6 +37,13 @@ def given_a_3x3_table_having_span_state(context: Context, span_state: str):
3737
context.table_ = document.tables[table_idx]
3838

3939

40+
@given("a _Cell object spanning {count} layout-grid cells")
41+
def given_a_Cell_object_spanning_count_layout_grid_cells(context: Context, count: str):
42+
document = Document(test_docx("tbl-cell-props"))
43+
table = document.tables[0]
44+
context.cell = _Cell(table._tbl.tr_lst[int(count)].tc_lst[0], table)
45+
46+
4047
@given("a _Cell object with {state} vertical alignment as cell")
4148
def given_a_Cell_object_with_vertical_alignment_as_cell(context: Context, state: str):
4249
table_idx = {
@@ -292,6 +299,13 @@ def when_I_set_the_table_autofit_to_setting(context: Context, setting: str):
292299
# then =====================================================
293300

294301

302+
@then("cell.grid_span is {count}")
303+
def then_cell_grid_span_is_count(context: Context, count: str):
304+
expected = int(count)
305+
actual = context.cell.grid_span
306+
assert actual == expected, f"expected {expected}, got {actual}"
307+
308+
295309
@then("cell.tables[0] is a 2 x 2 table")
296310
def then_cell_tables_0_is_a_2x2_table(context: Context):
297311
cell = context.cell
13.5 KB
Binary file not shown.

features/tbl-cell-props.feature

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ Feature: Get and set table cell properties
44
I need a way to get and set the properties of a table cell
55

66

7+
Scenario Outline: Get _Cell.grid_span
8+
Given a _Cell object spanning <count> layout-grid cells
9+
Then cell.grid_span is <count>
10+
11+
Examples: Cell.grid_span value cases
12+
| count |
13+
| 1 |
14+
| 2 |
15+
| 4 |
16+
17+
718
Scenario Outline: Get _Cell.vertical_alignment
819
Given a _Cell object with <state> vertical alignment as cell
920
Then cell.vertical_alignment is <value>

src/docx/table.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ def add_table( # pyright: ignore[reportIncompatibleMethodOverride]
222222
self.add_paragraph()
223223
return table
224224

225+
@property
226+
def grid_span(self) -> int:
227+
"""Number of layout-grid cells this cell spans horizontally.
228+
229+
A "normal" cell has a grid-span of 1. A horizontally merged cell has a grid-span of 2 or
230+
more.
231+
"""
232+
return self._tc.grid_span
233+
225234
def merge(self, other_cell: _Cell):
226235
"""Return a merged cell created by spanning the rectangular region having this
227236
cell and `other_cell` as diagonal corners.

tests/test_table.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,19 @@ def table(self, document_: Mock):
326326
class Describe_Cell:
327327
"""Unit-test suite for `docx.table._Cell` objects."""
328328

329+
@pytest.mark.parametrize(
330+
("tc_cxml", "expected_value"),
331+
[
332+
("w:tc", 1),
333+
("w:tc/w:tcPr", 1),
334+
("w:tc/w:tcPr/w:gridSpan{w:val=1}", 1),
335+
("w:tc/w:tcPr/w:gridSpan{w:val=4}", 4),
336+
],
337+
)
338+
def it_knows_its_grid_span(self, tc_cxml: str, expected_value: int, parent_: Mock):
339+
cell = _Cell(cast(CT_Tc, element(tc_cxml)), parent_)
340+
assert cell.grid_span == expected_value
341+
329342
@pytest.mark.parametrize(
330343
("tc_cxml", "expected_text"),
331344
[

0 commit comments

Comments
 (0)