ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
When saving certain files, we ended up with the following error:
File "weblate/formats/external.py", line 66, in save_content
workbook.save(handle)
File "openpyxl/workbook/workbook.py", line 386, in save
save_workbook(self, filename)
File "openpyxl/writer/excel.py", line 294, in save_workbook
writer.save()
File "openpyxl/writer/excel.py", line 275, in save
self.write_data()
File "openpyxl/writer/excel.py", line 77, in write_data
self._write_worksheets()
File "openpyxl/writer/excel.py", line 215, in _write_worksheets
self.write_worksheet(ws)
File "openpyxl/writer/excel.py", line 200, in write_worksheet
writer.write()
File "openpyxl/worksheet/_writer.py", line 359, in write
self.write_rows()
File "openpyxl/worksheet/_writer.py", line 125, in write_rows
self.write_row(xf, row, row_idx)
File "openpyxl/worksheet/_writer.py", line 147, in write_row
write_cell(xf, self.ws, cell, cell.has_style)
File "openpyxl/cell/_writer.py", line 130, in lxml_write_cell
el.text = value
File "src/lxml/etree.pyx", line 1048, in lxml.etree._Element.text.__set__
File "src/lxml/apihelpers.pxi", line 749, in lxml.etree._setNodeText
File "src/lxml/apihelpers.pxi", line 737, in lxml.etree._createTextNode
File "src/lxml/apihelpers.pxi", line 1530, in lxml.etree._utf8
ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
The error is triggered by lxml because of some forbidden characters:
I think the following regexp should be extended to catch invalid values when setting the text instead of crashing with ValueError
later when saving the file:
https://foss.heptapod.net/openpyxl/openpyxl/-/blob/branch/3.1/openpyxl/cell/cell.py#L45