If I understand your question correctly, you have a string x
of len(x)=7729
and while filling parts of the string into cells you encounter the error: openpyxl.utils.exceptions.IllegalCharacterError
.
Befor running your string through the loop you could replace all problematic substrings with an empty string ('') in the string, using the ILLEGAL_CHARACTERS_RE
pattern from https://stackoverflow.com/a/68578582.
import re
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
x = 'string containing substrings like \x0c or \x0b...'
clean_x= re.sub(ILLEGAL_CHARACTERS_RE, '', x)
print(clean_x)
...'string containing substrings like or ...'
The resulting clean_x
you can just use in your loop as before.
Cleaning the string beforehand in one line, is likely much faster, then doing it in every loop step (doing it once vs doing it 7729 times).
Alternatively, if you need to retain a representation of the problematic substrings, you could use the dictionary (escape_xlsx_char) from this post How to handle the exception ‘IllegalCharacterError’ in openpyxl to get an exscape of the problematic substrings.
.value=
content ? – Sobriquetprint (sys.getdefaultencoding())
? – Sobriquetexception openpyxl.utils.exceptions.IllegalCharacterError
Reason : The data submitted which cannot be used directly in Excel files. It must be removed or escaped. so the issue is some how with your data i.e urx
value – Sobriquet