Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reportlab - how to introduce line break if the paragraph is too long for a line

I have a list of text to be added to a reportlab frame

style = getSampleStyleSheet()['Normal']
style.wordWrap = 'LTR'
style.leading = 12
for legend in legends:
    elements.append(Paragraph(str(legend),style))

If the legend is too long, the text at the end is not visible at all. How to introduce line breaks in this situation.

like image 619
Vinod Avatar asked Sep 28 '10 18:09

Vinod


People also ask

How do you put a line break in text?

To add spacing between lines or paragraphs of text in a cell, use a keyboard shortcut to add a new line. Click the location where you want to break the line. Press ALT+ENTER to insert the line break.

How do I wrap text in a table cell in Reportlab?

As mentioned, you can use 'VALIGN' to wrap text within the cells, but there is another hack if you want to further control the table elements on the canvas. Now adding two components: rowHeights attribute. _argH for finer editing of the row heights.


2 Answers

This may or may not apply but I just learned that \n which I normally use to introduce new lines in Python strings gets ignored by the Paragraph object of ReportLab.

From a mailing list I learned that inside Paragraph you can use HTML's <br/> to introduce the new line instead.

That works well for me.

like image 148
PolyGeo Avatar answered Sep 21 '22 08:09

PolyGeo


As PolyGeo says, you can use <br /> to add new lines to a Paragraph.

Convert new lines to <br /> tags

replace('\n','<br />\n')

Updated code

 for legend in legends:
        content = str(legend).replace('\n','<br />\n')
        elements.append(Paragraph(content, style))
like image 39
ndequeker Avatar answered Sep 21 '22 08:09

ndequeker