Printing framework - Part 9 (Python)

Keywords : ReportLab, Printing, PDF.


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

img_sample_one.png

   1 # sample_one.py
   2 
   3 """
   4 
   5 Sample platypus document
   6 From the FAQ at reportlab.org/oss/rl-toolkit/faq/#1.1
   7 
   8 """
   9 
  10 from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
  11 from reportlab.lib.styles import getSampleStyleSheet
  12 from reportlab.rl_config import defaultPageSize
  13 from reportlab.lib.units import inch
  14 
  15 #---------------------------------------------------------------------------
  16 
  17 PAGE_HEIGHT=defaultPageSize[1]
  18 PAGE_WIDTH=defaultPageSize[0]
  19 styles = getSampleStyleSheet()
  20 Title = "Hello world"
  21 pageinfo = "platypus example"
  22 
  23 #---------------------------------------------------------------------------
  24 
  25 def myFirstPage(canvas, doc):
  26     """
  27     ...
  28     """
  29 
  30     canvas.saveState()
  31     canvas.setFont('Times-Bold',16)
  32     canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title)
  33     canvas.setFont('Times-Roman',9)
  34     canvas.drawString(inch, 0.75 * inch,"First Page / %s" % pageinfo)
  35     canvas.restoreState()
  36 
  37 #---------------------------------------------------------------------------
  38 
  39 def myLaterPages(canvas, doc):
  40     """
  41     ...
  42     """
  43 
  44     canvas.saveState()
  45     canvas.setFont('Times-Roman', 9)
  46     canvas.drawString(inch, 0.75 * inch,"Page %d %s" % (doc.page, pageinfo))
  47     canvas.restoreState()
  48 
  49 #---------------------------------------------------------------------------
  50 
  51 def go():
  52     """
  53     ...
  54     """
  55 
  56     doc = SimpleDocTemplate("phello.pdf")
  57     Story = [Spacer(1,2*inch)]
  58     style = styles["Normal"]
  59     for i in range(100):
  60         bogustext = ("Paragraph number %s. " % i) *20
  61         p = Paragraph(bogustext, style)
  62         Story.append(p)
  63         Story.append(Spacer(1,0.2*inch))
  64     doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)
  65 
  66 #---------------------------------------------------------------------------
  67 
  68 if __name__ == "__main__":
  69     go()


Second example

img_sample_two.png

   1 # sample_two.py
   2 
   3 """
   4 
   5 https://fraoustin.fr/old/python_reportlab.html
   6 
   7 """
   8 
   9 from reportlab.graphics.shapes import Drawing
  10 from reportlab.graphics.charts.barcharts import VerticalBarChart
  11 
  12 #---------------------------------------------------------------------------
  13 
  14 d = Drawing(300, 200)
  15 
  16 chart = VerticalBarChart()
  17 chart.width = 260
  18 chart.height = 160
  19 chart.x = 20
  20 chart.y = 20
  21 chart.data = [[1,2], [3,4]]
  22 chart.categoryAxis.categoryNames = ['foo', 'bar']
  23 chart.valueAxis.valueMin = 0
  24 
  25 d.add(chart)
  26 d.save(fnRoot='test1', formats=['png', 'pdf'])


Third example

img_sample_three.png

   1 # sample_three.py
   2 
   3 """
   4 
   5 https://fraoustin.fr/old/python_reportlab.html
   6 
   7 """
   8 
   9 from reportlab.lib.styles import getSampleStyleSheet
  10 from reportlab.lib.pagesizes import A4
  11 from reportlab.platypus import SimpleDocTemplate, Paragraph
  12 
  13 #---------------------------------------------------------------------------
  14 
  15 styles = getSampleStyleSheet()
  16 styleN = styles['Normal']
  17 
  18 story = []
  19 story.append(Paragraph("Blabla bla", styleN))
  20 
  21 doc = SimpleDocTemplate('test2.pdf',pagesize = A4,
  22                         title = 'Premier test',
  23                         author = 'Christophe Guyeux' )
  24 doc.build(story)


Fourth example

img_sample_four.png

   1 # sample_four.py
   2 
   3 """
   4 
   5 https://fraoustin.fr/old/python_reportlab.html
   6 
   7 """
   8 
   9 from reportlab.lib.styles import getSampleStyleSheet
  10 from reportlab.lib.pagesizes import A4
  11 from reportlab.platypus import Paragraph, SimpleDocTemplate
  12 
  13 #---------------------------------------------------------------------------
  14 
  15 styles = getSampleStyleSheet()
  16 styleT = styles['Title']
  17 styleH1 = styles['Heading1']
  18 styleH2 = styles['Heading2']
  19 styleN = styles['Normal']
  20 
  21 story = []
  22 story.append(Paragraph("Ceci est le titre", styleT))
  23 story.append(Paragraph("Ceci est la section", styleH1))
  24 story.append(Paragraph("Ceci est la sous-section", styleH2))
  25 story.append(Paragraph("Ceci est le paragraphe", styleN))
  26 
  27 doc = SimpleDocTemplate('test3.pdf',pagesize = A4,
  28                         title = 'Divers styles',
  29                         author = 'Christophe Guyeux' )
  30 doc.build(story)


Fifth example

img_sample_five.png

   1 # sample_five.py
   2 
   3 """
   4 
   5 https://fraoustin.fr/old/python_reportlab.html
   6 
   7 """
   8 
   9 from reportlab.lib.pagesizes import A4
  10 from reportlab.platypus import Paragraph, SimpleDocTemplate, Table, TableStyle
  11 from reportlab.lib.units import cm
  12 from reportlab.lib import colors
  13 
  14 #---------------------------------------------------------------------------
  15 
  16 story = []
  17 data = [ [1,2,3],[4,5,6],[7,8,9] ]
  18 t=Table(data, len(data)*[2*cm], len(data[0])*[1*cm])
  19 t.setStyle(TableStyle([('FONTSIZE', (0,0),(-1,-1),12),
  20             ('INNERGRID', (-1,-1), (0,0), 0.5, colors.blue),
  21             ('BOX', (0,0),(-1,-1), 0.5, colors.red) ]))
  22 story.append(t)
  23 
  24 doc = SimpleDocTemplate('test4.pdf',pagesize = A4,
  25                         title = 'Premier test',
  26                         author = 'Christophe Guyeux' )
  27 doc.build(story)


Download source

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...

Printing framework - Part 9 (Python) (last edited 2020-10-07 10:43:31 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.