views:

950

answers:

3

Task:
I generate formated excel tables from csv-files by using the python package pyExcelerator (comparable with xlwt). I need to be able to write less-than-or-equal-to (≤) and greater-than-or-equal-to (≥) signs.

So far:
I can save my table as csv-files with UTF-8 encoding, so that I can view the special characters in my text editor, by adding the following line to my python source code:

#! /usr/bin/env python
# -*- coding: UTF-8 -*-

Problem:
However, there is no option to choose UTF-8 as font in pyExcelerator's Font class. The only options are:

CHARSET_ANSI_LATIN          = 0x00
CHARSET_SYS_DEFAULT         = 0x01
CHARSET_SYMBOL              = 0x02
CHARSET_APPLE_ROMAN         = 0x4D
CHARSET_ANSI_JAP_SHIFT_JIS  = 0x80
CHARSET_ANSI_KOR_HANGUL     = 0x81
CHARSET_ANSI_KOR_JOHAB      = 0x82
CHARSET_ANSI_CHINESE_GBK    = 0x86
CHARSET_ANSI_CHINESE_BIG5   = 0x88
CHARSET_ANSI_GREEK          = 0xA1
CHARSET_ANSI_TURKISH        = 0xA2
CHARSET_ANSI_VIETNAMESE     = 0xA3
CHARSET_ANSI_HEBREW         = 0xB1
CHARSET_ANSI_ARABIC         = 0xB2
CHARSET_ANSI_BALTIC         = 0xBA
CHARSET_ANSI_CYRILLIC       = 0xCC
CHARSET_ANSI_THAI           = 0xDE
CHARSET_ANSI_LATIN_II       = 0xEE
CHARSET_OEM_LATIN_I         = 0xFF

Do any of these character sets contain the less-than-or-equal-to and greater-than-or-equal-to signs? If so, which on?
Which python encoding name corresponds to these sets? Is there another way for generating these special characters?

+2  A: 

You may be overthinking the problem. The font shouldn't play into the matter, although character encoding might.

In any case, I was able to use xlwt to create an excel spreadsheet with less-than-equal and greater-than-equal signs with the following script:

import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('Test Sheet')
lte = u'\u2264'
gte = u'\u2265'
ws.write(0,0,lte+gte)
wb.save('foo.xls')

Note that -- coding: utf-8 -- is not required because the special characters are encoded with their unicode numeric indices. In general, I recommend using unicode where possible.

It's also possible to use utf-8 and type the characters directly into the Python code. This would be exactly the same except for how the characters are entered:

#-*- coding: utf-8 -*- 
import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('Test Sheet')
lte = u'≤'
gte = u'≥'
ws.write(0,0,lte+gte)
wb.save('foo.xls')

Note, however, that you must be using an editor that is aware that you are saving the Python code as UTF-8. If your editor encodes the file in any other way, the special characters will not be parsed properly when loaded by the Python interpreter.

Jason R. Coombs
thanks, that worked out
SimonSalman
A: 

(1) Re: """ I can save my table as csv-files with UTF-8 encoding, so that I can view the special characters in my text editor, by adding the following line to my python source code:

#! /usr/bin/env python
# -*- coding: UTF-8 -*-
"""

Being able to write a file with characters encoded in UTF-8 is NOT dependant on what encoding is used in the source of the program that writes the file!

(2) UTF-8 is an encoding, not a font. Those charsets in an Excel FONT record are a blast from the past AFAIK. I've not heard from any xlwt user who ever thought it necessary to use other than the default for the charset. Just feed unicode objects to xlwt as demonstrated by Jason ... if you have an appropriate font on your system (see if you can display the characters in OpenOffice Calc), you should be OK.

(3) Any particular reason for using pyExcelerator instead of xlwt?

John Machin
to (3): I just found pyExcelerator first and installed it. Would you recommend xlwt, instead?
SimonSalman
@SimonSalman: Of course :-) ... Disclosure: I'm the maintainer of `xlwt`.
John Machin
A: 

This should help with writing UTF-8 chars using pyexcelerator or xlwt:

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

edit:

Seems it's not working for pyexcelerator, but I havent confirmed it.

yoshi
@yoshi: It won't help much with pyExcelerator -- it has a HARD-CODED default encoding (cp1251). xlwt had the caller-specifiable default encoding functionality added in Feb 2007. pyExcelerator is not maintained. A question: what is a UTF-8 character?
John Machin
@John Machin: By UTF-8 character I meant a character that's encoded using UTF-8, not ASCII. Have I got something wrong?
yoshi