Reputation: 131
I am trying to render an image generated by MathplotLib into a pdf created by ReportLab. Please fix.
graph1 = Image(filename='Graphs/AvgAnnMedCostPerEE.png',x=50,y=50,width=None,height=None,path='Graphs/AvgAnnMedCostPerEE.png')#,kind='direct',mask='auto',hAlign='CENTER',)
first_graph_table_header = [["Average Annual Medical Cost Per Employee",""],
["",graph1]]
first_graph_page_style = TableStyle([('SPAN', (0, 0), (1, 0))])
# ('ALIGN', (0, 3), (8, 3), 'CENTER'),
# ('ALIGN', (0, 0), (8, 0), 'CENTER'),
# ('VALIGN', (0, 0), (8, 0), 'MIDDLE'),
# ('VALIGN', (2, 3), (6, 3), 'MIDDLE'),
# ('BACKGROUND', (0, 2), (8, 2), colors.HexColor('#305496')),
# ('BACKGROUND', (0, 3), (8, 3), colors.HexColor('#b4c6e7')),
# ('BACKGROUND', (8, 2), (8, 3), colors.orange),
# ('FONT', (0, 2), (-9, -2), 'Helvetica-Bold', 10),
# ('FONT', (2, 3), (-7, -1), 'Helvetica-Bold', 12),
# ('FONT', (3, 3), (-3, -1), 'Helvetica-Bold', 10),
# ('TEXTCOLOR', (0, 2), (-9, -2), colors.white)
# ])
g1 = Table(first_graph_table_header, colWidths=[14.75, 161],
rowHeights=[5.5*mm, 2*inch])
g1.setStyle(first_graph_page_style)
self.story.append(g1)
self.story.append(Image(filename='Graphs/AvgAnnMedCostPerEE.png',x=50,y=50,width=None,height=None,path='Graphs/AvgAnnMedCostPerEE.png'))
self.story.append(PageBreak())
# def createGraphs(self, canvas, doc):
# ----------------------------------------------------------------------
if __name__ == "__main__":
t = CoverPage_Index()
t.run()
I keep getting AttributeError: Image instance has no attribute 'getKeepWithNext'
Upvotes: 0
Views: 352
Reputation: 36
I had the same problem. After a bit of madness, I understood that the reason is a name clashing. So this is the solution that worked for me. You need a flowable Image, but the name Image is probably not unique in your imported classes. First, give to the Image class you want to use a name that you are sure is unique at the import time; I used GNAA (who on earth would write a class named GNAA?).
from reportlab.platypus.flowables import Image as GNAA
then you can use it to pick an image. In this example, the image is in the same directory as the python program. IMPORTANT: use a jpg, because a png may not work:
logo = "logo.jpg"
im = GNAA(logo, width=200, height=38)
story.append(im)
Now the Image is the correct one, flowable and with all the correct parameters needed. I did non investigate where the doppelganger of the Image is, but this trick works fine.
Upvotes: 2