Re: Format cells inside table

Chris Else <[email protected]>
Newsgroups gmane.comp.python.reportlab.user
Message-ID <CAB0HUwQJ1K49bsZGnLiMDbbNv8QJhX+D1Yqq-64OWsn4OXKiZg@mail.gmail.com>
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 <[email protected]> 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 <[email protected]>
> 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
> > [email protected]
> > https://pairlist2.pair.net/mailman/listinfo/reportlab-users
> >
> _______________________________________________
> reportlab-users mailing list
> [email protected]
> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.