Python Reportlab表

Python Reportlab表,python,reportlab,Python,Reportlab,我正在尝试使用Reportlab(通过一个表)构建一个图例。该表应该有三行两列,每个i,j元素是一个带有彩色项目符号的ListItem。代码如下: ptext = '<font size=10><b><i>Legend:</i></b></font>' light = [] mild = [] strong = [] #Create the table ptext = '&l

我正在尝试使用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)
    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)
ptext='图例:'
灯光=[]
轻度=[]
strong=[]
#创建表
ptext=‘光屏障’
light.append(ListFlowable(ListItem)(段落(ptext,styles[“Normal”]),
bulletColor=浅红色,值='circle'),bulletType='bullet',start='circle'))
ptext=‘光效益’
light.append(ListFlowable(ListItem)(段落(ptext,styles[“Normal”]),
bulletColor=浅绿色,值='circle'),bulletType='bullet',start='circle'))
ptext=‘轻度屏障’
轻度.append(ListFlowable(ListItem)(段落(ptext,样式[“正常])),
bulletColor=mildRed,value='circle',bulletType='bullet',start='circle'))
ptext=‘轻度效益’
轻度.append(ListFlowable(ListItem)(段落(ptext,样式[“正常])),
bulletColor=mildGreen,value='circle'),bulletType='bullet',start='circle'))
ptext=‘强势垒’
strong.append(ListFlowable(ListItem)(段落(ptext,styles[“Normal”]),
bulletColor=strongRed,值='circle'),bulletType='bullet',start='circle'))
ptext=‘强劲收益’
strong.append(ListFlowable(ListItem)(段落(ptext,styles[“Normal”]),
bulletColor=strongGreen,值='circle'),bulletType='bullet',start='circle'))
#将它们附加到表中
数据=[轻度、轻度、重度]
t=表(数据)
故事。附加(t)
我得到的错误是:

“ListItem不可编辑”


非常感谢您的帮助。

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

    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)
    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)
a11=ListFlowable([ListItem(段落('Light Barrier',样式[“Normal”]),
bulletColor=浅红色,值='circle')],bulletType='bullet',start='circle')
a12=列表可流动([ListItem(段落('Light Benefit',styles[“Normal”]),
bulletColor=浅绿色,值='circle')],bulletType='bullet',start='circle')
a21=列表可流动([ListItem(段落('轻度障碍'),样式[“正常]),
bulletColor=mildRed,value='circle')],bulletType='bullet',start='circle')
a22=列表可流动([ListItem(段落('轻度受益'),样式[“正常]),
bulletColor=mildGreen,value='circle')],bulletType='bullet',start='circle')
a31=可列表流动([ListItem(段落('Strong Barrier',样式[“Normal”])),
bulletColor=strongRed,值='circle')],bulletType='bullet',start='circle')
a32=列表可流动([ListItem(段落('Strong Benefit',styles[“Normal”]),
bulletColor=strongGreen,值='circle')],bulletType='bullet',start='circle')
数据=[[a11,a12,],[a21,a22,],[a31,a32,],]
t=表格(数据,线宽=[1.3*英寸]*2,行高=[0.17*英寸]*3,hAlign='RIGHT')
故事。附加(t)