[reportlab-users] Line splitting for long tables

Vasudev Ram vasudevram at gmail.com
Wed Oct 21 15:31:23 EDT 2009


On Wed, Oct 21, 2009 at 11:15 PM, Roberto Alsina <ralsina at netmanagers.com.ar

> wrote:



> On Wednesday 21 October 2009 16:31:43 Benjamin Higgins wrote:

> > We've been using ReportLab with great results except for one hang-up --

> we

> > have some tables that are too wide for the page sometimes. It's caused

> > by long text in one of the cells. We need a solution for splitting text

> > to multiple lines so that the table will fit on the page. Any

> suggestions

> > on how to do this, even a rudimentary solution? I'm thinking of writing

> > an algorithm myself to split long text to multiple lines but I'm sure

> it's

> > not going to be very good.

>

> Why not use a Paragraph inside that cell?

>

> --

> ("\''/").__..-''"`-. . Roberto Alsina

> `9_ 9 ) `-. ( ).`-._.`) KDE Developer (MFCH)

> (_Y_.)' ._ ) `._`. " -.-' http://lateral.netmanagers.com.ar

> _..`-'_..-_/ /-'_.' The 6,855th most popular site of Slovenia

> (l)-'' ((i).' ((!.' according to alexa.com (27/5/2007)

> _______________________________________________

> reportlab-users mailing list

> reportlab-users at lists2.reportlab.com

> http://two.pairlist.net/mailman/listinfo/reportlab-users

>


Paragraph in a cell sounds like a good solution. But writing your own
algorithm for line splitting could be fun too.


>I'm thinking of writing

> an algorithm myself to split long text to multiple lines but I'm sure

it's

> not going to be very good.


Why does it need to be very good? As long as it works ... :)
Obviously good or very good is better (heh:) than not so good, but if it
meets the need, you've at least solved your problem. unless by "very good"
you mean its speed, and performance is an issue.

Here is an overview of a possible algorithm - it's off the top of my head,
so no guarantee that it is right - you'd have to test it:

I'm assuming that you have the line in a Python string.

1) If you don't mind splitting the line in the middle of a word, the
algorithm is trivial - just split the line at every cell_len characters,
where cell_len is the number of characters that can fit into (one line of)
your table cell. (You could optionally add a hyphen (after cell_len - 1
characters, to allow one character for the hyphen) to indicate the line is
split in the middle of a word - when it is.) The last line in the cell may
have less than cell_len characters, but that's ok.

2) If approach 1) above (splitting the line in the middle of words) is not
applicable, you'll have to go to the cell_len'th character of the line, step
backwards character by character, looking for the first space from that
point towards the left (in lower string indices than cell_len). Split the
line at that point (i.e. the first space found). Repeat for the rest of the
line (i.e. string) to the right of the space (i.e. now treating that part of
the string as the whole string was treated earlier above), as long as the
rest of the line (string) is still longer than cell_len. Voila! :) Though it
may be less effiicient, this algorithm should lend itself nicely to a
recursive solution, I think, based on the technique I describe above, which
is recursive - i.e. repeats the partial solution with successively smaller
pieces of the string, until the string length is less than cell_len, at
which point the recursive algorithm terminates. It can also easily be
implemented in an interative, non-recursive way, instead.

The above may not handle the case when the whole substring s[:cell_len] has
no space it it (you're looking for the first space, and there is none
there). Handle that case separately (but note, there is a twist involved
here - as they say, I'm leaving that as an exercise to the reader :).

HTH,
Vasudev Ram
Biz: dancingbison.com
Blog: jugad2.blogspot.com
Products: dancingbison.com/products.html
Twitter: @vasudevram
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20091022/1f35e53e/attachment.htm>


More information about the reportlab-users mailing list