Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Parse xls booleans as booleans, not integeres (#232)
Browse files Browse the repository at this point in the history
* [parsers][m]: parse xls booleans as booleans, not integeres

* pin openpyxl to versions < 2.5 because of a bug on 2.5
  • Loading branch information
zelima authored and roll committed Mar 21, 2018
1 parent 4c1b394 commit 9675ead
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
Binary file added data/special/merged-cells-boolean.xls
Binary file not shown.
Binary file added data/special/table-with-booleans.xls
Binary file not shown.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 5 additions & 0 deletions tabulator/parsers/xls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
17 changes: 17 additions & 0 deletions tests/formats/test_xls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 9675ead

Please sign in to comment.