[reportlab-users] Line numbers in Platypus

Jonathan Claggett jonathan at claggett.org
Thu Mar 20 00:24:05 EDT 2008


On Wed, Mar 19, 2008 at 11:29 AM, Jonathan Claggett <jonathan at claggett.org>
wrote:


> Hello,

>

> I've just recently become aware of the RL toolkit (thus my first post to

> the list :-) and I'm evaluating it for use in generating legal bills* for a

> model UN youth organization.

>

> One of the requirements of these bills is that the body has line numbers

> and I'm not sure how or even if line numbering can occur with variable

> height objects like the Paragraph. My initial reaction after reading the

> user manual is 'probably'.

>

> At a hand waving level, I think I can subclass the BaseDocTemplate to

> create an afterFlowable method which determines how many lines a Paragraph

> or other flowable has used (using the wrap method?) which I then use to draw

> a sequence (maintained by the sequencer module) of line numbers in a

> separate frame down the left hand margin.

>

> Does this above approach sound reasonable? Maybe Platypus has a line

> numbering feature already and I missed it? Perhaps someone has already

> created a line numbering document template that I could use?

>

> Thanks for any help you can provide.

>

> Regards,

> Jonathan

>

> * e.g., http://jonathan.claggett.googlepages.com/sample-bill.pdf




Hello again,

just to follow up with my original post, I hacked something together that
provides line numbering which, if you're interested, I've appended to this
message. It is not very robust in that it assumes that the paragraphs will
never have any large fonts or images embedded in them. Also, using a regex
to search for a sqlreset tag is pretty fragile. That said, it works for my
specific needs ;-)

Actually, after looking at the Paragraph code briefly, I think the proper
place to add line numbers is when the paragraph line is being drawn because
that is the point at which the line's y position is known (which is
important for matching the line number up with the text). But, this would
probably need to work in conjunction with a Frame level setting so the
Paragraph would know where in the margin to draw the line number.

Jonathan

----

import re
from reportlab.platypus import SimpleDocTemplate, Paragraph, Frame, Spacer
from reportlab.lib.styles import getSampleStyleSheet
styles = getSampleStyleSheet()

class BillTemplate(SimpleDocTemplate):
def beforePage(self):

# Create a frame just for line numbers.
f = self.pageTemplate.frames[0]
self.ln_frame = Frame(0, f.y1, f.x1, f.height)
self.ln_story = []

# Compile a regex expression for finding line_number resets.
self.seqreset_pattern = re.compile(
'<\s*seqreset\s+id\s*=\s*"line_number"\s*/>')

def afterFlowable(self, flowable):

# Line numbers only apply to 'Normal' Paragraphs.
if isinstance(flowable, Paragraph) \
and flowable.style == styles['Normal']:
text = '<para align="right">'
text += '<seqdefault id="line_number"/>'

# Reset line numbering if we find an appropriate <seqreset/>
tag.
if self.seqreset_pattern.search(flowable.text):
text += '<seqreset/>'

# A <seq/> tag is added for every line in the paragraph.
text += '<br/>'.join(['<seq/>'] * len(flowable.blPara.lines))

text += '</para>'

item = Paragraph(text, styles['Normal'])

else:
item = Spacer(1, flowable.height)

self.ln_story.append(item)

def afterPage(self):
# Draw the line numbers.
self.ln_frame.addFromList(self.ln_story, self.canv)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20080320/db2d89c4/attachment.htm>


More information about the reportlab-users mailing list