Skip to content

Commit

Permalink
fix: skip last empty rows of sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
cupen committed Jan 11, 2024
1 parent fd08ec7 commit 53b66d3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
24 changes: 22 additions & 2 deletions excel2xx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from excel2xx import utils, fields, fieldmeta
import excel2xx.exporter as exporter

VERSION = "0.11.0"
VERSION = "0.11.1"
__author__ = "cupen"
__email__ = "xcupen@gmail.com"

Expand Down Expand Up @@ -101,15 +101,36 @@ def fields(self):

def rows(self):
skipRows = self.__excel.fieldMeta.dataRowIdx
emptyRows = []
for row in self.__sheet.get_rows():
if skipRows > 0:
skipRows -= 1
# print(row)
continue

if len(row) <= 0:
emptyRows+=1
continue

if self._isEmptyRow(row):
emptyRows.append(row)
continue
elif len(emptyRows) > 0:
raise self.throwException(f"Invalid row with empty'{emptyRows[0]}'")
yield row
pass

def _isEmptyRow(self, row):
_fields = self.fields()
len(_fields)
if len(row) > len(_fields):
row = row[:len(_fields)]
for cell in row:
if cell.ctype != xlrd.XL_CELL_EMPTY:
return False
pass
return True

def toList(self):
return list(iter(self))

Expand Down Expand Up @@ -159,7 +180,6 @@ def toKV(self):

def toDataFrame(self):
import pandas

return pandas.DataFrame(self, columns=self.fields().keys())

@property
Expand Down
4 changes: 2 additions & 2 deletions excel2xx/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
NOTE = "// DONT EDIT: generated by excel2xx/codegen.\n"


def generate(src: list, context={}, note=NOTE):
def generate(src: list, context={}, note=NOTE, encoding="utf-8", newline="\n"):
"""
generate source code from mako template files
"""
Expand Down Expand Up @@ -42,7 +42,7 @@ def generate(src: list, context={}, note=NOTE):
fpath_new = fpath.replace(".mako", "")
try:
text = tmpl.render(**ctx)
with open(fpath_new, "w", encoding="utf-8") as fp:
with open(fpath_new, "w", encoding=encoding, newline=newline) as fp:
if note:
fp.write(note)
fp.write(text)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="excel2xx",
version="0.11.0",
version="0.11.1",
packages=find_packages(),
url="https://github.com/cupen/excel2xx",
license="WTFPL",
Expand Down

0 comments on commit 53b66d3

Please sign in to comment.