报表选项卡中段落的行距和拟合

2024-09-28 17:20:14 发布

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

我在python 2.7中使用reportlab 3.1.44 这是使用表中段落的代码。

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import inch
from reportlab.lib.styles import ParagraphStyle
from reportlab.platypus.tables import Table, TableStyle
from reportlab.lib import colors
from reportlab.lib.colors import Color

styles = getSampleStyleSheet()


def make_report():
    doc = SimpleDocTemplate("hello.pdf")
    story = []
    style = styles["Normal"]
    ps = ParagraphStyle('title', fontSize=20)

    p1 = "here is some paragraph to see in large font"
    data = []
    table_row = [Paragraph(p1, ps),\
                 Paragraph(p1, ps)\
                ]


    data.append(table_row)
    t1 = Table(data)
    t1.setStyle(TableStyle([\
               ('GRID', (0,0), (-1,-1), 0.25, colors.red, None, (2,2,1)),\
             ]))
    story.append(t1)

    doc.build(story)

if __name__ == "__main__":
    make_report()

字体大时有两个问题。

  • 文本比单元格大,因此超出了边框
  • 行距太小

我怎样才能解决这个问题?


Tags: fromimportdatalibpst1colorsreportlab
1条回答
网友
1楼 · 发布于 2024-09-28 17:20:14

这两个问题实际上是由同一个问题引起的,即Paragraph的高度。表格单元格由决定行高的行距决定。同时空白的缺乏也造成了它的行距。

在Reportlab中,行距是根据文档使用leading样式属性设置的。

Interline spacing (Leading)

The vertical offset between the point at which one line starts and where the next starts is called the leading offset.

因此,正确的代码版本将使用:

ps = ParagraphStyle('title', fontSize=20, leading=24)

结果是: Example of the output after correction

相关问题 更多 >