summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2020-05-16 08:07:32 +0200
committerGitHub <noreply@github.com>2020-05-16 09:07:32 +0300
commit6d097c0214f9d581a2c41e4c701cfaca3796f874 (patch)
treed867f400297c0fb797f458529a823747eda56305
parent16b556535445ed2a2347104f0e0604f59f066099 (diff)
downloadtablib-6d097c0214f9d581a2c41e4c701cfaca3796f874.tar.gz
Fixes #465 - Allow importing 'ragged' .xlsx files (#466)
-rw-r--r--HISTORY.md1
-rw-r--r--src/tablib/formats/_xlsx.py2
-rw-r--r--tests/files/ragged.xlsxbin0 -> 4698 bytes
-rwxr-xr-xtests/test_tablib.py7
4 files changed, 10 insertions, 0 deletions
diff --git a/HISTORY.md b/HISTORY.md
index de2ab9d..c7a1336 100644
--- a/HISTORY.md
+++ b/HISTORY.md
@@ -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
diff --git a/src/tablib/formats/_xlsx.py b/src/tablib/formats/_xlsx.py
index dffc192..e740373 100644
--- a/src/tablib/formats/_xlsx.py
+++ b/src/tablib/formats/_xlsx.py
@@ -92,6 +92,8 @@ class XLSXFormat:
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)
diff --git a/tests/files/ragged.xlsx b/tests/files/ragged.xlsx
new file mode 100644
index 0000000..339ec22
--- /dev/null
+++ b/tests/files/ragged.xlsx
Binary files differ
diff --git a/tests/test_tablib.py b/tests/test_tablib.py
index ca57d00..3efc87f 100755
--- a/tests/test_tablib.py
+++ b/tests/test_tablib.py
@@ -1015,6 +1015,13 @@ class XLSXTests(BaseTestCase):
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