Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reportlab Dynamic Table Q
#1
Hi, I am struggling with using a parsed JSON list as the data for a dynamic table in reportlab.

Can anyone help me out here -- below is a generic reportlab script for a table. What I need help with is using the same principle but with a list, which will always be changing, instead of the static data that is used in the example below.. Thanks for any help!

#!/usr/local/bin/python
 
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4, inch, landscape
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
 
doc = SimpleDocTemplate("test_report_lab.pdf", pagesize=A4, rightMargin=30,leftMargin=30, topMargin=30,bottomMargin=18)
doc.pagesize = landscape(A4)
elements = []
 
data = [
["Letter", "Number", "Stuff", "Long stuff that should be wrapped"],
["A", "01", "ABCD", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"],
["B", "02", "CDEF", "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"],
["C", "03", "SDFSDF", "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"],
["D", "04", "SDFSDF", "DDDDDDDDDDDDDDDDDDDDDDDD DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"],
["E", "05", "GHJGHJGHJ", "EEEEEEEEEEEEEE EEEEEEEEEEEEEEEEE EEEEEEEEEEEEEEEEEEEE"],
]
 
#TODO: Get this line right instead of just copying it from the docs
style = TableStyle([('ALIGN',(1,1),(-2,-2),'RIGHT'),
                       ('TEXTCOLOR',(1,1),(-2,-2),colors.red),
                       ('VALIGN',(0,0),(0,-1),'TOP'),
                       ('TEXTCOLOR',(0,0),(0,-1),colors.blue),
                       ('ALIGN',(0,-1),(-1,-1),'CENTER'),
                       ('VALIGN',(0,-1),(-1,-1),'MIDDLE'),
                       ('TEXTCOLOR',(0,-1),(-1,-1),colors.green),
                       ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                       ('BOX', (0,0), (-1,-1), 0.25, colors.black),
                       ])
 
#Configure style and word wrap
s = getSampleStyleSheet()
s = s["BodyText"]
s.wordWrap = 'CJK'
data2 = [[Paragraph(cell, s) for cell in row] for row in data]
t=Table(data2)
t.setStyle(style)
 
#Send the data and build the file
elements.append(t)
doc.build(elements)
I think I need to do a list of lists... but am relatively new to python so looking for a little bump in the right direction
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Looking for documentation on Reportlab's canvas.transform() function NeilUK 1 552 Aug-23-2023, 01:21 PM
Last Post: NeilUK
  Right to left alignment in python report using Reportlab jalal0034 1 1,761 Sep-27-2022, 04:25 AM
Last Post: jalal0034
  Label Maker FPDF, Reportlab jamesaarr 1 2,601 Aug-09-2021, 11:57 PM
Last Post: Pedroski55
  Need help on extract dynamic table data Dr_Strange 0 2,446 Apr-30-2021, 07:03 AM
Last Post: Dr_Strange
  cyrillic symbols in tables in reportlab. hiroz 5 11,196 Sep-10-2020, 04:57 AM
Last Post: bradmalcom
  Using Reportlab to create a landscape pdf SmukasPlays 2 5,280 Aug-09-2020, 09:31 PM
Last Post: SmukasPlays
  Mysql CREATE TABLE IF NOT EXISTS dynamic table name nisusavi 0 2,301 Apr-29-2020, 06:45 PM
Last Post: nisusavi
  Help! - How to create a Title for a Reportlab Table crabbylou 0 5,272 Mar-29-2020, 09:14 PM
Last Post: crabbylou
  ReportLab Polypop77 0 1,802 Mar-20-2020, 01:17 PM
Last Post: Polypop77
  Python Reportlab Wordwrap Table Mady 0 6,906 Dec-18-2018, 06:31 AM
Last Post: Mady

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020