[reportlab-users] Format cells inside table

84 views
Skip to first unread message

Jakob Damgaard Møller

unread,
Aug 18, 2015, 4:24:10 AM8/18/15
to reportl...@lists2.reportlab.com
Hello, this is my first post to this list, so i hope i do it right.

I try to create a table in reportlab.

data = [
  ['Firstname', 'Lastname', 'Points'],
  ['Jakob', 'Møller', '5'],
  ['Anna', 'Møller', '10'],
etc...
]

I want to format cell in colors based on result.
F.ex:
if points > 5:
  cell_color = "green"
elif point < 5:
  cell_color = "red"
else:
  cell_color = "yellow"

I have looked at this:
But as i understand it, the color is defined on coordinates. Cant i assign a color when i create "data" ?

Andy Robinson

unread,
Aug 18, 2015, 7:03:20 AM8/18/15
to reportlab-users
Hi Jakob,

That's true, it's based on positions.

In Report Markup Language, we allow things like
<td textColor="green">99</td>
but we implemented it by scanning the markup and generating extra
table style commands for that particular table as we parsed the input.

Your best bet would be to write a function which looks at the data and
generates a new table style, based on the original one but adding
extra block commands to colour each cell that needs colouring. It
could be quite short and reusable. If it's not clear what to do, one
of us could try to help in the next few days.

- Andy

> _______________________________________________
> reportlab-users mailing list
> reportl...@lists2.reportlab.com
> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
>
_______________________________________________
reportlab-users mailing list
reportl...@lists2.reportlab.com
https://pairlist2.pair.net/mailman/listinfo/reportlab-users

Chris Else

unread,
Aug 24, 2015, 7:03:59 PM8/24/15
to reportlab-users
Hi Jakob,

Using your example and the code at:

I have a solution that might work for you.

Kind regards

Chris


from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
 
doc = SimpleDocTemplate("simple_table.pdf", pagesize=letter)
# container for the 'Flowable' objects
elements = []

table_data = [['Firstname', 'Lastname', 'Points']]
table_style = [('TEXTCOLOR', (0,0), (-1, -1), colors.black),
  ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
  ('BOX', (0,0), (-1,-1), 0.25, colors.black)]

data = [['Jakob', 'Moller', 5],
  ['Anna', 'Moller', 10],
  ['Another', 'Person', 1]]

row_number = 1

for forename, surname, points in data:
  if points > 5:
    cell_colour = colors.green
  elif points < 5:
    cell_colour = colors.red
  else:
    cell_colour = colors.yellow

  table_data = table_data + [[forename, surname, str(points)]]
  table_style = table_style + [('BACKGROUND', (0,row_number), (-1, row_number), cell_colour)]

  row_number = row_number + 1
  
t=Table(table_data)
t.setStyle(TableStyle(table_style))
elements.append(t)
# write the document to disk
doc.build(elements)

Jakob Damgaard Møller

unread,
Aug 25, 2015, 2:14:22 AM8/25/15
to reportlab-users
Hello Chris,
i should just "figure" how the Tabletyle was working.
I wanted to set the color when i added the "data".
But now i just create the data and the tableStyle in the same loop. So i got what i wanted. :o)

Thanks for your help
Reply all
Reply to author
Forward
0 new messages