Fixed #373 - Properly detect xlsx format

This commit is contained in:
Claude Paroz
2019-10-04 20:32:01 +02:00
parent 34334e72a1
commit 91062672b5
5 changed files with 19 additions and 4 deletions
+1
View File
@@ -6,6 +6,7 @@
- Fixed `DataBook().load` parameter ordering (first stream, then format). - Fixed `DataBook().load` parameter ordering (first stream, then format).
- Fixed a regression for xlsx exports where non-string values were forced to - Fixed a regression for xlsx exports where non-string values were forced to
strings (#314). strings (#314).
- Fixed xlsx format detection (which was often detected as `xls` format).
- Added search to all documentation pages - Added search to all documentation pages
- Open xlsx workbooks in read-only mode (#316) - Open xlsx workbooks in read-only mode (#316)
- Unpin requirements - Unpin requirements
+2 -1
View File
@@ -17,4 +17,5 @@ from . import _df as df
from . import _rst as rst from . import _rst as rst
from . import _jira as jira 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)
+1 -1
View File
@@ -55,5 +55,5 @@ def detect(stream):
try: try:
json.loads(stream) json.loads(stream)
return True return True
except ValueError: except (TypeError, ValueError):
return False return False
+4 -1
View File
@@ -22,8 +22,11 @@ extensions = ('xlsx',)
def detect(stream): def detect(stream):
"""Returns True if given stream is a readable excel file.""" """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: try:
openpyxl.reader.excel.load_workbook(stream) openpyxl.reader.excel.load_workbook(stream, read_only=True)
return True return True
except openpyxl.shared.exc.InvalidFileException: except openpyxl.shared.exc.InvalidFileException:
pass pass
+11 -1
View File
@@ -13,7 +13,7 @@ from uuid import uuid4
from MarkupPy import markup from MarkupPy import markup
import tablib import tablib
from tablib.compat import unicode, is_py3 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 from tablib.formats import _csv as csv_module
@@ -783,6 +783,16 @@ class TSVTests(BaseTestCase):
class XLSXTests(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): def test_xlsx_import_set(self):
date_time = datetime.datetime(2019, 10, 4, 12, 30, 8) date_time = datetime.datetime(2019, 10, 4, 12, 30, 8)
data.append(('string', 42, 21.55, date_time)) data.append(('string', 42, 21.55, date_time))