diff --git a/data/special/merged-cells-boolean.xls b/data/special/merged-cells-boolean.xls new file mode 100644 index 00000000..45049e38 Binary files /dev/null and b/data/special/merged-cells-boolean.xls differ diff --git a/data/special/table-with-booleans.xls b/data/special/table-with-booleans.xls new file mode 100644 index 00000000..2686249f Binary files /dev/null and b/data/special/table-with-booleans.xls differ diff --git a/setup.py b/setup.py index 666a8bbf..4b80919a 100644 --- a/setup.py +++ b/setup.py @@ -38,7 +38,7 @@ def read(*paths): # Format: xls 'xlrd>=1.0,<2.0', # Format: xlsx - 'openpyxl>=2.4,<3.0', + 'openpyxl>=2.4,<2.5', ] INSTALL_FORMAT_DATAPACKAGE_REQUIRES = [ 'datapackage>=1.1.3,<2.0', diff --git a/tabulator/parsers/xls.py b/tabulator/parsers/xls.py index a21759a9..77da3334 100644 --- a/tabulator/parsers/xls.py +++ b/tabulator/parsers/xls.py @@ -87,9 +87,14 @@ def __iter_extended_rows(self): row_number = x + 1 row = [] for y, value in enumerate(self.__sheet.row_values(x)): + # hacky way to parse booleans (cell values are integers (0/1) other way) + if self.__sheet.cell(x, y).ctype == xlrd.XL_CELL_BOOLEAN: + value = bool(value) if self.__fill_merged_cells: for xlo, xhi, ylo, yhi in self.__sheet.merged_cells: if x in range(xlo, xhi) and y in range(ylo, yhi): value = self.__sheet.cell_value(xlo, ylo) + if self.__sheet.cell(xlo, ylo).ctype == xlrd.XL_CELL_BOOLEAN: + value = bool(value) row.append(value) yield (row_number, None, row) diff --git a/tests/formats/test_xls.py b/tests/formats/test_xls.py index 1187c9d6..7a8abef3 100644 --- a/tests/formats/test_xls.py +++ b/tests/formats/test_xls.py @@ -65,6 +65,23 @@ def test_stream_xlsx_merged_cells_fill(): assert stream.read() == [['data', 'data'], ['data', 'data'], ['data', 'data']] +def test_stream_xls_with_boolean(): + with Stream('data/special/table-with-booleans.xls') as stream: + assert stream.headers is None + assert stream.read() == [['id', 'boolean'], [1.0, True], [2.0, False]] + + +def test_stream_xlsx_merged_cells_boolean(): + source = 'data/special/merged-cells-boolean.xls' + with Stream(source) as stream: + assert stream.read() == [[True, ''], ['', ''], ['', '']] + + +def test_stream_xlsx_merged_cells_fill_boolean(): + source = 'data/special/merged-cells-boolean.xls' + with Stream(source, fill_merged_cells=True) as stream: + assert stream.read() == [[True, True], [True, True], [True, True]] + # Parser def test_parser_xls():