[reportlab-users] Format cells inside table

Chris Else ubuntu247 at gmail.com
Mon Aug 24 19:03:55 EDT 2015


Hi Jakob,

Using your example and the code at:
http://www.blog.pythonlibrary.org/2010/09/21/reportlab-tables-creating-tables-in-pdfs-with-python/

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)

On 18 August 2015 at 12:03, Andy Robinson <andy at reportlab.com> wrote:

> 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
>
>
>
>
> On 18 August 2015 at 09:23, Jakob Damgaard Møller <jakobdo at gmail.com>
> wrote:
> > 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:
> >
> http://www.blog.pythonlibrary.org/2010/09/21/reportlab-tables-creating-tables-in-pdfs-with-python/
> > But as i understand it, the color is defined on coordinates. Cant i
> assign a
> > color when i create "data" ?
> >
> > _______________________________________________
> > reportlab-users mailing list
> > reportlab-users at lists2.reportlab.com
> > https://pairlist2.pair.net/mailman/listinfo/reportlab-users
> >
> _______________________________________________
> reportlab-users mailing list
> reportlab-users at lists2.reportlab.com
> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist2.pair.net/pipermail/reportlab-users/attachments/20150825/09e1baee/attachment.html>


More information about the reportlab-users mailing list