[reportlab-users] Getting the number of lines in a frame

Saketh Bhamidipati saketh.bhamidipati at gmail.com
Mon Aug 21 08:43:46 EDT 2006


I have a frame from which I want to get the number of lines of text.
Checking for newlines does not work, because the frames wrap the text. I
want to get the number of lines of text after the frame wraps them.

Here is my template code:

class TestTemplate(BaseDocTemplate):
    """
    Direct interface class to ReportLab's Platypus library.
    """
    _invalidInitArgs = ('pageTemplates',)
    def __init__(self, filename, title, author, fontsize, summaryframe,
            boundaries, **kw):

        if boundaries:
            self.boundaries = 1
        else:
            self.boundaries = 0

        self.textframe = textframe = Frame(2 * inch, 1 * inch, 6 * inch, 9 *
                inch, id = 'TF', showBoundary = self.boundaries)
        self.headingframe = headingframe = Frame(0.5 * inch, 1 * inch, 1.5 *
                inch, 9 * inch, id = 'HF', showBoundary = self.boundaries)
        self.allowSplitting = 1
        apply(BaseDocTemplate.__init__,(self,filename),kw)

        self.title = unicode(title)
        self.author = unicode(author)
        self.fontsize = fontsize

        self.addPageTemplates(PageTemplate('normal',[textframe,headingframe],
self.myPageTemplate))

    def fillTextFrame(self, flowables):
        f = self.textframe
        self.handle_currentFrame('TF')
        while len(flowables)>0 and f is self.frame:
            self.handle_flowable(flowables)

    def myPageTemplate(self, canvas, doc):
        PAGE_WIDTH, PAGE_HEIGHT = canvas._pagesize
        canvas.saveState()
        canvas.setFont('Times-Roman', self.fontsize + 2)
        canvas.drawCentredString(PAGE_WIDTH / 2.0, PAGE_HEIGHT - 108,
                unicode(self.title))
        canvas.drawString(PAGE_WIDTH - 2 * inch, PAGE_HEIGHT - inch,
                unicode(self.author))
        canvas.setFont('Times-Roman', self.fontsize)
        canvas.drawCentredString(PAGE_WIDTH / 2.0, inch / 2.0, "%d" %
doc.page)
        canvas.restoreState()

    def fillHeadingFrame(self, flowables):
        f = self.headingframe
        self.handle_currentFrame('HF')
        while len(flowables)>0 and f is self.headingframe:
            self.handle_flowable(flowables)

    def build(self, flowables1, flowables2):
        self._startBuild()
        while (len(flowables1) > 0 or len(flowables1) > 0):
            self.clean_hanging()
            self.fillTextFrame(flowables1)
            self.fillHeadingFrame(flowables2)

        self._endBuild()

I hope that there is a way for me to count the number of lines of text. I
don't want to have to measure the width of the characters and calculate the
lines of text manually.

Thanks in advance,
Saketh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://two.pairlist.net/pipermail/reportlab-users/attachments/20060821/0d3c229d/attachment.html


More information about the reportlab-users mailing list