简体   繁体   中英

Reportlab Table

I'm trying to build a legend (through a Table) with Reportlab. The table should have three lines and two columns, each i, j element being a ListItem with a colored bullet point. Here is the code:

    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)

The error I get says:

"ListItem is not iterable".

Any help is appreciated.

In the end, I figured out a solution, which I post below. Please note that the posted solution answers to the question: how can I make a Reportlab table where the entries (a_ij) are ListItems? The answer is that you need to enclose ListItems in a ListFlowable, and ListFlowables in a nested Python list. Here is the code:

    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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM