Skip to content

Commit a137e01

Browse files
author
wangkebo
committed
fix tc_at_grid_offset lookup bug
1 parent e84ff1b commit a137e01

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/docx/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
if TYPE_CHECKING:
1414
from docx.opc.part import Part
1515

16-
__version__ = "1.1.2"
16+
__version__ = "1.1.2.2"
1717

1818

1919
__all__ = ["Document"]

src/docx/oxml/table.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,36 @@ def tc_at_grid_offset(self, grid_offset: int) -> CT_Tc:
8282
Raises ValueError when this `w:tr` contains no `w:tc` with exact starting `grid_offset`.
8383
"""
8484
# -- account for omitted cells at the start of the row --
85-
remaining_offset = grid_offset - self.grid_before
86-
85+
86+
if grid_offset < self.grid_before:
87+
raise ValueError(f"no `tc` element at grid_offset={grid_offset}")
88+
89+
cell_dict = dict()
90+
cell_index = 0
8791
for tc in self.tc_lst:
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:
93-
return tc
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}")
92+
for _ in range(tc.grid_span):
93+
cell_dict[cell_index] = tc
94+
cell_index += 1
95+
96+
if grid_offset not in cell_dict:
97+
raise ValueError(f"no `tc` element at grid_offset={grid_offset}")
98+
99+
return cell_dict[grid_offset]
100+
101+
# remaining_offset = grid_offset - self.grid_before
102+
103+
# for tc in self.tc_lst:
104+
# # -- We've gone past grid_offset without finding a tc, no sense searching further. --
105+
# if remaining_offset < 0:
106+
# break
107+
# # -- We've arrived at grid_offset, this is the `w:tc` we're looking for. --
108+
# if remaining_offset == 0:
109+
# return tc
110+
# # -- We're not there yet, skip forward the number of layout-grid cells this cell
111+
# # -- occupies.
112+
# remaining_offset -= tc.grid_span
113+
114+
# raise ValueError(f"no `tc` element at grid_offset={grid_offset}")
99115

100116
@property
101117
def tr_idx(self) -> int:

0 commit comments

Comments
 (0)