Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ValueError when date type cell is empty #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ def test_cell_type_date(self):
ws = wb["test"]
self.assertEquals(ws["A1"].value.date(), date(1981, 1, 24))

def test_empty_cell_type_date(self):
template = """
<sheet title="test">
<row><cell type="date" date-fmt="%d.%m.%Y"></cell></row>
</sheet>
"""
sheet = io.BytesIO(xml2xlsx(template))
wb = load_workbook(sheet)
ws = wb['test']
self.assertEquals(ws['A1'].value, None)

def test_cell_number_format(self):
template = """
<sheet title="test">
Expand Down
8 changes: 6 additions & 2 deletions xml2xlsx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,12 @@ def end(self, tag):
except InvalidOperation:
pass
elif self._cell_type == 'date':
self._cell.value = datetime.strptime(
self._cell.value, self._cell_date_format).date()
if self._cell.value:
try:
self._cell.value = datetime.strptime(
self._cell.value, self._cell_date_format).date()
except TypeError:
pass
self._row_buf.append(self._cell)
self._cell = None
self._col += 1
Expand Down