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 /tablib | |
| parent | 34334e72a12c28425aff9162da13c3300bb80e18 (diff) | |
| download | tablib-91062672b5406972da75bdcf02ad8373fff96377.tar.gz | |
Fixed #373 - Properly detect xlsx format
Diffstat (limited to 'tablib')
| -rw-r--r-- | tablib/formats/__init__.py | 3 | ||||
| -rw-r--r-- | tablib/formats/_json.py | 2 | ||||
| -rw-r--r-- | tablib/formats/_xlsx.py | 5 |
3 files changed, 7 insertions, 3 deletions
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 |
