循环遍历 python reportlab 中的表
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 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 thatii
is the y axis (rows) andjj
is the x axis (columns), so you'd have to supply x and y to ReportLab asjj, 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.
看起来 table1 只是一个列表列表。我认为这段代码会做你想要的事情,假设问题出在你的循环中而不是在reportlab模块中(我对此没有经验)。
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).