Python ReportLab 使用 splitfirst/splitlast [英] Python ReportLab use of splitfirst/splitlast

查看:47
本文介绍了Python ReportLab 使用 splitfirst/splitlast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Python 与 ReportLab 2.2 结合使用来创建 PDF 报告.
根据用户指南

I'm trying to use Python with ReportLab 2.2 to create a PDF report.
According to the user guide,

特殊的 TableStyle Indeces [原文]

Special TableStyle Indeces [sic]

在任何样式命令中,第一行索引可以设置为特殊字符串 'splitlast' 或 'splitfirst' 之一,以指示样式应仅用于拆分表的最后一行,或一个延续.这允许拆分表格,并在拆分周围产生更好的效果.

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) 

这些似乎都不起作用.第一个生成带有消息的 TypeError:

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?

推荐答案

好吧,看来我要回答我自己的问题了.

Well, it looks as if I will be answering my own question.

首先,文档清楚地表明在任何样式命令中,第一行索引可以设置为特殊字符串 'splitlast' 或 'splitfirst' 之一,以指示该样式应仅用于最后一个拆分表的行,或延续的第一行."在当前版本中,splitlast"和splitfirst"行索引与前面提到的 TEXTCOLOR 和背景命令上的 TypeError 相冲突.

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.

基于阅读源代码,我怀疑目前只有 tablestyle 行命令(GRID、BOX、LINEABOVE 和 LINEBELOW)与 'splitfirst' 和 'splitlast' 行索引兼容.我怀疑所有单元格命令都会因上述类型错误而中断.

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.

但是,我可以通过子类化 Table 类并覆盖 onSplit 方法来做我想做的事情.这是我的代码:

However, I was able to do what I wanted by subclassing the Table class and overriding the onSplit method. Here is my code:

class XTable(Table):
    def onSplit(self, T, byRow=1):
        T.setStyle(TableStyle([
          ('TEXTCOLOR', (0, 1), (1, 1), colors.black)]))

这样做是将文本颜色应用到每页第二行的第一个和第二个单元格.(第一行是标题,由表的 repeatRows 参数重复.)更准确地说,它对每个框架的第一个和第二个单元格执行此操作,但由于我使用的是 SimpleDocTemplate,因此框架和页面是相同的.

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.

这篇关于Python ReportLab 使用 splitfirst/splitlast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆