8000 rfctr(table): reimplement CT_Tc.tc_at_grid_offset · python-openxml/python-docx@512f269 · GitHub
[go: up one dir, main page]

Skip to content

Commit 512f269

Browse files
committed
rfctr(table): reimplement CT_Tc.tc_at_grid_offset
This method was formerly named `.tc_at_grid_col()`. New implementation takes `CT_Tr.grid_before` into account.
1 parent 7508051 commit 512f269

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

src/docx/oxml/table.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,26 @@ def grid_before(self) -> int:
7676
return 0
7777
return trPr.grid_before
7878

79-
def tc_at_grid_col(self, idx: int) -> CT_Tc:
80-
"""`<w:tc>` element appearing at grid column `idx`.
79+
def tc_at_grid_offset(self, grid_offset: int) -> CT_Tc:
80+
"""The `tc` element in this tr at exact `grid offset`.
8181
82-
Raises |ValueError| if no `w:tc` element begins at that grid column.
82+
Raises ValueError when this `w:tr` contains no `w:tc` with exact starting `grid_offset`.
8383
"""
84-
grid_col = 0
84+
# -- account for omitted cells at the start of the row --
85+
remaining_offset = grid_offset - self.grid_before
86+
8587
for tc in self.tc_lst:
86-
if grid_col == idx:
88+
# -- We've gone past grid_offset without finding a tc, no sense searching further. --
89+
if remaining_offset < 0:
90+
break
91+
# -- We've arrived at grid_offset, this is the `w:tc` we're looking for. --
92+
if remaining_offset == 0:
8793
return tc
88-
grid_col += tc.grid_span
89-
if grid_col > idx:
90-
raise ValueError("no cell on grid column %d" % idx)
91-
raise ValueError("index out of bounds")
94+
# -- We're not there yet, skip forward the number of layout-grid cells this cell
95+
# -- occupies.
96+
remaining_offset -= tc.grid_span
97+
98+
raise ValueError(f"no `tc` element at grid_offset={grid_offset}")
9299

93100
@property
94101
def tr_idx(self) -> int:
@@ -505,7 +512,7 @@ def merge(self, other_tc: CT_Tc) -> CT_Tc:
505512
element and `other_tc` as diagonal corners.
506513
"""
507514
top, left, height, width = self._span_dimensions(other_tc)
508-
top_tc = self._tbl.tr_lst[top].tc_at_grid_col(left)
515+
top_tc = self._tbl.tr_lst[top].tc_at_grid_offset(left)
509516
top_tc._grow_to(width, height)
510517
return top_tc
511518

@@ -731,15 +738,15 @@ def _tbl(self) -> CT_Tbl:
731738
@property
732739
def _tc_above(self) -> CT_Tc:
733740
"""The `w:tc` element immediately above this one in its grid column."""
734-
return self._tr_above.tc_at_grid_col(self.grid_offset)
741+
return self._tr_above.tc_at_grid_offset(self.grid_offset)
735< 10000 /code>742

736743
@property
737744
def _tc_below(self) -> CT_Tc | None:
738745
"""The tc element immediately below this one in its grid column."""
739746
tr_below = self._tr_below
740747
if tr_below is None:
741748
return None
742-
return tr_below.tc_at_grid_col(self.grid_offset)
749+
return tr_below.tc_at_grid_offset(self.grid_offset)
743750

744751
@property
745752
def _tr(self) -> CT_Row:

tests/oxml/test_table.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,11 @@ def it_can_add_a_trPr(self, tr_cxml: str, expected_cxml: str):
3434
tr._add_trPr()
3535
assert tr.xml == xml(expected_cxml)
3636

37-
@pytest.mark.parametrize(
38-
("snippet_idx", "row_idx", "col_idx", "err_msg"),
39-
[
40-
(0, 0, 3, "index out of bounds"),
41-
(1, 0, 1, "no cell on grid column 1"),
42-
],
43-
)
44-
def it_raises_on_tc_at_grid_col(
45-
self, snippet_idx: int, row_idx: int, col_idx: int, err_msg: str
46-
):
37+
@pytest.mark.parametrize(("snippet_idx", "row_idx", "col_idx"), [(0, 0, 3), (1, 0, 1)])
38+
def it_raises_on_tc_at_grid_col(self, snippet_idx: int, row_idx: int, col_idx: int):
4739
tr = cast(CT_Tbl, parse_xml(snippet_seq("tbl-cells")[snippet_idx])).tr_lst[row_idx]
48-
with pytest.raises(ValueError, match=err_msg):
49-
tr.tc_at_grid_col(col_idx)
40+
with pytest.raises(ValueError, match=f"no `tc` element at grid_offset={col_idx}"):
41+
tr. 6D4E tc_at_grid_offset(col_idx)
5042

5143

5244
class DescribeCT_Tc:
@@ -76,12 +68,12 @@ def it_can_merge_to_another_tc(
7668
top, left, height, width = 0, 1, 2, 3
7769
_span_dimensions_.return_value = top, left, height, width
7870
_tbl_.return_value.tr_lst = [tr_]
79-
tr_.tc_at_grid_col.return_value = top_tc_
71+
tr_.tc_at_grid_offset.return_value = top_tc_
8072

8173
merged_tc = tc.merge(other_tc)
8274

8375
_span_dimensions_.assert_called_once_with(tc, other_tc)
84-
top_tr_.tc_at_grid_col.assert_called_once_with(left)
76+
top_tr_.tc_at_grid_offset.assert_called_once_with(left)
8577
top_tc_._grow_to.assert_called_once_with(width, height)
8678
assert merged_tc is top_tc_
8779

0 commit comments

Comments
 (0)
0