Python ReportLab 使用 splitfirst/splitlast
我正在尝试将 Python 与 ReportLab 2.2 结合使用来创建 PDF 报告。
根据用户指南,
特殊表格样式索引[原文如此]
在任何样式命令中,第一行索引可以设置为特殊字符串“splitlast”或“splitfirst”之一,以指示该样式应仅用于拆分表的最后一行,或拆分表的第一行。一个延续。 这允许分割表,并在分割周围产生更好的效果。
我尝试过使用几种样式元素,包括:
('TEXTCOLOR', (0, 'splitfirst'), (1, 'splitfirst'), colors.black)
('TEXTCOLOR', (0, 'splitfirst'), (1, 0), colors.black)
('TEXTCOLOR', (0, 'splitfirst'), (1, -1), colors.black)
但这些似乎都不起作用。 第一个生成带有消息的 TypeError:
TypeError: cannot concatenate 'str' and 'int' objects
后两个生成带有消息的 TypeError:
TypeError: an integer is required
这个功能只是被破坏了还是我做错了什么? 如果是后者,我做错了什么?
I'm trying to use Python with ReportLab 2.2 to create a PDF report.
According to the user guide,
Special TableStyle Indeces [sic]
In any style command the first row index may be set to one of the special strings 'splitlast' or 'splitfirst' to indicate that the style should be used only for the last row of a split table, or the first row of a continuation. This allows splitting tables with nicer effects around the split.
I've tried using several style elements, including:
('TEXTCOLOR', (0, 'splitfirst'), (1, 'splitfirst'), colors.black)
('TEXTCOLOR', (0, 'splitfirst'), (1, 0), colors.black)
('TEXTCOLOR', (0, 'splitfirst'), (1, -1), colors.black)
and none of these seems to work. The first generates a TypeError with the message:
TypeError: cannot concatenate 'str' and 'int' objects
and the latter two generate TypeErrors with the message:
TypeError: an integer is required
Is this functionality simply broken or am I doing something wrong? If the latter, what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,看来我要回答我自己的问题了。
首先,文档明确指出“在任何样式命令中,第一行索引可以设置为特殊字符串“splitlast”或“splitfirst”之一,以指示该样式应仅用于最后一行拆分表,或延续的第一行。” 在当前版本中,“splitlast”和“splitfirst”行索引因前面提到的 TEXTCOLOR 和 BACKGROUND 命令上的 TypeError 而中断。
根据阅读源代码,我怀疑目前只有 tablestyle 行命令(GRID、BOX、LINEABOVE 和 LINEBELOW)与“splitfirst”和“splitlast”行索引兼容。 我怀疑所有单元格命令都会因上述类型错误而中断。
但是,我可以通过子类化 Table 类并重写 onSplit 方法来完成我想要的操作。 这是我的代码:
它的作用是将文本颜色黑色应用于每页第二行的第一个和第二个单元格。 (第一行是标题,由表的 RepeatRows 参数重复。)更准确地说,它对每个框架的第一个和第二个单元格执行此操作,但由于我使用的是 SimpleDocTemplate,因此框架和页面是相同的。
Well, it looks as if I will be answering my own question.
First, the documentation flat out lies where it reads "In any style command the first row index may be set to one of the special strings 'splitlast' or 'splitfirst' to indicate that the style should be used only for the last row of a split table, or the first row of a continuation." In the current release, the "splitlast" and "splitfirst" row indices break with the aforementioned TypeErrors on the TEXTCOLOR and BACKGROUND commnds.
My suspicion, based on reading the source code, is that only the tablestyle line commands (GRID, BOX, LINEABOVE, and LINEBELOW) are currently compatible with the 'splitfirst' and 'splitlast' row indices. I suspect that all cell commands break with the aforementioned TypeErrors.
However, I was able to do what I wanted by subclassing the Table class and overriding the onSplit method. Here is my code:
What this does is apply the text color black to the first and second cell of the second row of each page. (The first row is a header, repeated by the repeatRows parameter of the Table.) More precisely, it is doing this to the first and second cell of each frame, but since I am using the SimpleDocTemplate, frames and pages are identical.
这似乎是 ReportLab Table 类中的一个错误。 除了 DLJessup 自己的答案之外,另一个修复方法是修改
Table 中导致错误的 ReportLab 代码._drawBkgrnd()
,大约第 1301 行。对于 'splitlast',将: 更改为:
这可以在您自己的代码中轻松完成,无需通过子类化 Table 并覆盖此方法来破解 ReportLab。 我不需要使用“splitfirst”; 如果我这样做,我将在这里发布其余的黑客攻击。
This seems to be a bug in the ReportLab Table class. Another fix for this in addition to DLJessup's own answer is to modify the ReportLab code that's causing the error, in
Table._drawBkgrnd()
, around line 1301. For 'splitlast', change:to:
This is easily done in your own code without hacking ReportLab by subclassing Table and overwriting this method. I've not had need to use 'splitfirst'; if I do I'll post the rest of the hack here.
在第一个示例中,您还将第二 行索引设置为特殊字符串。
不知道为什么其他两个不起作用...您确定这是异常的来源吗?
In your first example you're setting the second row index to a special string as well.
Not sure why the other two don't work... Are you sure this is where the exception is coming from?