User:Simon/Trim4/Layout using Reportlab: Difference between revisions
No edit summary |
No edit summary |
||
Line 2: | Line 2: | ||
From this tutorial, I created a basic page: | From this tutorial, I created a basic page: | ||
<nowiki> | |||
from reportlab.pdfgen import canvas | from reportlab.pdfgen import canvas | ||
Line 8: | Line 8: | ||
c.drawString(100,750,"Welcome to Reportlab!") | c.drawString(100,750,"Welcome to Reportlab!") | ||
c.save() | c.save() | ||
</nowiki> | |||
A form: | A form: | ||
<nowiki> | |||
from reportlab.lib.pagesizes import letter | from reportlab.lib.pagesizes import letter | ||
from reportlab.pdfgen import canvas | from reportlab.pdfgen import canvas | ||
Line 17: | Line 17: | ||
canvas = canvas.Canvas('myfile.pdf', pagesize=letter) | canvas = canvas.Canvas('myfile.pdf', pagesize=letter) | ||
width, height = letter | width, height = letter | ||
</nowiki> | |||
and a letter with flowing text: | and a letter with flowing text: | ||
<nowiki> | |||
import time | |||
from reportlab.lib.enums import TA_JUSTIFY | from reportlab.lib.enums import TA_JUSTIFY | ||
from reportlab.lib.pagesizes import letter | from reportlab.lib.pagesizes import letter | ||
Line 84: | Line 85: | ||
Story.append(Paragraph(ptext, styles["Normal"])) | Story.append(Paragraph(ptext, styles["Normal"])) | ||
Story.append(Spacer(1, 12)) | Story.append(Spacer(1, 12)) | ||
doc.build(Story)</nowiki> | doc.build(Story) | ||
</nowiki> |
Revision as of 14:46, 25 October 2019
Reportlab is a Python library that allows you to make multi-page documents, working with a canvas. I found a nice introduction and tutorial here: https://www.blog.pythonlibrary.org/2010/03/08/a-simple-step-by-step-reportlab-tutorial/
From this tutorial, I created a basic page: from reportlab.pdfgen import canvas c = canvas.Canvas("hello.pdf") c.drawString(100,750,"Welcome to Reportlab!") c.save()
A form: from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas canvas = canvas.Canvas('myfile.pdf', pagesize=letter) width, height = letter
and a letter with flowing text: import time from reportlab.lib.enums import TA_JUSTIFY from reportlab.lib.pagesizes import letter from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib.units import inch doc = SimpleDocTemplate("form_letter.pdf",pagesize=letter, rightMargin=72,leftMargin=72, topMargin=72,bottomMargin=18) Story=[] logo = "python_logo.png" magName = "Pythonista" issueNum = 12 subPrice = "99.00" limitedDate = "03/05/2010" freeGift = "tin foil hat" formatted_time = time.ctime() full_name = "Mike Driscoll" address_parts = ["411 State St.", "Marshalltown, IA 50158"] im = Image(logo, 2*inch, 2*inch) Story.append(im) styles=getSampleStyleSheet() styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY)) ptext = '<font size=12>%s</font>' % formatted_time Story.append(Paragraph(ptext, styles["Normal"])) Story.append(Spacer(1, 12)) # Create return address ptext = '<font size=12>%s</font>' % full_name Story.append(Paragraph(ptext, styles["Normal"])) for part in address_parts: ptext = '<font size=12>%s</font>' % part.strip() Story.append(Paragraph(ptext, styles["Normal"])) Story.append(Spacer(1, 12)) ptext = '<font size=12>Dear %s:</font>' % full_name.split()[0].strip() Story.append(Paragraph(ptext, styles["Normal"])) Story.append(Spacer(1, 12)) ptext = '<font size=12>We would like to welcome you to our subscriber base for %s Magazine! \ You will receive %s issues at the excellent introductory price of $%s. Please respond by\ %s to start receiving your subscription and get the following free gift: %s.</font>' % (magName, issueNum, subPrice, limitedDate, freeGift) Story.append(Paragraph(ptext, styles["Justify"])) Story.append(Spacer(1, 12)) ptext = '<font size=12>Thank you very much and we look forward to serving you.</font>' Story.append(Paragraph(ptext, styles["Justify"])) Story.append(Spacer(1, 12)) ptext = '<font size=12>Sincerely,</font>' Story.append(Paragraph(ptext, styles["Normal"])) Story.append(Spacer(1, 48)) ptext = '<font size=12>Ima Sucker</font>' Story.append(Paragraph(ptext, styles["Normal"])) Story.append(Spacer(1, 12)) doc.build(Story)