= How to use printing framework - Part 9 (Python) = '''Keywords :''' ReportLab, Printing, PDF. <<TableOfContents>> -------- = Demonstrating : = __'''''Tested''' py3.x, wx4.x and Win10. ''__ Printing is an essential element for your programs, here we show you how to print. Are you ready to use some samples ? ;) Test, modify, correct, complete, improve and share your discoveries ! (!) -------- == ReportLab (PDF Toolkit) : == An Open Source Python library for generating PDFs and graphics. You must install this package for use it : '''pip install reportlab''' === First example === {{attachment:img_sample_one.png}} {{{#!python # sample_one.py """ Sample platypus document From the FAQ at reportlab.org/oss/rl-toolkit/faq/#1.1 """ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer from reportlab.lib.styles import getSampleStyleSheet from reportlab.rl_config import defaultPageSize from reportlab.lib.units import inch #--------------------------------------------------------------------------- PAGE_HEIGHT=defaultPageSize[1] PAGE_WIDTH=defaultPageSize[0] styles = getSampleStyleSheet() Title = "Hello world" pageinfo = "platypus example" #--------------------------------------------------------------------------- def myFirstPage(canvas, doc): """ ... """ canvas.saveState() canvas.setFont('Times-Bold',16) canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title) canvas.setFont('Times-Roman',9) canvas.drawString(inch, 0.75 * inch,"First Page / %s" % pageinfo) canvas.restoreState() #--------------------------------------------------------------------------- def myLaterPages(canvas, doc): """ ... """ canvas.saveState() canvas.setFont('Times-Roman', 9) canvas.drawString(inch, 0.75 * inch,"Page %d %s" % (doc.page, pageinfo)) canvas.restoreState() #--------------------------------------------------------------------------- def go(): """ ... """ doc = SimpleDocTemplate("phello.pdf") Story = [Spacer(1,2*inch)] style = styles["Normal"] for i in range(100): bogustext = ("Paragraph number %s. " % i) *20 p = Paragraph(bogustext, style) Story.append(p) Story.append(Spacer(1,0.2*inch)) doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages) #--------------------------------------------------------------------------- if __name__ == "__main__": go() }}} -------- === Second example === {{attachment:img_sample_two.png}} {{{#!python # sample_two.py """ https://fraoustin.fr/old/python_reportlab.html """ from reportlab.graphics.shapes import Drawing from reportlab.graphics.charts.barcharts import VerticalBarChart #--------------------------------------------------------------------------- d = Drawing(300, 200) chart = VerticalBarChart() chart.width = 260 chart.height = 160 chart.x = 20 chart.y = 20 chart.data = [[1,2], [3,4]] chart.categoryAxis.categoryNames = ['foo', 'bar'] chart.valueAxis.valueMin = 0 d.add(chart) d.save(fnRoot='test1', formats=['png', 'pdf']) }}} -------- === Third example === {{attachment:img_sample_three.png}} {{{#!python # sample_three.py """ https://fraoustin.fr/old/python_reportlab.html """ from reportlab.lib.styles import getSampleStyleSheet from reportlab.lib.pagesizes import A4 from reportlab.platypus import SimpleDocTemplate, Paragraph #--------------------------------------------------------------------------- styles = getSampleStyleSheet() styleN = styles['Normal'] story = [] story.append(Paragraph("Blabla bla", styleN)) doc = SimpleDocTemplate('test2.pdf',pagesize = A4, title = 'Premier test', author = 'Christophe Guyeux' ) doc.build(story) }}} -------- === Fourth example === {{attachment:img_sample_four.png}} {{{#!python # sample_four.py """ https://fraoustin.fr/old/python_reportlab.html """ from reportlab.lib.styles import getSampleStyleSheet from reportlab.lib.pagesizes import A4 from reportlab.platypus import Paragraph, SimpleDocTemplate #--------------------------------------------------------------------------- styles = getSampleStyleSheet() styleT = styles['Title'] styleH1 = styles['Heading1'] styleH2 = styles['Heading2'] styleN = styles['Normal'] story = [] story.append(Paragraph("Ceci est le titre", styleT)) story.append(Paragraph("Ceci est la section", styleH1)) story.append(Paragraph("Ceci est la sous-section", styleH2)) story.append(Paragraph("Ceci est le paragraphe", styleN)) doc = SimpleDocTemplate('test3.pdf',pagesize = A4, title = 'Divers styles', author = 'Christophe Guyeux' ) doc.build(story) }}} -------- === Fifth example === {{attachment:img_sample_five.png}} {{{#!python # sample_five.py """ https://fraoustin.fr/old/python_reportlab.html """ from reportlab.lib.pagesizes import A4 from reportlab.platypus import Paragraph, SimpleDocTemplate, Table, TableStyle from reportlab.lib.units import cm from reportlab.lib import colors #--------------------------------------------------------------------------- story = [] data = [ [1,2,3],[4,5,6],[7,8,9] ] t=Table(data, len(data)*[2*cm], len(data[0])*[1*cm]) t.setStyle(TableStyle([('FONTSIZE', (0,0),(-1,-1),12), ('INNERGRID', (-1,-1), (0,0), 0.5, colors.blue), ('BOX', (0,0),(-1,-1), 0.5, colors.red) ])) story.append(t) doc = SimpleDocTemplate('test4.pdf',pagesize = A4, title = 'Premier test', author = 'Christophe Guyeux' ) doc.build(story) }}} -------- = Download source = [[attachment:source.zip]] -------- = Additional Information = '''Link :''' https://pypi.org/project/reportlab/ https://www.reportlab.com/opensource/ https://www.blog.pythonlibrary.org/2012/06/27/reportlab-mixing-fixed-content-and-flowables/ http://www.blog.pythonlibrary.org/2010/03/08/a-simple-step-by-step-reportlab-tutorial/ http://www.blog.pythonlibrary.org/2010/09/21/reportlab-tables-creating-tables-in-pdfs-with-python/ - - - - - https://wiki.wxpython.org/TitleIndex https://docs.wxpython.org/ -------- = Thanks to = ReportLab team. -------- = About this page = Date (d/m/y) Person (bot) Comments : 10/10/18 - Ecco (Created page for wxPython Phoenix). -------- = Comments = - blah, blah, blah...