Python Reportlab - 如果段落对于一行来说太长,如何引入换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3816006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:52:58  来源:igfitidea点击:

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

pythonpdf-generationnewlinereportlab

提问by Vinod

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

我有一个要添加到 reportlab 框架的文本列表

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.

如果图例太长,则末尾的文本根本不可见。在这种情况下如何引入换行符。

回答by kradem

style.wordWrap = 'LTR'

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

抱歉,如果我将其误解为letter,但 Paragraph 本身也是“文字包装”,与文档页面大小有关。

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.

亚洲语言有“CJK”的用户指南值,可能您的设置是根据其他内容(例如亚洲语言分词)执行文本搜索终点线。将它设置为 None 应该做的事情。

回答by PolyGeo

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

这可能适用也可能不适用,但我刚刚了解到,\n我通常用来在 Python 字符串中引入新行的内容会被 ReportLab 的 Paragraph 对象忽略。

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

我从邮件列表中了解到,在 Paragraph 中,您可以使用 HTML<br/>来引入新行。

That works well for me.

这对我很有效。

回答by ndequeker

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

正如PolyGeo所说,您可以使用<br />向段落添加新行。

Convert new lines to <br />tags

将新行转换为<br />标签

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

Updated code

更新代码

 for legend in legends:
        content = str(legend).replace('\n','<br />\n')
        elements.append(Paragraph(content, style))