Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
32 changes: 14 additions & 18 deletions docx/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,14 @@ def cell(self, row_idx, col_idx):
Return |_Cell| instance correponding to table cell at *row_idx*,
*col_idx* intersection, where (0, 0) is the top, left-most cell.
"""
cell_idx = col_idx + (row_idx * self._column_count)
return self._cells[cell_idx]
return self._cells[row_idx][col_idx]

def column_cells(self, column_idx):
"""
Sequence of cells in the column at *column_idx* in this table.
"""
cells = self._cells
idxs = range(column_idx, len(cells), self._column_count)
return [cells[idx] for idx in idxs]
return [cells[row_idx][column_idx] for row_idx in range(0, len(cells))]

@lazyproperty
def columns(self):
Expand All @@ -100,10 +98,7 @@ def row_cells(self, row_idx):
"""
Sequence of cells in the row at *row_idx* in this table.
"""
column_count = self._column_count
start = row_idx * column_count
end = start + column_count
return self._cells[start:end]
return self._cells[row_idx]

@lazyproperty
def rows(self):
Expand Down Expand Up @@ -161,20 +156,21 @@ def table_direction(self, value):
@property
def _cells(self):
"""
A sequence of |_Cell| objects, one for each cell of the layout grid.
A matrix of |_Cell| objects, one for each cell of the layout grid.
If the table contains a span, one or more |_Cell| object references
are repeated.
"""
col_count = self._column_count
cells = []
for tc in self._tbl.iter_tcs():
for grid_span_idx in range(tc.grid_span):
if tc.vMerge == ST_Merge.CONTINUE:
cells.append(cells[-col_count])
elif grid_span_idx > 0:
cells.append(cells[-1])
else:
cells.append(_Cell(tc, self))
for row_idx, tr in enumerate(self._tbl.tr_lst):
cells.append([])
for col_idx, tc in enumerate(tr.tc_lst):
for grid_span_idx in range(tc.grid_span):
if tc.vMerge == ST_Merge.CONTINUE:
cells[row_idx].append(cells[row_idx - 1][col_idx])
elif grid_span_idx > 0:
cells[row_idx].append(cells[row_idx][col_idx])
else:
cells[row_idx].append(_Cell(tc, self))
return cells

@property
Expand Down