Has anyone been able to write UTF-8 characters using python xlwt?

I am trying to write data to an excel file containing japanese characters. I use codec.open () to get the data, and this seems to work fine, but I encounter this error when I try to write data:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 16-17: ordinal not in range(128) 

I do not understand why the program will insist on using ascii here. When I created a new book object, I did this with

 wb = xlwt.Workbook(encoding='utf-8') 

and both the program file and the file that it reads are saved as UTF-8.

Does anyone have any ideas?

EDIT: Here is a link to the xlwt package. http://pypi.python.org/pypi/xlwt

+6
source share
2 answers

In an Excel 97-2003 XLS file, each text fragment is encoded in latin1 , if possible, otherwise UTF-16LE , with a flag to indicate which one. For this, xlwt nees a unicode object. If the caller sends the str object, xlwt will try to decode it using the encoding specified in the Workbook () call (the default is ascii ).

It works; try running the following short script and open the resulting file with Excel.

 import xlwt wb = xlwt.Workbook(encoding="UTF-8") uc = u"".join(unichr(0x0410 + i) for i in xrange(32)) # some Cyrillic characters u8 = uc.encode("UTF-8") ws = wb.add_sheet("demo") ws.write(0, 0, uc) ws.write(1, 0, u8) ws.write(2, 0, xlwt.Formula("A1=A2")) ws.write(3, 0, "ASCII is a subset of UTF-8") wb.save("xlwt_write_utf8.xls") 

The fact that you are getting a coding error, not a decoding error, indicates a possible problem in the input of your script file. Please put the shortest script that causes the error you receive. The script should contain something like print repr(your_utf8_text) just before the error output so that we can see exactly what the text data is. Please provide a complete error message and full trace, as well as the contents ( print repr(contents) ) of your very short input file.

+13
source

As suggested by this question, setting the encoding in WorkBook

 wb = xlwt.Workbook(encoding='latin-1') 

should also solve the problem (it worked for me).

+2
source

Source: https://habr.com/ru/post/895825/


All Articles