15

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.

Vinod
  • 4,138
  • 11
  • 49
  • 65
  • I recommend yo to post this to reportlab-users@lists2.reportlab.com, mailing lists probably are not very cool, but ReportLab mailing list is still the best source for info :) – juanefren Sep 28 '10 at 19:51
  • @juanefren Does this still true? I found the documentation is still very shallow. – handras Feb 07 '19 at 15:44

3 Answers3

27

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.

PolyGeo
  • 1,340
  • 3
  • 26
  • 59
  • 13
    `
    ` and `
    ` work. But `
    ` raises a `ValueError: paraparser: syntax error: No content allowed in br tag`
    – Bob Stein Apr 17 '18 at 09:59
  • 5
    ReportLab uses an XML parser so it's more XHTML than HTML meaning you must have a closing tag. – Tim May 26 '18 at 15:50
  • Some time White-space before and after string, cause an error during report generations. – hardika May 08 '20 at 06:11
14

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))
Community
  • 1
  • 1
ndequeker
  • 7,932
  • 7
  • 61
  • 93
0
style.wordWrap = 'LTR'

Sorry if I misunderstood this as letter, but Paragraph itself is "word wrapped", in relation to document pagesize also.

There's userguide value of 'CJK' for Asian language, possibly your setting do the text to search for finishing line according to something else, like Asian language word splitting. Set it to None should do the thing.

kradem
  • 63
  • 1
  • 9