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

问题描述 投票:9回答:2

我在文档中定义了这种风格:

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)),
                ]))

我的问题是:定义第一行单元格垂直高度的设置在哪里?我有一些问题,文本对于单元格来说太大和/或在单元格中设置得太低,但我无法确定导致它的原因或修复它的方法。我已经改变了两种尺寸,但我不能让细胞成为除了相同高度之外的任何东西。当我只是将文本放入单元格而不是段落时,表格def'n工作得很好,但段落引起了问题。

python reportlab
2个回答
6
投票

我不相信TableStyle中有一个允许你改变rowheight的设置。当您创建新的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)

这为你的[.2*inch, .2*inch, ...]变量中的每一行提供了一个像data这样的列表。


4
投票

(没有足够的声誉来评论其他答案)

关于最后一个快捷方式,只需“ROW_HEIGHT = 5 * mm”即可。无需乘以表中行数的行高。

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

节省一点内存。 :)

© www.soinside.com 2019 - 2024. All rights reserved.