ReportLab 行具有不同的列

问题描述 投票:0回答:1

我正在将数据写入 pdf 表格中。它看起来像这样:

[[1,2,3,4], [1,2], [1,2,3,4], [1,2,3]]
——如您所见,行有不同的长度,范围从 2 到 4。

我使用此代码来设置行的样式:

table.setStyle(TableStyle([
    ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
    ('BOX', (0,0), (-1,-1), 0.25, colors.grey),
]))

然而,这会为每行生成 4 列。如何确保我只获得与我的行长度对应的列数?

python pdf reportlab
1个回答
0
投票

仅显式绘制您需要的边框。在文档中查找“TableStyle Line Commands”。

编辑:

假设您的数据列表如下所示:

data = [[0, 1, 2, 3],
        [0, 1],
        [0, 1, 2, 3],
        [0, 1, 2]]

那么也许这样的事情会起作用:

>>> lineweight = 0.25
>>> gridlines = [('GRID', (i, 0), (i, len(j)), lineweight, colors.grey) for i, j in enumerate(data)]
>>> gridlines
[('GRID', (0, 0), (0, 4), 0.25, colors.grey),
 ('GRID', (1, 0), (1, 2), 0.25, colors.grey),
 ('GRID', (2, 0), (2, 4), 0.25, colors.grey),
 ('GRID', (3, 0), (3, 3), 0.25, colors.grey)]

希望有帮助。

编辑2:添加

lineweight

© www.soinside.com 2019 - 2024. All rights reserved.