Skip to content

Commit

Permalink
Fixes #465 - Allow importing 'ragged' .xlsx files (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
claudep authored May 16, 2020
1 parent 16b5565 commit 6d097c0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- Fixed minimal openpyxl dependency version to 2.6.0 (#457).
- Dates from xls files are now read as Python datetime objects (#373).
- Allow import of "ragged" xlsx files (#465).

### Improvements

Expand Down
2 changes: 2 additions & 0 deletions src/tablib/formats/_xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def import_book(cls, dbook, in_stream, headers=True):
if (i == 0) and (headers):
data.headers = row_vals
else:
if i > 0 and len(row_vals) < data.width:
row_vals += [''] * (data.width - len(row_vals))
data.append(row_vals)

dbook.add_sheet(data)
Expand Down
Binary file added tests/files/ragged.xlsx
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,13 @@ def test_xlsx_import_set(self):
self.assertEqual(data.dict[0]['float'], 21.55)
self.assertEqual(data.dict[0]['date/time'], date_time)

def test_xlsx_import_set_ragged(self):
"""Import XLSX file when not all rows have the same length."""
xlsx_source = Path(__file__).parent / 'files' / 'ragged.xlsx'
with open(str(xlsx_source), mode='rb') as fh:
book = tablib.Databook().load(fh, 'xlsx')
self.assertEqual(book.sheets()[0].pop(), (1.0, ''))

def test_xlsx_wrong_char(self):
"""Bad characters are not silently ignored. We let the exception bubble up."""
from openpyxl.utils.exceptions import IllegalCharacterError
Expand Down

0 comments on commit 6d097c0

Please sign in to comment.