我的第一个想法是使用Paragraph
对齐左边的文字,然后使用TableStyles
在列/单元格的右侧显示该段落,但TableStyle([('ALIGN', (1, 0), (-1, -1), 'RIGHT')]))
似乎不合适有任何影响。在我的示例代码中,我需要RED框中的文本为左对齐文本(如绿色框中的文本),但“卡在”红色框的右侧。
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import (SimpleDocTemplate, Paragraph, Table, TableStyle, Spacer)
from reportlab.lib import colors
from reportlab.lib.enums import TA_RIGHT, TA_CENTER
doc = SimpleDocTemplate("//cglvf01/tec/tmp/test/old/simple_table.pdf", pagesize=letter)
elements = []
styles = getSampleStyleSheet()
style_right = ParagraphStyle(name='right', parent=styles[
'Normal'], alignment=TA_RIGHT)
style_center = ParagraphStyle(name='right', parent=styles[
'Normal'], alignment=TA_CENTER)
zero1 = """<font size="14">
<b>header</b><br/></font><font size="12">
line1 <br/>
line2 <br/>
line3 </font><br/><br/>
"""
zero2 = """<font size="14">
<b>header</b><br/></font><font size="12">
line1 <br/>
line2 <br/>
line3 </font><br/><br/>
"""
p1 = Paragraph(zero1,styles['Normal'])
p2 = Paragraph(zero2, style_center)
data= [[p1,p2]]
t=Table(data)
t.setStyle(TableStyle([('TEXTCOLOR',(0,0),(1,-1),colors.red),
('BOX', (0, 0), (-2, -1), 0.5, colors.green),
('BOX', (1, 0), (-1, -1), 0.5, colors.red),
('ALIGN', (1, 0), (-1, -1), 'RIGHT'),
]))
elements.append(t)
elements.append(Spacer(1, 12))
p1 = Paragraph(zero1,styles['Normal'])
p2 = Paragraph(zero2, style_right)
data2= [[p1,p2]]
t2=Table(data2)
t2.setStyle(TableStyle([('TEXTCOLOR',(0,0),(1,-1),colors.red),
('BOX', (0, 0), (-2, -1), 0.5, colors.green),
('BOX', (1, 0), (-1, -1), 0.5, colors.red),
('VALIGN', (1, 0), (-1, -1), 'TOP'),
]))
elements.append(t2)
# write the document to disk
doc.build(elements)
print("fin")