From bab53a126c19b401cee8fc938bfa2867a3d876c0 Mon Sep 17 00:00:00 2001 From: Rene Kijewski Date: Mon, 11 Jan 2016 14:24:15 +0100 Subject: [PATCH] Accept generator in writerow A generator has not `__len__` attribute, disallowing supplying one in `Sheet.writerow()`. This PR makes the for-loop count the number of cells, so the method does not need to know the number of cells in advance. --- odswriter/__init__.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/odswriter/__init__.py b/odswriter/__init__.py index e06d0dc..11daba6 100644 --- a/odswriter/__init__.py +++ b/odswriter/__init__.py @@ -105,13 +105,7 @@ def __init__(self, dom, name="Sheet 1", cols=None): def writerow(self, cells): row = self.dom.createElement("table:table-row") - content_cells = len(cells) - - if self.cols is not None: - padding_cells = self.cols - content_cells - if content_cells > self.cols: - raise Exception("More cells than cols.") - cells += [None]*padding_cells + content_cells = 0 for cell_data in cells: cell = self.dom.createElement("table:table-cell") @@ -165,6 +159,16 @@ def writerow(self, cells): row.appendChild(cell) + content_cells += 1 + + if self.cols is not None: + if content_cells > self.cols: + raise Exception("More cells than cols.") + + for _ in range(content_cells, self.cols): + cell = self.dom.createElement("table:table-cell") + row.appendChild(cell) + self.table.appendChild(row) def writerows(self, rows):