Reportlab的网格看起来完全不对

时间:2019-02-28 14:07:14

标签: python reportlab

看来我的表格内容和网格行为不正常,即网格未按预期方式装箱数据。

这是我的代码:

    story = []

    data = [['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6'],
            ['0.2', '-0.1', '0', '0', '-0.5', '0.6']]

    colwidths = (50, 50, 50, 50, 50, 50)
    rowheights = (10, 10)

    t = Table(data, colwidths, rowheights)

    GRID_STYLE = TableStyle(
        [('FONTSIZE', (0, 0), (-1, -1), 5),
         ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
         ('ALIGN', (1, 1), (-1, -1), 'RIGHT')]
    )

    t.setStyle(GRID_STYLE)
    story.append(t)

    doc = SimpleDocTemplate('mydoc.pdf', pagesize=landscape(letter), topMargin=50)
    doc.build(story)

这是我得到的pdf输出:

enter image description here

有人知道我在这里俯瞰什么吗?

1 个答案:

答案 0 :(得分:1)

根据我的经验,除了从第1列第1行(而不是第0列和第0行(从0开始))向右对齐的Align(1,1),(-1,-1)开始指定TableStyle时,所有这些都是冗长的。如果不这样做,则样式本身将分配默认值(例如,左右填充),而这通常不是您想要的。因此,要完全控制,请尝试为所有类别分配一个值,并且不要遗漏任何相关类别。

例如我觉得下面看起来更好

story = []

data = [['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6'],
        ['0.2', '-0.1', '0', '0', '-0.5', '0.6']]

colwidths = (50)
rowheights = (10)

t = Table(data, colwidths, rowheights)

GRID_STYLE = TableStyle(
    [('FONTSIZE', (0, 0), (-1, -1), 5),
     ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
     ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
     ('LEFTPADDING', (0, 0), (-1, -1), 0),
     ('RIGHTPADDING', (0, 0), (-1, -1), 0),
     ('TOPPADDING', (0, 0), (-1, -1), 0),
     ('BOTTOMPADDING', (0, 0), (-1, -1), 0),
     ('FONTNAME', (0, 0), (-1, -1), 'Helvetica'),
     ('SIZE', (0, 0), (-1, -1), 7),
     ('LEADING', (0, 0), (-1, -1), 8.2),
     ]
)

t.setStyle(GRID_STYLE)
story.append(t)

doc = SimpleDocTemplate('mydoc.pdf', pagesize=landscape(letter), topMargin=50)
doc.build(story)

enter image description here