[reportlab-users] Formatting decimals inside Table

Andy Robinson andy at reportlab.com
Fri Sep 7 03:52:44 EDT 2012

Our tables don't really know or care about their cell contents, so the
contents of each table cell should be the string you want to display.
This is a deliberate design decision - it's easier to let you
prepare the data, because otherwise we would need an infinite variety
of formatting utilities - numeric formats, currency symbols, and all
the special cases where someone doesn't want a number in that
particular cell etc etc. If you are always going to show, say, two
digits after the comma, then formatting it yourself and right-aligning
will work, because fonts almost always have a constant width for all
the digits to help numbers line up neatly.

There is also a decimal alignment property for the table styles, which
helps the English-language folks. tests/test_platypus_tables.py
shows the use of this (example pasted below). However, it hunts
within the string for a '.' character and aligns the string from that,
so it won't do the same for a comma. I can take a look at whether we
could make the 'pivot character' configurable in the table style if
you wish.

t = Table([[u'Corporate Assets','Amount'],
['Fixed Assets','1,234,567.89'],
['Company Vehicle','1,234.8901'],
['Petty Cash','42'],
[u'Intellectual Property\u00ae','(42,078,231.56)'],
['Boardroom Flat Screen','60 inches'],
['Net Position','Deep Sh*t.Really']

ts = TableStyle(
[#first the top row
('ALIGN', (1,1), (-1,-1), 'CENTER'),
('LINEABOVE', (0,0), (-1,0), 1, colors.purple),
('LINEBELOW', (0,0), (-1,0), 1, colors.purple),
('FONT', (0,0), (-1,0), 'Times-Bold'),

#bottom row has a line above, and two lines below
('LINEABOVE', (0,-1), (-1,-1), 1, colors.purple), #last 2
are count, sep
('LINEBELOW', (0,-1), (-1,-1), 0.5, colors.purple, 1, None, None, 4,1),
('LINEBELOW', (0,-1), (-1,-1), 1, colors.red),
('FONT', (0,-1), (-1,-1), 'Times-Bold'),

#numbers column
('ALIGN', (1,1), (-1,-1), 'DECIMAL'),
('RIGHTPADDING', (1,1), (-1,-1), 36),
('TEXTCOLOR', (1,4), (1,4), colors.red),

On 7 September 2012 07:40, Pasi Oja-Nisula <pon at iki.fi> wrote:

> Hi,


> I'm doing generic reports (recordsets from database) with Reportlab. By

> looking at the data I can do left/right alignment based on datatype. I

> just modify the tablestyle accordingly. But now I need to fix

> decimal.Decimal fields. They look fine except the decimal point. I really

> need an option to change this to comma instead of point. So if the data is

> "1234.500" it needs to read "1234,500". Actually it would be nice to have

> other formatting also, but for now this is the major stumbling point.


> I googled and searched the archives and closest I found was

> lib.formatters.DecimalFormatter, but it seems to be usable for charting

> only. So far I have no ideas but to loop through the data and convert

> Decimals to strings with something like str(d).replace('.', ',')


> Am I missing something obvious or is this really the only option?


> Thanks,


> Pasi

> _______________________________________________

> reportlab-users mailing list

> reportlab-users at lists2.reportlab.com

> http://two.pairlist.net/mailman/listinfo/reportlab-users

Andy Robinson
Managing Director
ReportLab Europe Ltd.
Thornton House, Thornton Road, Wimbledon, London SW19 4NG, UK
Tel +44-20-8405-6420

More information about the reportlab-users mailing list