Question

I tried to build a table of contents in Reportlab (but failed ... and did not insisted too much as and seems even more than what I'm needing .. might be I'll give a try newly in the future ..).

As now I'd be quite happy to have some simple text as guide for a document (the document is mainly composed by some Pandas generated numbered grids. Id' simly like to have a text with the titles of the grids at the beginning of the Reportlab generated .pdf).

My goal looked so very simple and was to append two Platypuses one with the titels and one with the grids but did not worked. So I move to an even simpler goal and tried to append two Platypuses plain texts .. but that did not worked again ... :-(

My code as below:

# settings
from reportlab.pdfgen import canvas
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import *
styles = getSampleStyleSheet()
PATH_OUT = "C:\\"
titolo = 'Test.pdf'
doc = SimpleDocTemplate( PATH_OUT + titolo )
elements0 = []
elements1 = []
elements2 = []

# 1-st platypus
elements0.append(Paragraph("The Platypus0", styles['Heading1']))
elements0.append(Paragraph("Very <i>Special</i>!", styles['Normal']))

# 2-nd platypus
elements1.append(Paragraph("The Platypus1", styles['Heading1']))
elements1.append(Paragraph("Very <i>Special</i>!", styles['Normal']))

# append them
elements2 = elements0.append(elements1)

# Write the document
doc.build(elements2)

The issue I have is this is miserably crashing apparently because of no len() resulting object.

Do you have any suggestion that might be of help in this ? If I use elements0 or elements1, one separate from the other, they work pretty smoothly but when I try to append one with the other it does not. Any suggestion ?

Thank you so much :-) Fabio.

Was it helpful?

Solution

append on list appends the item in place and does not return a new list. With the following:

elements2 = elements0.append(elements1)

elements2 being assigned the value None and elements0 now contains a new item, which is elements1

elements0[0] -> Paragraph("The Platypus0", styles['Heading1'])
elements0[1] -> Paragraph("Very <i>Special</i>!", styles['Normal'])
elements0[2] -> [Paragraph("The Platypus1", styles['Heading1']), Paragraph("Very <i>Special</i>!", styles['Normal'])

If you want to put the two texts together, use the concatenation

elements2 = elements0 + elements1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top