【发布时间】:2021-12-24 09:49:41
【问题描述】:
我需要在表格单元格内创建一个表格,这是数据中的描述字段,它本身就是一个列表。我需要从该列表中制作一个表格。 下面是我目前在reportlab中用来创建普通表的代码,我只需要在该表的描述字段中的表内插入一个表,描述字段本身就是数据列表中的一个列表。
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)
【问题讨论】:
-
请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。