tob*_*den 4 python formatting reportlab
所以,有一段时间我一直在努力解决这个问题.我知道有很多类似的问题和很好的答案,我已经尝试过这些答案,但我的代码基本上反映了给出的答案.
我正在编写代码来自动生成工作表的匹配练习.所有这些信息都应该放在一张表格中.并且文本应该都与单元格的顶部对齐.
这就是我现在拥有的:
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import cm
document = []
doc = SimpleDocTemplate('example.pdf', pagesize=A4, rightMargin=72, leftMargin=72, topMargin=72)
styles = getSampleStyleSheet()
definitions = []
i, a = 1, 65
table = []
for x in range(1, 10):
line = []
line.append(Paragraph(str(i), styles['BodyText']))
line.append(Paragraph('Vocabulary', styles['BodyText']))
line.append(Paragraph(chr(a), styles['BodyText']))
line.append(Paragraph('Often a multi-line definition of the vocabulary. But then, sometimes something short and sweet.', styles['BodyText']))
table.append(line)
i += 1
a += 1
t = Table(table, colWidths=(1*cm, 4*cm, 1*cm, None))
t.setStyle(TableStyle([
('VALIGN', (1, 1), (-1, -1), 'TOP')
]))
document.append(t)
doc.build(document)
Run Code Online (Sandbox Code Playgroud)
我在俯瞰什么?
问题是你索引的方式TableStyle.Reportlab中的索引从(0, 0)第一行第一列开始.因此,在您的情况下,(1, 1)仅将样式应用于第一列和第一列右下方的所有内容.
正确的方法是使用:
('VALIGN', (0, 0), (-1, -1), 'TOP')
Run Code Online (Sandbox Code Playgroud)
这将将样式应用于中的所有单元格Table.
| 归档时间: |
|
| 查看次数: |
2275 次 |
| 最近记录: |