Skip to content

Commit d0f14e6

Browse files
authored
Merge pull request #108 from OpenDataServices/421-remove-control-char
[#421] remove control char
2 parents 7b2f4d4 + be583bc commit d0f14e6

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

flattentool/output.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
"""
66

77
import openpyxl
8+
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
89
import csv
910
import os
1011
import sys
12+
from warnings import warn
13+
import six
1114

1215
if sys.version > '3':
1316
import csv
@@ -52,7 +55,16 @@ def write_sheet(self, sheet_name, sheet):
5255
worksheet.title = sheet_name
5356
worksheet.append(sheet_header)
5457
for sheet_line in sheet.lines:
55-
worksheet.append([ sheet_line.get(x) for x in sheet_header ])
58+
line = []
59+
for header in sheet_header:
60+
value = sheet_line.get(header)
61+
if isinstance(value, six.text_type):
62+
new_value = ILLEGAL_CHARACTERS_RE.sub('', value)
63+
if new_value != value:
64+
warn("Character(s) in '{}' are not allowed in a spreadsheet cell. Those character(s) will be removed".format(value))
65+
value = new_value
66+
line.append(value)
67+
worksheet.append(line)
5668

5769
def close(self):
5870
self.workbook.remove_sheet(self.workbook.active)

0 commit comments

Comments
 (0)