reportlab设置左表位置

问题描述:

如何设置表格的左侧位置?reportlab设置左表位置

response = HttpResponse(mimetype='application/pdf') 
response['Content-Disposition'] = 'attachment; filename=%s' % pdf_name 
buffer = StringIO() 
PAGESIZE = pagesizes.portrait(pagesizes.A4) 
doc = SimpleDocTemplate(buffer, pagesize=PAGESIZE, leftMargin=1*cm) 
story = [] 

story.append(Paragraph(header_part2, styleN)) 
table_row = ['Total Score:',''] 
data.append(table_row) 
ts = [ 
    #header style  
    ('LINEABOVE', (0,0), (-1,0), 1, colors.gray), 
    ('LINEBELOW', (0,0), (-1,0), 1, colors.gray)] 
t = Table(data, (6*cm,6*cm), None, style=ts) 
    story.append(t) 
    doc.build(story) 
    pdf = buffer.getvalue() 
buffer.close() 
response.write(pdf) 

尽管段落从左侧打印1厘米,但打印的表格几乎没有距左页边框的距离。

我必须在哪里设置表格位置的leftMargin?

这同样适用于我添加的图像。他们似乎打印在某个地方。

story.append(Image(path,35,10)) 

发现神奇hAlign关键字:

t = Table(data, (6*cm,6*cm,2*cm,2*cm,2*cm), None, style=ts, hAlign='LEFT') 

我想补充一点,你也可以设置在TABLESTYLE排列,就像你设置lineabovelinebelow

虽然这可能不是在本身有价值的信息,但要知道,水平对齐方式设置与关键字“ALIGN”,而不是很重要,“HALIGN”(如你轻松假设给定会是垂直对齐方式设置与'VALIGN'和上面的解决方案在函数调用中也有hAlign)。我疯了,试图整天与'HALIGN'对齐。

以下是您可以测试水平对齐('ALIGN')的代码示例。

from reportlab.platypus import SimpleDocTemplate 
from reportlab.platypus.tables import Table, TableStyle 
from reportlab.lib import colors 

doc = SimpleDocTemplate('align.pdf', showBoundary=1) 

t = Table((('', 'Team A', 'Team B', 'Team C', 'Team D'), 
    ('Quarter 1', 100, 200, 300, 400), 
    ('Quarter 2', 200, 400, 600, 800), 
    ('Total', 300, 600, 900, 1200)), 
    (72, 45, 45, 45, 45), 
    (25, 15, 15, 15) 
) 

t.setStyle(TableStyle([ 
    ('ALIGN', (0, 0), (-1, -1), 'RIGHT'), 
    ('GRID', (0, 0), (-1, -1), 0.25, colors.red, None, (2, 2, 1)), 
    ('BOX', (0, 0), (-1, -1), 0.25, colors.blue), 
    ])) 

story = [t] 
doc.build(story) 

Resulting table in pdf