How to insert image with Reportlab and variable extension text?

0

What I want is to generate a file in which on the first page is an image generated with matplotlib and then insert text of variable size, as I understand this method makes the number of pages adapt to the text, The code that I have is the following:

from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet,ParagraphStyle
from reportlab.platypus import Spacer,SimpleDocTemplate, Table,TableStyle
from reportlab.platypus import Paragraph, Image
from reportlab.lib import colors
import matplotlib.pyplot as plt
import io
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch, cm

doc=SimpleDocTemplate("para-grahp.pdf",pagesize=letter,showBoundary=1)
styleSheet=getSampleStyleSheet()
#Figura de una grafica
fig = plt.figure(figsize=(4, 3))
plt.plot([1,2,3,4])
plt.grid(True)
plt.ylabel('some numbers')
imgdata =io.BytesIO()
fig.savefig(imgdata, format='png')
imgdata.seek(0)

#This image is what I want to insert before all the text

Image = ImageReader(imgdata)


story=[]
h1=styleSheet['Heading1']
h1.pageBreakBefore=0
h1.keepWithNext=1
h1.backColor=colors.blue
h2=styleSheet['Heading2']
h2.pageBreakBefore=0
h2.keepWithNext=1
P=Paragraph("Grafica",h1)
story.append(P)
P=Paragraph("Estilo h2 ",h2)
story.append(P)
style=styleSheet['BodyText']
texto=" Texto escrito para vercomocrear ficheros PDF."+\
"Este parrafo esta escritoen estilo BodyText"
texto_largo=texto
#texto_largo=texto*10025 
P=Paragraph(texto_largo,style)
story.append(P)
story.append(Spacer(0,12))
t=Table([['','Ventas','Compras'],['Enero',1000, 2000],['Febrero',3000,100.5],['Marzo',2000,1000],
    ['Febrero',3000,100.5],['','Ventas','Compras'],['Enero',1000, 2000],['Febrero',3000,100.5],['Marzo',2000,1000],
    ['Febrero',3000,100.5],['','Ventas','Compras'],['Enero',1000, 2000],['Febrero',3000,100.5],['Marzo',2000,1000],
    ['Febrero',3000,100.5],['','Ventas','Compras'],['Enero',1000, 2000],['Febrero',3000,100.5],['Marzo',2000,1000],
    ['Febrero',3000,100.5],['','Ventas','Compras'],['Enero',1000, 2000],['Febrero',3000,100.5],['Marzo',2000,1000],
    ['Febrero',3000,100.5]],rowHeights=1.8 *23)
story.append(t)
story.append(Spacer(0,15))
P=Paragraph("Cabecera h1",h1)
story.append(P)
cadena=''' Mediante ReportLabes posible43 generar ficheros PDF degran44 calidad. Es posible45 incluir graficos, image-nes,46 tablas; creando informes47 de gran calidad '''
P=Paragraph(cadena,style)
story.append(Spacer(0,15))

doc.build(story)
    
asked by Leonel Galicia 23.05.2018 в 04:04
source

0 answers