python reportlab調用操作分頁報表

#coding=utf-8
def init_config():
    import reportlab.rl_config
    reportlab.rl_config.warnOnMissingFontGlyphs = 0
    from reportlab.pdfbase import pdfmetrics
    from reportlab.pdfbase.ttfonts import TTFont
    import copy
    pdfmetrics.registerFont(TTFont('zhenhei', 'D:/python_workspace/python_pdf_demo/wqy-zenhei.ttc'))
    stylesheet= getSampleStyleSheet()
    styles= copy.deepcopy(stylesheet['Normal'])
    styles.fontName ='zhenhei'
    styles.fontSize = 20
    return styles
from reportlab.platypus import *
from reportlab.lib.styles import getSampleStyleSheet  
from reportlab.rl_config import defaultPageSize  
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import *
#from reportlab.lib.utils import *
from reportlab.lib.units import mm
styles=init_config()
PAGE_HEIGHT=defaultPageSize[1]  
PAGE_WIDTH=defaultPageSize[0]
Title = "Hello world" 
pageinfo = "platypus example" 
def myFirstPage(canvas,doc):  
    canvas.saveState()  
    canvas.setFont('zhenhei',16)  
    canvas.drawCentredString(PAGE_HEIGHT/2, PAGE_HEIGHT-108, Title)  
    canvas.setFont('zhenhei',9)  
    canvas.drawString((PAGE_WIDTH/2)-20,10,u"首頁")  
    canvas.restoreState()
def myLaterPages(canvas, doc):  
    canvas.saveState()  
    canvas.setFont('zhenhei', 9)  
    canvas.drawString((PAGE_WIDTH/2)-20,10,u"頁碼:%d 頁" % (doc.page))  
    canvas.restoreState()
def go():  
    doc = SimpleDocTemplate("phello.pdf",pagesize=A4) 
    #Story = [Spacer(1,2*inch)]  
    #style = styles["Normal"]
    i=0
    I=Image("logo.jpg")
    I.drawHeight = 1*inch*I.drawHeight / I.drawWidth
    I.drawWidth = 1*inch
    print doc.allowSplitting
    Story=[]
    for i in range(10):
        Story.append(I)
        Story.append(PageBreak())
    #doc.build(Story)
    doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)
    '''for i in range(100):
        I = Image("logo.jpg")
        #print dir(I)
        #vl=getImageData('logo.jpg')
        #print vl
        #I.drawWidth=
        #I.drawHeight=str(I.imageHeight)+'px'
        bogustext=(u"測試數字:%s."%i)
        p = Paragraph(bogustext, styles)  
        Story.append(p)
        Story.append(I)
        Story.append(Spacer(1,0.2*inch))  
        doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)
        print i'''
if __name__ == "__main__":
   # init_config()
    go()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章