什么决定了Reportlab表中的垂直空间?

2024-05-15 17:57:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在文档中定义此样式:

styles.add(ParagraphStyle(name='Table Header', font ='Helvetica-Bold',fontSize=16, alignment=TA_CENTER))

我用这个定义段落,让文本进入每个表的最上面一行(以便正确换行):

L2sub = [(Paragraph(L[0][0], styles['Table Header']))]

稍后,当我添加表时,还有一个地方可以定义样式:

report.append(Table(data,style=[
                ('GRID',(0,0),(len(topiclist)-1,-1),0.5,colors.grey),
                ('FONT', (0,0),(len(topiclist)-1,0),'Helvetica-Bold',16),
                ('FONT', (0,1),(len(topiclist)-1,1),'Helvetica-Bold',12),
                ('ALIGN',(0,0),(-1,-1),'CENTER'),
                ('VALIGN',(0,0),(-1,-1),'MIDDLE'),
                ('SPAN',(0,0),(len(topiclist)-1,0)),
                ]))

我的问题是:定义第一行单元格垂直高度的设置在哪里?我有一些问题,文本对单元格来说太大和/或在单元格中设置得太低,但我无法确定是什么导致了它,或者如何修复它。我已经改变了两种尺寸,但我不能让细胞的高度都一样。当我只是将文本放入单元格而不是段落中时,表定义工作得很好,但段落导致了问题。


Tags: 文档文本len高度定义table样式header
2条回答

(没有足够的声誉来评论另一个答案)

关于最后一个快捷方式,“ROW_HEIGHT=5*mm”就可以了。不需要按表中的行数乘以行高。

ROW_HEIGHT = 5 * mm
curr_table = Table(data, COL_WIDTHS, rowHeights=ROW_HEIGH )

节省一点内存。:)

我不相信TableStyle中有允许您更改行高的设置。当您创建一个新的Table对象时,会给出该度量值:

Table(data, colwidths, rowheights)

其中colwidthsrowheights是测量值列表,如下所示:

from reportlab.lib.units import inch
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph
from reportlab.platypus import Table
from reportlab.lib import colors

# Creates a table with 2 columns, variable width
colwidths = [2.5*inch, .8*inch]

# Two rows with variable height
rowheights = [.4*inch, .2*inch]

table_style = [
    ('GRID', (0, 1), (-1, -1), 1, colors.black),
    ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
    ('ALIGN', (1, 1), (1, -1), 'RIGHT')
]

style = getSampleStyleSheet()

title_paragraph = Paragraph(
    "<font size=13><b>My Title Here</b></font>",
    style["Normal"]
)
# Just filling in the first row
data = [[title_paragraph, 'Random text string']]

# Now we can create the table with our data, and column/row measurements
table = Table(data, colwidths, rowheights)

# Another way of setting table style, using the setStyle method.
table.setStyle(tbl_style)

report.append(table)

colwidthsrowheights可以更改为适合内容所需的任何度量。colwidths从左到右读取,rowheights从上到下读取。

如果知道所有表行的高度都将相同,可以使用以下快捷方式:

rowheights = [.2*inch] * len(data)

它为data变量中的每一行提供一个类似[.2*inch, .2*inch, ...]的列表。

相关问题 更多 >