Reportlab - how to introduce line break if the par

2019-01-27 21:23发布

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.

3条回答
Melony?
2楼-- · 2019-01-27 22:03

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))
查看更多
该账号已被封号
3楼-- · 2019-01-27 22:16

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.

查看更多
Lonely孤独者°
4楼-- · 2019-01-27 22:26
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.

查看更多
登录 后发表回答