简体   繁体   English

如何在ReportLab段落中插入回车符?

[英]How to insert a carriage return in a ReportLab paragraph?

Is there a way to insert a carriage return in a Paragraph in ReportLab? 有没有办法在ReportLab的段落中插入回车符? I am trying to concatenate a "\\n" to my paragraph string but this isnt working. 我试图将“\\ n”连接到我的段落字符串,但这不起作用。

Title = Paragraph("Title" + "\n" + "Page", myStyle)

I want to do this since I am putting names into cells and want to control how many names lie on a line in a cell (ideally 1). 我想这样做,因为我将名字放入单元格并想要控制单元格中一行上有多少名称(理想情况下为1)。 One cell can contain multiple names but within that cell i would like each name to be on its own line, hence the need to insert a new line. 一个单元格可以包含多个名称,但在该单元格中我希望每个名称都在其自己的行上,因此需要插入新行。

At some point im getting a flowable to large for frame error (I think it has something to do with a table being too large OR having too many merged rows). 在某些时候我得到一个可流动到大的帧错误(我认为这与表太大或有太多的合并行有关)。 The only way i can think to suppress this is to have only one name per line in a cell so that i can limit table size based on a count of names and segment the tables into smaller tables. 我能想到的唯一方法就是在一个单元格中每行只有一个名称,这样我就可以根据名称的数量来限制表格大小,并将表格分成更小的表格。

Seems like there has to be a much cleaner way of doing this. 似乎必须有一个更清洁的方式来做到这一点。 Any suggestions? 有什么建议?

A Paragraph is a Flowable in reportlab. Paragraph在reportlab中是可Flowable的。 A newline character will not work within a flowable in the way you want it to. 换行符不会以您希望的方式在可流动的内部工作。 If your Paragraph is within a table (as you suggest), you might consider creating a cell without a flowable. 如果您的Paragraph在表格内(如您所建议的那样),您可以考虑创建一个没有可流动的单元格。 For example, you might do this: 例如,您可以这样做:

data = [['Title\nPage', 'Name', 'Exists'],  # note the newline character
        ['', 'George', 'True']]
t = Table(data, style=style_)
...

The above example will make the first data cell two rows tall (but part of the same cell). 上面的例子将使第一个数据单元高两行(但是同一单元的一部分)。

If you really need to preserve the style of the Paragraph flowable, however, you could insert two paragraphs into the same cell: 但是,如果您确实需要保留Paragraph可流动的样式,则可以在同一个单元格中插入两个段落:

title1 = Paragraph("Title", myStyle)
title2 = Paragraph("Page", myStyle)
cell = [title1, title2]               # put this in a single cell of your table

If you want to start a new paragraph (regardless of whether you are in a table or not), you can use the <br/> tag. 如果您想要开始一个新段落(无论您是否在表中),您都可以使用<br/>标签。 This should work for you as well: 这对你也有用:

Title = Paragraph("Title" + "<br/>" + "Page", myStyle)

(credit: Reportlab - how to introduce line break if the paragraph is too long for a line ) (信用: Reportlab - 如果段落太长,如何引入换行符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM