[reportlab-users] Truncate table cell content to end with ellipsis

Yves Forkl Y.Forkl at srz.de
Wed Apr 15 04:45:16 EDT 2009


Robin Becker schrieb:


> it's not exactly clear what you want. If you want to ensure that a

> particular paragraph is only of a specified width then you need to mess

> around with the paragraph wrapping stuff if you need the html style

> formatting. If in fact you just need a string that's truncated and the

> end replaced with '...' then it might be better to think about messing

> with a Table class. Unfortunately we don't have an intermediate thing

> that knows about different fonts/sizes without the full horror of the

> paragraph class.


Thank you very much for your detailed explanations and the code samples.

Sorry for not having specified more clearly what I am looking for. In
fact, I do need HTML formatting inside the table cell, but AFAICS I
could ignore its effects when determining the width. The reason for this
is that I am only using <super></super>, <sub></sub>, and <u></u>.
Underlining shouldn't affect the width at all. The other two elements
are only capable of reducing the width a tiny bit, so ignoring them will
lead to cutting off some characters more than is actually necessary, but
that is OK for me.

This makes me think the solution to my problem might be rather simple:
Treat the content simply as a string (temporarily replacing any HTML
markup), determine how many characters to cut off at the end so that
appending an ellipsis will still keep the result within the first line
of the table cell, replace the extraneous characters with an ellipsis,
then make the result a Paragraph.

How could I implement this?



> In the paragraph approach you can do

>

> w,h = p.wrap(availWidth,0x7fffffff)

>

> to create the broken lines structure and see if the paragraph is in fact

> too long.


I assume availWidth holds the table cell width (minus padding space) in
pt. But how can I know from the values of w and h (actual width and
height of the para??) if the para is too wide and by how many characters
I need to shorten it, or how many characters its first line may contain?
(Though I suppose I don't need to work with paragraphs, as stated above.)

Yves


More information about the reportlab-users mailing list