Format Cells to Display Currency

1,045 views
Skip to first unread message

Axe Box

unread,
Apr 1, 2010, 7:11:07 PM4/1/10
to python-excel
I have this code to create a style storing US dollar values in a cell:

currencyStyle = xlwt.XFStyle()
currencyFont = xlwt.Font()
currencyFont.bold = False
currencyFont.name = 'Arial New'
currencyStyle.font = currencyFont
currencyStyle.format = '$#,##0'


Then I write lines in my spreadsheet like to:
sheet.write(currentRow, 4, '%.2f' %
float(record['valueDollars'] ), currencyStyle )


But they do not have the dollar sign or commas in the number. What am
I doing wrong? The output I would like looks so: $1,000.00


Any ideas?

John Machin

unread,
Apr 1, 2010, 10:03:39 PM4/1/10
to python...@googlegroups.com
On 2/04/2010 10:11 AM, Axe Box wrote:
> I have this code to create a style storing US dollar values in a cell:
>
> currencyStyle = xlwt.XFStyle()
> currencyFont = xlwt.Font()
> currencyFont.bold = False
> currencyFont.name = 'Arial New'
> currencyStyle.font = currencyFont
> currencyStyle.format = '$#,##0'

Have you read the tutorial obtainable from http://www.python-excel.org ?
"easyxf" isn't just marketing hype, it really is easy (or at least
much easier) :-)

> Then I write lines in my spreadsheet like to:
> sheet.write(currentRow, 4, '%.2f' %
> float(record['valueDollars'] ), currencyStyle )

>
> But they do not have the dollar sign or commas in the number. What am
> I doing wrong? The output I would like looks so: $1,000.00

This might give you a clue: open your file with OOo Calc or Excel or
Gnumeric, and widen column E (rowx index == 4) to say twice its normal
width. Do the "numbers" cling to the right or to the left of the column?

Reply all
Reply to author
Forward
0 new messages