Search code examples
pythonexcelxlsxwriter

How to write German umlauts to a spreadsheet with xlsxwriter in Python


I already tried this one, but when I open the Excel spreadsheet, the whole Excel file is blank. Is there another way?

import xlsxwriter

....
    sheet.write(1, 27, "Französisch".decode('latin1'), bold)

Solution

  • Excel, and XlsxWriter, use either ASCII or UTF-8. To write a string like that in Python 2:

    1. Encode the file as UTF-8.
    2. Include the "coding" directive at the start of the file.
    3. Use u'' to indicate a Unicode string.

    Like this:

    # _*_ coding: utf-8
    
    import xlsxwriter
    
    workbook = xlsxwriter.Workbook('example.xlsx')
    worksheet = workbook.add_worksheet()
    
    worksheet.write('B3', u'Französisch')
    
    workbook.close()
    

    enter image description here

    In Python 3 you just need to encode the file as UTF-8.

    See the Unicode examples in XlsxWriter docs.