Skip to content

Commit 0857681

Browse files
xlsx export: remove redundant code
1 parent bff435d commit 0857681

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/tablib/formats/_xlsx.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,12 @@ def dset_sheet(cls, dataset, ws, freeze_panes=True, escape=False):
158158

159159
# wrap the rest
160160
else:
161-
try:
162-
str_col_value = str(col)
163-
except TypeError:
164-
str_col_value = ''
165-
if '\n' in str_col_value:
161+
if '\n' in str(col):
166162
cell.alignment = wrap_text
167163

168164
try:
169165
cell.value = col
170-
except (ValueError, TypeError):
166+
except ValueError:
171167
cell.value = str(col)
172168

173169
if escape and cell.data_type == 'f' and cell.value.startswith('='):

tests/test_tablib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,16 @@ def test_xlsx_bad_dimensions(self):
11801180
data = tablib.Dataset().load(fh, read_only=False)
11811181
self.assertEqual(data.height, 3)
11821182

1183+
def test_xlsx_raise_ValueError_on_cell_write_during_export(self):
1184+
"""Test that the process handles errors which might be raised
1185+
when calling cell setter."""
1186+
# openpyxl does not handle array type, so will raise ValueError,
1187+
# which results in the array being cast to string
1188+
data.append(([1],))
1189+
_xlsx = data.export('xlsx')
1190+
wb = load_workbook(filename=BytesIO(_xlsx))
1191+
self.assertEqual('[1]', wb.active['A1'].value)
1192+
11831193

11841194
class JSONTests(BaseTestCase):
11851195
def test_json_format_detect(self):

0 commit comments

Comments
 (0)