Cannot write special characters to Excel using Openpyxl
Created originally on Bitbucket by Anonymous
Currentlty, we are querying the database(DB2) to fetch values and outputting the result to an excel file. Here is a dummy snippet:
results = conn.query ('select * from MY_TABLE') # conn: connection object ; results: List of tuples wb= Workbook(optimized_write=True) ws = wb.create_sheet(0, title='Sheet 1') for eachRow in results: ws.append(eachRow) wb.save('myfile.xlsx')
Now, we tried setting the encoding by doing this:
wb = Workbook(optimized_write=True, encoding='IBM-1252') # IBM-1252 is the code-set of our DB2 database
.. but it did not help!
We do not encounter any runtime issues, but when we try opening the file, it would throw a warning and not show any string valued columns in the excel file.
When we replaced all the special characters in the database query result and ran the same code, everything worked fine.