Reportlab:reportlab中表列的动态颜色(存储在django模型中的颜色)
我有名为 TestResults
models.py 的
Class TestResults(models)
chemical_name charfield
value floatfield
unit charfield
method charfield
normal_limit charfield
caution_limit charfield
color charfield
django 模型现在,
下面的代码将生成油表,其中包含以下字段。
views.py
fields = ('Test Name', 'Value', 'Unit', 'Method',
'Normal Limit', 'Caution Limit')
all_oils = [(test.chemical_name, test.value, test.unit, test.method,
test.normal_limit, test.caution_limit)
for test in TestResult.objects.all())]
oil_table = Table([fields] + all_oils
oil_table.setStyle(TableStyle([('BACKGROUND', (0, 0), (-1, 0), '#a7a5a5'),
('FONTSIZE', (0, 0), (-1, 0), 6),
('GRID', (0, 0), (-1, -1), 2, '#a7a5a5'),
('FONTSIZE', (0, 0), (-1, -1), 8),
('FONTNAME',(1,1),(1,-1),'Times-Bold')]))
现在我如何为每列提供动态颜色。假设我的 TestResult 模型具有颜色字段(I,e color=red),
在报告中我想要来自模型对象的第二列的动态颜色
如何实现?
I have django model called TestResults
models.py
Class TestResults(models)
chemical_name charfield
value floatfield
unit charfield
method charfield
normal_limit charfield
caution_limit charfield
color charfield
Now,
The below code will generate the table for oils, which has following fields.
views.py
fields = ('Test Name', 'Value', 'Unit', 'Method',
'Normal Limit', 'Caution Limit')
all_oils = [(test.chemical_name, test.value, test.unit, test.method,
test.normal_limit, test.caution_limit)
for test in TestResult.objects.all())]
oil_table = Table([fields] + all_oils
oil_table.setStyle(TableStyle([('BACKGROUND', (0, 0), (-1, 0), '#a7a5a5'),
('FONTSIZE', (0, 0), (-1, 0), 6),
('GRID', (0, 0), (-1, -1), 2, '#a7a5a5'),
('FONTSIZE', (0, 0), (-1, -1), 8),
('FONTNAME',(1,1),(1,-1),'Times-Bold')]))
Now how can I provide dynamic color for each column. Suppose my TestResult model is having field for color (I,e color=red)
In report i want dynamic color for second column which comes from model objects
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

也许我错过了一些东西,但这看起来很简单。
在您的对象中,将所需的颜色存储在
color
等变量中作为有效的 ReportLab 颜色规范(例如,十六进制颜色值的字符串)。然后,当您构建报告时,如果您想设置背景颜色,只需为列中的所有单元格设置背景颜色即可。例如,要设置x
列的背景,请将以下内容添加到TableStyle
中:您可以放入尽可能多的
'BACKGROUND'
样式您想要在TableStyle
中使用,因此您可以对要更改颜色的每一列执行此操作。Maybe I'm missing something, but this seems really easy.
In your objects, store the color you want in a variable like
color
as a valid ReportLab color specification (e.g. string of hex color value). Then when you build your report, if you want to set the background color, just set it for all the cells in the column. For example, to set the background of columnx
, add the following to yourTableStyle
:You can put in as many
'BACKGROUND'
styles as you want in aTableStyle
, so you can do this for each column you want to change the color of.