Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/docx/oxml/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,21 @@ def tc_at_grid_offset(self, grid_offset: int) -> CT_Tc:
Raises ValueError when this `w:tr` contains no `w:tc` with exact starting `grid_offset`.
"""
# -- account for omitted cells at the start of the row --
remaining_offset = grid_offset - self.grid_before

if grid_offset < self.grid_before:
raise ValueError(f"no `tc` element at grid_offset={grid_offset}")

cell_dict = dict()
cell_index = 0
for tc in self.tc_lst:
# -- We've gone past grid_offset without finding a tc, no sense searching further. --
if remaining_offset < 0:
break
# -- We've arrived at grid_offset, this is the `w:tc` we're looking for. --
if remaining_offset == 0:
return tc
# -- We're not there yet, skip forward the number of layout-grid cells this cell
# -- occupies.
remaining_offset -= tc.grid_span

raise ValueError(f"no `tc` element at grid_offset={grid_offset}")
for _ in range(tc.grid_span):
cell_dict[cell_index] = tc
cell_index += 1

if grid_offset not in cell_dict:
raise ValueError(f"no `tc` element at grid_offset={grid_offset}")

return cell_dict[grid_offset]

@property
def tr_idx(self) -> int:
Expand Down