Reputation: 25
I am in a situation where I need to create a table inside the table cell, which is the description field in the data which will be a list itself. I need to make a table out of that list. Below is the code that I am currently using to create a normal table in reportlab, I just need to insert a table inside a table in the description field of this table, the description field itself would be a list inside the data list.
from reportlab.lib.pagesizes import A1
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import BaseDocTemplate, Frame, PageTemplate, Paragraph, Table, TableStyle
from functools import partial
from reportlab.lib import colors
from reportlab.platypus.doctemplate import SimpleDocTemplate
cm = 2.58
styles = getSampleStyleSheet()
data = [["Register","S.No.", "PON","Description","Quantity","Cost","Supplier","DOR","RVN","Alloted Lab",
"TON","TOD","Transferred Dept./Lab","Remarks"],
["Register REG1", 12, 56, Paragraph('Here is large field retrieve from database Here is large field retrieve from database Here is large field retrieve from database', styles['Normal']), 4,"4466561", "SHAKTI", "2021-09-05", 778, "Iron Man Lab", 4566, "2021-09-04", "Tony Stark Lab", "This is the remark for REG1"]]
for i in range(0,6):
data.extend(data)
doc = SimpleDocTemplate('testtable.pdf', pagesize=A1)
table = Table(data, repeatRows=1)
# add style
numberofcols = len(data[0])
style = TableStyle([
('BACKGROUND', (0,0), (numberofcols,0), colors.green),
('TEXTCOLOR',(0,0),(-1,0),colors.whitesmoke),
('ALIGN',(0,0),(-1,-1),'CENTER'),
('FONTNAME', (0,0), (-1,0), 'Courier-Bold'),
('FONTSIZE', (0,0), (-1,0), 14),
('BOTTOMPADDING', (0,0), (-1,0), 12),
('BACKGROUND',(0,1),(-1,-1),colors.beige),
])
table.setStyle(style)
# 2) Alternate backgroud color -- formatting
rowNumb = len(data)
for i in range(1, rowNumb):
if i % 2 == 0:
bc = colors.burlywood
else:
bc = colors.beige
ts = TableStyle(
[('BACKGROUND', (0,i),(-1,i), bc)]
)
table.setStyle(ts)
# 3) Add borders -- formatting
ts = TableStyle(
[
('BOX',(0,0),(-1,-1),2,colors.black),
('LINEBEFORE',(2,1),(2,-1),2,colors.red),
('LINEABOVE',(0,2),(-1,2),2,colors.green),
('GRID',(0,1),(-1,-1),2,colors.black),
]
)
table.setStyle(ts)
elems = []
# elems.append("TABLE TITLE")
elems.append(table)
doc.build(elems)
Upvotes: 1
Views: 2470
Reputation: 46
For this case, reportlab's documentation doesn't help how to achieve this. My answer is heavily based in this comment here. To create a Table inside a row you have to first create a new table and add them to your "original table". Bellow I will explain the steps:
from reportlab.platypus import SimpleDocTemplate, TableStyle, Table
# Creating a simple pdfdoc = SimpleDocTemplate(aa)
story = []
# Set a table style
table_style = TableStyle(
[
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('BOX', (0,0), (-1,-1), 0.25, colors.black),
]
)
# Creating mock data and cols to add a new table inside the third a row
data_table = [
['Col1', 'Col2'],
['aaa', 'bbb'],
[Table([['data to add as third row', 'a']], style=table_style)]
]
final_table = Table(data_table, style=table_style)
story.append(final_table)
doc.build(story)
Upvotes: 3