Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 什么决定了Reportlab表中的垂直空间?_Python_Reportlab - Fatal编程技术网

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

Python 什么决定了Reportlab表中的垂直空间?,python,reportlab,Python,Reportlab,我在文档中定义此样式: styles.add(ParagraphStyle(name='Table Header', font ='Helvetica-Bold',fontSize=16, alignment=TA_CENTER)) 我使用它来定义段落,以便文本进入每个表的顶行(以便正确包装): 稍后,当我添加表时,还有一个地方可以定义样式: report.append(Table(data,style=[ ('GRID',(0,0),(len(topiclis

我在文档中定义此样式:

styles.add(ParagraphStyle(name='Table Header', font ='Helvetica-Bold',fontSize=16, alignment=TA_CENTER))
我使用它来定义段落,以便文本进入每个表的顶行(以便正确包装):

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

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工作得很好,但段落导致了问题。

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

Table(data, colwidths, rowheights)
其中,
colwidths
rowheights
是测量值列表,如下所示:

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)
这将为
数据中的每一行提供一个类似
[.2*英寸,.2*英寸,…]
的列表。

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

关于最后一个快捷方式,简单地说“ROW_HEIGHT=5*mm”就行了。无需将表中每行的行高乘以行数

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

节省一点内存。:)

我的理解是,如果不使用段落,并将文本直接放在表格单元格中,则可以自动进行换行。段落有自己的包装逻辑,需要一个固定的工作空间。那你为什么要使用段落“以便它们正确地包装”?这不是我的经验:我有一些表格越过了页面的边界,因为它们没有包装,直到我将文本放入段落中。我是从另一个问题中得到这个想法的,很好。有代表性
rowheights = [.2*inch] * len(data)
ROW_HEIGHT = 5 * mm
curr_table = Table(data, COL_WIDTHS, rowHeights=ROW_HEIGH )