我想在 PDF 模板文件的方框内绘制表格。由于盒子的高度是固定的,如果表格的行数超过特定数量(例如4),表格就会从盒子中溢出。 有没有办法在使用reportlab包绘制表格时动态指定表格的高度?
def add_table(self, data: list[list[str]]):
t = Table(data)
t.setStyle(TableStyle([
("FACE", (0, 0), (-1, -1), "Times-Roman"),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
("VALIGN", (0, 0), (-1, -1), "BOTTOM"),
('FONTSIZE', (0,0), (-1,-1), 4),
('INNERGRID',(0,0),(-1,-1), 1, colors.lightgrey),
('OUTLINE', (0, 0), (-1, -1), 1, colors.lightgrey),
("LEFTPADDING", (0, 0), (-1, -1), 0),
("RIGHTPADDING", (0, 0), (-1, -1), 5),
("BOTTOMPADDING", (0, 0), (-1, -1), 0),
("TOPPADDING", (0, 0), (-1, -1), 0),
])
)
t.wrap(0, 0)
t.drawOn(self.c, 72, 200)
self.c.save()
我使用这段代码来解决我的问题。
COL_WIDTHS = [40, 50, 30, 40, 45, 40, 45, 40, 40, 40, 40, 45, 45, 40, 40, 40, 40]
TABLE_STYLE = [
('GRID', (0, 0), (-1, -1), 0.5, colors.lightgrey),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
("HALIGN", (0, 0), (-1, -1), "MIDDLE"),
("VALIGN", (0, 0), (-1, -1), "MIDDLE"),
('LEFTPADDING', (0, 0), (-1, -1), 0.5),
('RIGHTPADDING', (0, 0), (-1, -1), 0),
('TOPPADDING', (0, 0), (-1, -1), 0),
('BOTTOMPADDING', (0, 0), (-1, -1), 0),
('FONTNAME', (0, 0), (-1, 0), 'Times-Roman-Bold'),
('LEADING', (0, 0), (-1, -1), 8.2),
]
def get_styled_table(self, data: list[list[str]]) -> Table:
# My table has 17 columns and variable number of rows
t = Table(data, colWidths=COL_WIDTHS, rowHeights=[20] * len(data))
t.setStyle(TableStyle(TABLE_STYLE))
row_heights = 20
while True:
# Break loop if height of table is smaller than box size (e.g. 160)
if t.wrapOn(self.c, 730, 160)[1] < 160:
break
# Decrease rows height if height of table is bigger than box size
row_heights -= 0.5
# Define table with new row heights
t = Table(data, colWidths=COL_WIDTHS, rowHeights=[row_heights] * len(data))
# Set font size to accommodate cells content inside them
t.setStyle(TableStyle(TABLE_STYLE + [('FONTSIZE', (0,0), (-1,-1), 0.5 * row_heights)]))
return t
def add_table(self, data: list[list[str]]):
t = self.get_styled_table(data)
t.wrapOn(self.c, 730, 160)
t.drawOn(self.c, 43, 408)