[英]Python Reportlab Page Break
我正在嘗試使用python中的reportlab生成pdf報告。
我的目標是讓我的pdf文件的第一頁只有一個簡單的標題和一個沒有實際內容的表。 實際內容將從第二頁開始。
在查看了一些SO帖子之后,我發現可以使用afterPage()
命令來破解頁面。 所以,我提出了這樣的事情:
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer,KeepTogether,tables
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.pagesizes import A4,landscape
from reportlab.lib.units import inch,cm,mm
from reportlab.pdfgen import canvas
PAGE_HEIGHT = defaultPageSize[1]
PAGE_WIDTH = defaultPageSize[0]
styles = getSampleStyleSheet()
style = styles["Normal"]
def myFirstPage(canvas, doc):
canvas.saveState()
canvas.setFont('Times-Bold',15)
canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-38, 'Title 1')
canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-58, 'Title 2')
NormalStyle = tables.TableStyle([
('BOX',(0,0),(-1,-1),0.45,colors.blue),
('ALIGN',(0,0),(-1,-1),'LEFT'),
('BACKGROUND',(0,0),(-1,-1),colors.lightblue)
])
mytable = tables.Table([('test','test'),('test2','test2'),('test3','test3')],
colWidths = 1* inch,rowHeights= 0.25 *inch,style = NormalStyle)
mytable.wrapOn(canvas,PAGE_WIDTH ,PAGE_HEIGHT)
mytable.drawOn(canvas,(doc.width/2)-20,700)
doc.afterPage()
canvas.restoreState()
def myLaterPages(canvas, doc):
canvas.saveState()
canvas.setFont('Times-Roman', 9)
canvas.restoreState()
doc = SimpleDocTemplate("myreport.pdf",
leftMargin = 0.75*inch,
rightMargin = 0.75*inch,
topMargin = 1*inch,
bottomMargin = 1*inch)
data = "".join(['this is a test' for i in range(200)])
mydata = Paragraph(data,style)
Story = [Spacer(2.5,0.75*inch)]
Story.append(mydata)
doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)
但相反,我的所有標題,表格和內容都是在第一頁中繪制的,而afterPage()
函數似乎對我的文檔內容沒有任何實際影響。
如何更改我的代碼以便內容(我的代碼中的data
)從第二頁開始?
您可以使用PageBreak()
。 只需插入Story.append(PageBreak())
並從reportlab.platypus
導入它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.