Reportlab表

时间:2018-08-30 16:45:19

标签: python reportlab

我正在尝试使用Reportlab构建图例(通过表)。该表应具有三行和两列,每个i,j元素是带有彩色项目符号点的ListItem。这是代码:

    ptext = '<font size=10><b><i>Legend:</i></b></font>'
    light = []
    mild = []
    strong = []

    #Create the table
    ptext = '<font size=10>Light Barrier</font>'
    light.append(ListFlowable(ListItem(Paragraph(ptext, styles["Normal"]), 
                               bulletColor = lightRed, value = 'circle'), bulletType = 'bullet', start = 'circle'))
    ptext = '<font size=10>Light Benefit</font>'
    light.append(ListFlowable(ListItem(Paragraph(ptext, styles["Normal"]), 
                               bulletColor = lightGreen, value = 'circle'), bulletType = 'bullet', start = 'circle'))
    ptext = '<font size=10>Mild Barrier</font>'
    mild.append(ListFlowable(ListItem(Paragraph(ptext, styles["Normal"]), 
                               bulletColor = mildRed, value = 'circle'), bulletType = 'bullet', start = 'circle'))
    ptext = '<font size=10>Mild Benefit</font>'
    mild.append(ListFlowable(ListItem(Paragraph(ptext, styles["Normal"]), 
                               bulletColor = mildGreen, value = 'circle'), bulletType = 'bullet', start = 'circle'))
    ptext = '<font size=10>Strong Barrier</font>'
    strong.append(ListFlowable(ListItem(Paragraph(ptext, styles["Normal"]), 
                               bulletColor = strongRed, value = 'circle'), bulletType = 'bullet', start = 'circle'))
    ptext = '<font size=10>Strong Benefit</font>'
    strong.append(ListFlowable(ListItem(Paragraph(ptext, styles["Normal"]), 
                               bulletColor = strongGreen, value = 'circle'), bulletType = 'bullet', start = 'circle'))

    #Append them to the table
    data = [light, mild, strong]
    t = Table(data)
    Story.append(t)

我得到的错误是:

  

“ ListItem不可迭代”。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

最后,我想出了一个解决方案,将其发布在下面。请注意,发布的解决方案回答了以下问题:我如何制作其中条目(a_ij)为ListItems的Reportlab表?答案是您需要将ListItems封装在ListFlowable中,并将ListFlowables封装在嵌套的Python列表中。这是代码:

    a11 = ListFlowable([ListItem(Paragraph('<font size=10>Light Barrier</font>', styles["Normal"]), 
                               bulletColor = lightRed, value = 'circle')], bulletType = 'bullet', start = 'circle')
    a12 = ListFlowable([ListItem(Paragraph('<font size=10>Light Benefit</font>', styles["Normal"]), 
                               bulletColor = lightGreen, value = 'circle')], bulletType = 'bullet', start = 'circle')
    a21 = ListFlowable([ListItem(Paragraph('<font size=10>Mild Barrier</font>', styles["Normal"]), 
                               bulletColor = mildRed, value = 'circle')], bulletType = 'bullet', start = 'circle')
    a22 = ListFlowable([ListItem(Paragraph('<font size=10>Mild Benefit</font>', styles["Normal"]), 
                               bulletColor = mildGreen, value = 'circle')], bulletType = 'bullet', start = 'circle')
    a31 = ListFlowable([ListItem(Paragraph('<font size=10>Strong Barrier</font>', styles["Normal"]), 
                               bulletColor = strongRed, value = 'circle')], bulletType = 'bullet', start = 'circle')
    a32 = ListFlowable([ListItem(Paragraph('<font size=10>Strong Benefit</font>', styles["Normal"]), 
                               bulletColor = strongGreen, value = 'circle')], bulletType = 'bullet', start = 'circle')

    data = [[a11, a12,], [a21, a22,], [a31, a32,],]
    t = Table(data, colWidths=[1.3*inch]*2, rowHeights=[0.17*inch]*3, hAlign='RIGHT')
    Story.append(t)