[FIX] tools: ensure xlsx worksheet names are valid

When xlsx is used, the worksheet name may be longer than 31 chars and
raise an exception.

This fix will troncate the name up to 31 chars and will remove invalid
chars.

Similar patch was already done at 0122c05eff for xlwt

xlsxwriter 0.9.8 has added a parameter worksheet_class=None so adding **kw to be
compatible with installs with the latest version (requirements.txt asks for
version 0.9.3)

Closes #18077
opw-751157
This commit is contained in:
Christophe Monniez
2017-07-07 09:44:09 +02:00
committed by Martin Trigaux
parent 2454dce7c5
commit c1e154bee1
+22
View File
@@ -363,6 +363,28 @@ except ImportError:
xlwt = None
try:
import xlsxwriter
# add some sanitizations to respect the excel sheet name restrictions
# as the sheet name is often translatable, can not control the input
class PatchedXlsxWorkbook(xlsxwriter.Workbook):
# TODO when xlsxwriter bump to 0.9.8, add worksheet_class=None parameter instead of kw
def add_worksheet(self, name=None, **kw):
# invalid Excel character: []:*?/\
name = re.sub(r'[\[\]:*?/\\]', '', name)
# maximum size is 31 characters
name = name[:31]
return super(PatchedXlsxWorkbook, self).add_worksheet(name, **kw)
xlsxwriter.Workbook = PatchedXlsxWorkbook
except ImportError:
xlsxwriter = None
class UpdateableStr(local):
""" Class that stores an updateable string (used in wizards)
"""