Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a carriage return in a ReportLab paragraph?

Is there a way to insert a carriage return in a Paragraph in ReportLab? I am trying to concatenate a "\n" to my paragraph string but this isnt working.

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). 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?

like image 849
user2860797 Avatar asked Dec 05 '14 19:12

user2860797


2 Answers

If you want to start a new paragraph (regardless of whether you are in a table or not), you can use the <br/> tag. 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)

like image 51
numeratus Avatar answered Oct 27 '22 00:10

numeratus


A Paragraph is a Flowable in reportlab. 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. 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:

title1 = Paragraph("Title", myStyle)
title2 = Paragraph("Page", myStyle)
cell = [title1, title2]               # put this in a single cell of your table
like image 43
Justin O Barber Avatar answered Oct 27 '22 01:10

Justin O Barber