Tables nested in tables inherit style -- but shouldn't!

20 views
Skip to first unread message

Dan Moss

unread,
Oct 28, 2024, 3:59:32 AM10/28/24
to reportlab-users
Hello, First time to ReportLab so I'm getting to know it.

I'm creating a section of my PDF that needs two data tables side-by-side, with a bit of white space between the two tables. My solution is to create one table with two columns, and nest the data tables within the two columns. Seems simple enough and it works, but there's no command (in Platypus) to add the white space.

My solution for the white space is to add a third table (1 col, x rows) with white grid white background to add the space. It's almost perfect but the top row style bleeds into the middle. (see diagram luridly coloured for illustration purposes) , here you can see the middle section of the main table with middle table in pink. The green shows that styling is bleeding across the top row.

Left table has one row, right has 4.
Screenshot from 2024-10-28 18-54-27.png

Is there a solution to either add white space between the data tables or suppress the spreading of styling across the main table?

Code snippet:

oak_grey = "#F5F5F5"

top_table_style = TableStyle([
#     ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
#     ('BACKGROUND', (0, 0), (-1, -1), oak_grey),
    ('BACKGROUND', (0, 0), (-1, -1), colors.green),
    ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
    ('VALIGN', (0, 0), (-1, -1), 'TOP'),
    ('FONTNAME', (0,0), (0,-1), 'SourceSansPro-Bold'),
    ('FONTNAME', (1,-1), (-1,-1), 'SourceSansPro-Regular'),
    ("TOPPADDING", (0,0), (-1,-1), 6),
    ("BOTTOMPADDING", (0,0), (-1,-1), 6),
    ('FONTSIZE', (0,0), (-1,-1), 9),
    ('LEADING', (0,0), (-1,-1), 12)
   
])

# Left Table with "Date of Birth" and its value
left_data = [
    ["Date of Birth", "21 Mar 2019"]
]

top_table_widths = [2.5 * cm, 7 * cm]

left_table = Table(left_data, colWidths=top_table_widths, cornerRadii=(3, 3, 3, 3))
left_table.setStyle(top_table_style)

# Right Table with other information data
right_data = [
    ["Practitioner", ""],
    ["Appointment", ""],
    ["Created", ""],
    ["Last updated", ""],
    ["Present", ""]
]



mid_table = Table([[" "]] * len(right_data),
                  colWidths=[0.2 * cm],
                  style=[
                      ('GRID', (0,0), (-1,-1), 1, colors.black),
#                                ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
#                                ('VALIGN', (0, 0), (-1, -1), 'TOP'),
                               ('FONTNAME', (0,0), (0,-1), 'SourceSansPro-Bold'),
                               ('FONTNAME', (1,-1), (-1,-1), 'SourceSansPro-Regular'),
                               ("TOPPADDING", (0,0), (-1,-1), 6),
                               ("BOTTOMPADDING", (0,0), (-1,-1), 6),
                               ('FONTSIZE', (0,0), (-1,-1), 9),
                               ('BACKGROUND', (0, 0), (-1, -1), colors.pink),
#                       ('LEADING', (0,0), (-1,-1), 20)
                  ])

right_table = Table(right_data, colWidths=top_table_widths, cornerRadii=(3, 3, 3, 3))
right_table.setStyle(top_table_style)

# Combine the left and right tables into a larger table
info_section = Table([[left_table, mid_table, right_table]])
info_section.setStyle(TableStyle([
    ('VALIGN', (0, 0), (-1, -1), 'TOP'),
    ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
    ('GRID', (0,0), (-1,-1), 1, colors.black),
#     ('BACKGROUND', (1, 0), (1, -1), colors.pink),
#     ('ROWBACKGROUNDS', (0,0), (-1,-1), [colors.white, colors.white])
    ('INNERGRID', (1,0), (1,-1), 12, colors.white),
]))

Dan Moss

unread,
Oct 29, 2024, 4:09:47 AM10/29/24
to reportlab-users
Found the issue, it was simple. I had declared the table widths to be too long, in the line below

top_table_widths = [2.5 * cm, 7 * cm]

Setting the second column width to 6cm fixed it.
Reply all
Reply to author
Forward
0 new messages