reportlab常用函數及參數解釋
fromreportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
fromreportlab.lib.styles import getSampleStyleSheet
fromreportlab.rl_config import defaultPageSize
fromreportlab.lib.units import inch
c.drawString(x,y,"Hello World"), x,y為在笛卡爾坐標系下的位置信息,坐標軸原點位於頁面的左下角。
c.translate(inch,inch) 將坐標原點指定為左上角(而不是左下角)
Flowable對象定義的為“漂浮的元素”,元素的精確位置是由它前面的那些元素決定的,如paragraph, diagram.
non-flowables包括(page numbering annotations)頁碼註釋,headers,footers,fixed diagrams, logos等。
canvas.line(x1,y1,x2,y2)
canvas.lines(linelist)
#繪制字符串的方法
canvas.drawString(x, y, text):
canvas.drawRightString(x, y, text)
canvas.drawCentredString(x, y, text)
這三種方法只能將字符串寫到一行中;
canvas.setFont(psfontname, size, leading = None)
reportlab中table表格的繪制:
t=Table(data,5*[0.4*inch], 4*[0.4*inch])
t.setStyle(TableStyle([('ALIGN',(1,1),(-2,-2),'RIGHT'),
('TEXTCOLOR',(1,1),(-2,-2),colors.red),
('VALIGN',(0,0),(0,-1),'TOP'),
('TEXTCOLOR',(0,0),(0,-1),colors.blue),
('ALIGN',(0,-1),(-1,-1),'CENTER'),
('VALIGN',(0,-1),(-1,-1),'MIDDLE'),
('TEXTCOLOR',(0,-1),(-1,-1),colors.green),
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('BOX', (0,0), (-1,-1), 0.25, colors.black),
]))
TableStyle類用於設置表格的樣式:
樣式包含在一個列表中,樣式中每個元組元素分別代表一種樣式的設置,("ALIGN",(1,1),(-2,-2),'RIGHT')
元組中一般包含4-5個參數,第一個參數為參數關鍵字,第二個參數(元組):設定表格中初始的cell, 第三個參數(元組),設定表格中結束的參數,
Reportlab中強制換行:
如果是在簡單的文本中強制換行,可以使用換行符“\n”('line1\nline2\nline3'),如果是在Paragraph中換行使用"<br/>"
Reportlab中設置table左對齊:
Table(coreTable2_data,2*[2*inch],5*[0.25*inch],hAlign="LEFT")
Tags: leading import 笛卡爾 字符串 坐標系
文章來源: