diff options
author | Claude Paroz <claude@2xlibre.net> | 2019-10-04 20:32:01 +0200 |
---|---|---|
committer | Claude Paroz <claude@2xlibre.net> | 2019-10-04 20:46:31 +0200 |
commit | 91062672b5406972da75bdcf02ad8373fff96377 (patch) | |
tree | 2ced8643acabcb9238d9c47d45f4ceea86439d2d | |
parent | 34334e72a12c28425aff9162da13c3300bb80e18 (diff) | |
download | tablib-91062672b5406972da75bdcf02ad8373fff96377.tar.gz |
Fixed #373 - Properly detect xlsx format
-rw-r--r-- | HISTORY.md | 1 | ||||
-rw-r--r-- | tablib/formats/__init__.py | 3 | ||||
-rw-r--r-- | tablib/formats/_json.py | 2 | ||||
-rw-r--r-- | tablib/formats/_xlsx.py | 5 | ||||
-rwxr-xr-x | test_tablib.py | 12 |
5 files changed, 19 insertions, 4 deletions
@@ -6,6 +6,7 @@ - Fixed `DataBook().load` parameter ordering (first stream, then format). - Fixed a regression for xlsx exports where non-string values were forced to strings (#314). +- Fixed xlsx format detection (which was often detected as `xls` format). - Added search to all documentation pages - Open xlsx workbooks in read-only mode (#316) - Unpin requirements diff --git a/tablib/formats/__init__.py b/tablib/formats/__init__.py index 418e607..52cf472 100644 --- a/tablib/formats/__init__.py +++ b/tablib/formats/__init__.py @@ -17,4 +17,5 @@ from . import _df as df from . import _rst as rst from . import _jira as jira -available = (json, xls, yaml, csv, dbf, tsv, html, jira, latex, xlsx, ods, df, rst) +# xlsx before as xls (xlrd) can also read xlsx +available = (json, xlsx, xls, yaml, csv, dbf, tsv, html, jira, latex, ods, df, rst) diff --git a/tablib/formats/_json.py b/tablib/formats/_json.py index a794a3d..009fb3c 100644 --- a/tablib/formats/_json.py +++ b/tablib/formats/_json.py @@ -55,5 +55,5 @@ def detect(stream): try: json.loads(stream) return True - except ValueError: + except (TypeError, ValueError): return False diff --git a/tablib/formats/_xlsx.py b/tablib/formats/_xlsx.py index 6662a2f..516191c 100644 --- a/tablib/formats/_xlsx.py +++ b/tablib/formats/_xlsx.py @@ -22,8 +22,11 @@ extensions = ('xlsx',) def detect(stream): """Returns True if given stream is a readable excel file.""" + if isinstance(stream, bytes): + # load_workbook expects a file-like object. + stream = BytesIO(stream) try: - openpyxl.reader.excel.load_workbook(stream) + openpyxl.reader.excel.load_workbook(stream, read_only=True) return True except openpyxl.shared.exc.InvalidFileException: pass diff --git a/test_tablib.py b/test_tablib.py index e4f1543..012f89d 100755 --- a/test_tablib.py +++ b/test_tablib.py @@ -13,7 +13,7 @@ from uuid import uuid4 from MarkupPy import markup import tablib from tablib.compat import unicode, is_py3 -from tablib.core import Row +from tablib.core import Row, detect_format from tablib.formats import _csv as csv_module @@ -783,6 +783,16 @@ class TSVTests(BaseTestCase): class XLSXTests(BaseTestCase): + def test_xlsx_format_detect(self): + """Test the XLSX format detection.""" + in_stream = self.founders.xlsx + self.assertEqual(detect_format(in_stream), 'xlsx') + + def test_xls_format_detect(self): + """Test the XLS format detection.""" + in_stream = self.founders.xls + self.assertEqual(detect_format(in_stream), 'xls') + def test_xlsx_import_set(self): date_time = datetime.datetime(2019, 10, 4, 12, 30, 8) data.append(('string', 42, 21.55, date_time)) |