我尝试用reportlab生成一个pdf表,但是我遇到了TableStyle的问题。
Valign和背景工作,但没有对齐和textcolor,我不明白为什么。
这是我的代码:
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename=filename.pdf'
doc = SimpleDocTemplate(response, pagesize=A4, rightMargin=30,leftMargin=30, topMargin=30,bottomMargin=18)
doc.pagesize = landscape(A4)
elements = []
data = [
["Equipe", "Partie\n1", "Partie2", "Partie3", "Partie4", "+", "-", "Total", "Parties gagnées", "Place"],
["Equipe", "Partie1", "Partie2", "Partie3", "Partie4", "+", "-", "Total", "Parties gagnées", "Place"],
["Equipe", "Partie1", "Partie2", "Partie3", "Partie4", "+", "-", "Total", "Parties gagnées", "Place"],
["Equipe", "Partie1", "Partie2", "Partie3", "Partie4", "+", "-", "Total", "Parties gagnées", "Place"],
]
#TODO: Get this line right instead of just copying it from the docs
style = TableStyle([
('ALIGN',(0,0),(-1,-1),'CENTER'),
('TEXTCOLOR',(2,2),(2,2),colors.red),
('VALIGN',(0, 0),(-1,-1),'MIDDLE'),
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('BOX', (0,0), (-1,-1), 0.25, colors.black),
('BACKGROUND', (2, 3), (2, 3), "#0088cc"),
('BACKGROUND', (0, 1), (-1, 1), lightgrey),
])
#Configure style and word wrap
s = getSampleStyleSheet()
s = s["BodyText"]
s.wordWrap = 'CJK'
data2 = [[Paragraph(cell, s) for cell in row] for row in data]
colwidths = [3.5*inch, .8*inch, .8*inch, .8*inch, .8*inch, .5*inch, .5*inch, .8*inch, 1*inch, .8*inch]
# Two rows with variable height
# rowheights = [1.4*inch, .2*inch]
t=Table(data2, colwidths)
t.setStyle(style)
#Send the data and build the file
elements.append(t)
doc.build(elements)
return response
命令' ALIGN'和' TEXTCOLOR'不工作,你能帮助我吗?
感谢。
答案 0 :(得分:0)
这是因为您已将每个Cell包装成段落。单元格右对齐,但单元格中的段落保持对齐。
如果您想要一个右对齐字段,请使用单元格而不是段落(单元格,s)
然后文本将不会换行。