循环遍历 python reportlab 中的表

发布于 2024-11-30 02:30:40 字数 1056 浏览 1 评论 0原文

我正在使用 python 模块 reportlab 创建一个表。在此表中,我想循环遍历并根据任何特定单元格的值使用不同的背景颜色。

为此,我提出了以下建议:

elements = []

table1 = [[34,27,35,35],
          [3,76,23,157],
          [13,137,15,75],
          [56,26,46,26]]




t1 = Table(table1)
for ii in range(len(table1)):
    for jj in range(len(table1)):
        if table1[ii][jj] <=50:
            ourcolor = colors.white
        elif table1[ii][jj] <=100:
            ourcolor = colors.skyblue
        elif table1[ii][jj] <=200:
            ourcolor = colors.green

        else:
            ourcolor = colors.white
        t1.setStyle(TableStyle([('BACKGROUND', (ii,jj), (ii,jj), ourcolor),
                                ('ALIGN', (0,0), (-1,-1), 'CENTER'),
                                ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                                ('BOX', (0,0), (-1,-1), 0.25, colors.black)
                                ]))

elements.append(t1)

但是,许多单元格仍然没有着色,其中许多单元格的颜色不正确,但其中一些单元格是正确的。我假设我的循环有问题,因为我不是一个非常有经验的程序员。

任何帮助或想法将不胜感激。

I am creating a table using the python module reportlab. In this table, I would like to loop through and have a different background color depending on the values of any particular cell.

To do this, I came up with the following:

elements = []

table1 = [[34,27,35,35],
          [3,76,23,157],
          [13,137,15,75],
          [56,26,46,26]]




t1 = Table(table1)
for ii in range(len(table1)):
    for jj in range(len(table1)):
        if table1[ii][jj] <=50:
            ourcolor = colors.white
        elif table1[ii][jj] <=100:
            ourcolor = colors.skyblue
        elif table1[ii][jj] <=200:
            ourcolor = colors.green

        else:
            ourcolor = colors.white
        t1.setStyle(TableStyle([('BACKGROUND', (ii,jj), (ii,jj), ourcolor),
                                ('ALIGN', (0,0), (-1,-1), 'CENTER'),
                                ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                                ('BOX', (0,0), (-1,-1), 0.25, colors.black)
                                ]))

elements.append(t1)

But, many of the cells still are not colored and many of them are colored incorrectly, however some of them are correct. I am assuming something is wrong with my loop since I am not a very experienced programmer.

Any help or ideas would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个ˇ局外人 2024-12-07 02:30:40

我对 ReportLab 的了解还不够,无法确定,但这种类型的编码中的一个常见问题是轴被交换。例如,像这样的索引:table1[ii][jj] 表示 ii 是 y 轴(行),jj 是 x 轴轴(列),因此您必须将 x 和 y 作为 jj, ii 提供给 ReportLab。检查输出在给单元格着色时是否交换了行和列。

另请注意,您的双循环在同一范围内循环两次,这仅在您的表格是正方形的情况下才起作用。如果您的表格甚至变得非方形,您的循环之一就会出现错误的范围。

I don't know enough about ReportLab to know for sure, but a common problem in this type of coding is that the axes are swapped. For example, indexing like this: table1[ii][jj] means that ii is the y axis (rows) and jj is the x axis (columns), so you'd have to supply x and y to ReportLab as jj, ii. Check if your output has rows and columns swapped when coloring cells.

Also, note that your double loop is looping over the same range twice, which works only because your table is square. If your table even becomes non-square, you'll have the wrong range on one of your loops.

请远离我 2024-12-07 02:30:40

看起来 table1 只是一个列表列表。我认为这段代码会做你想要的事情,假设问题出在你的循环中而不是在reportlab模块中(我对此没有经验)。

a=-1
for ii in table1:
  a = a+1
  b = -1 
  for jj in ii:
    b = b+1
    if jj <=50:
        ourcolor = colors.white
    elif jj <=100:
        ourcolor = colors.skyblue
    elif jj <=200:
        ourcolor = colors.green
    else:
        ourcolor = colors.white
    t1.setStyle(TableStyle([('BACKGROUND', (a,b), (a,b), ourcolor),
                              ('ALIGN', (0,0), (-1,-1), 'CENTER'),
                              ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                              ('BOX', (0,0), (-1,-1), 0.25, colors.black)
                              ]))

It looks like table1 is just a list of lists. I think this code will do what you want it to, assuming the problem lies in your loop and not in the reportlab module (I am not experienced with that).

a=-1
for ii in table1:
  a = a+1
  b = -1 
  for jj in ii:
    b = b+1
    if jj <=50:
        ourcolor = colors.white
    elif jj <=100:
        ourcolor = colors.skyblue
    elif jj <=200:
        ourcolor = colors.green
    else:
        ourcolor = colors.white
    t1.setStyle(TableStyle([('BACKGROUND', (a,b), (a,b), ourcolor),
                              ('ALIGN', (0,0), (-1,-1), 'CENTER'),
                              ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                              ('BOX', (0,0), (-1,-1), 0.25, colors.black)
                              ]))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文