Python Forum

Full Version: Problem with Polish characters in xlsx file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have got a problem with Polish characters in xlsx file(words do not have Polish letters that have disappeared in an unknown way), which I take from table. What am I doing wrong?

import os
import cx_Oracle
import xlsxwriter

con = cx_Oracle.connect('user/password@SID')

cur = con.cursor()

custdata =[]


def main():


    L_sFilename ="List_PNA.xlsx"

    workbook = xlsxwriter.Workbook(L_sFilename)
    worksheet = workbook.add_worksheet()

    format = workbook.add_format({'bold': True, 'font_color': 'red'})
    format.set_font_name('Times New Roman')
    format.set_align('center')

    worksheet.write('A1','PNA',format)
    worksheet.set_column('A:A', 60)

    worksheet.write('B1','BOX',format)
    worksheet.set_column('B:B', 60)

    worksheet.write('C1','NAME',format)
    worksheet.set_column('C:C', 60)

    worksheet.write('D1','PO',format)
    worksheet.set_column('D:D', 60)

    worksheet.write('E1','STREET',format)
    worksheet.set_column('E:E', 60)

    worksheet.write('F1','NUMBERS',format)
    worksheet.set_column('F:F', 60)

    worksheet.write('G1','CITY',format)
    worksheet.set_column('G:G', 60)

    worksheet.write('H1','PROVINCE',format)
    worksheet.set_column('H:H', 60)

    worksheet.write('I1','DISTRICT',format)
    worksheet.set_column('I:I', 60)

    worksheet.write('J1','COMMUNE',format)
    worksheet.set_column('J:J', 60)

    format2 = workbook.add_format()
    format2.set_align('center')
    format2.set_font_color('black')
    format2.set_font_name('Times New Roman')


    L_sSelect ="""select * from my_xml_schema.LIST_PNA_TAB"""

    cur.arraysize = 100
    cur.execute(L_sSelect)


    for result in cur:
        custdata.append((result))

    cur.close()
    con.close()

    for r, row in enumerate(custdata,1):

        for c, col in enumerate(row):
            a=str(col)
            cell = unicode(a, errors='ignore')
            worksheet.write(r, c, cell,format2)
            #print " r= "+str(r) + " c= "+str(c) + " val= "+cell


    workbook.close()
    os.startfile(os.getcwd()+"\\"+L_sFilename)



if __name__ == "__main__":
    main()
This looks like python 2 code. Are you still using python 2? The error comes probably from the unicode(a, errors='ignore') if it silently removes polish characters. It could probably be replaced by a.decode("some encoding") or codecs.decode(a, "some encoding")
Yes, I have got a version 2.7.15. Your solutions doesn`t work.
You're using a language, which is deprecated. The support of Python 2.7 ends in 2020.
With Python 3 you get better Unicode-Support (also for emoticons).
Try it with Python 3. If you still get the same results, something is wrong with the encoding. Fixing encoding errors is more guessing than knowing.
Mikser Wrote:Yes, I have got a version 2.7.15.
I agree with DeaD_EyE, upgrade to python 3 first. Most python 2 encoding issues simply disappear by moving to python 3. It is a waste of time to solve them.