reportlab line spacing and fitting of a Paragraph in a table
我在python 2.7中使用reportlab 3.1.44
这是在表中使用段落的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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() |
字体较大时有两个问题。
- 文本比单元格大,因此超出了边界
- 行之间的间距太小
我该如何解决这个问题?
这两个问题实际上是由同一问题引起的,即
在Reportlab中,根据文档使用
Interline spacing (Leading)
The vertical offset between the point at which one line starts and where the next starts is called the leading offset.
因此,正确的代码版本将使用:
1 | ps = ParagraphStyle('title', fontSize=20, leading=24) |
结果是: