[reportlab-users] Bounded text (truncated)

Michael Hipp Michael at Hipp.com
Sun Jan 7 12:06:02 EST 2007


Robin Becker wrote:

> Michael Hipp wrote:

>> I'm trying to find a way to print "bounded" text. Meaning I want the "field"

>> it is printed in to be a certain specified size and never exceed that size. If

>> a text value is printed that would go beyond that size, then the text should

>> be truncated.

>>

>> Is there some simple way to accomplish this?

> ......

>

> Maybe not simple. There are ways to do this for two cases. One where you just

> want a single line of text and another where you want something like a paragraph

> with specified width.

>

> In the first case it would be something like this.

>

> from reportlab.pdfbase.pdfmetrics import stringWidth

> t = "this is my very long and disreputable text ... boring etc etc etc"

> fontName='Times-Roman'

> fontSize=12

> desired = 2*72 # two inches

> while t and stringWidth(t,fontName,fontSize)>desired:

> t = t[:-1]

>

> print t


Thanks. This works swimmingly for one-line table values. And even seems
"simple" once hidden away in a method :-)

Thanks,
Michael


More information about the reportlab-users mailing list