[reportlab-users] Multi-level chapter prefixes in TOCs?

Dinu Gherman gherman at darwin.in-berlin.de
Mon Apr 21 10:58:54 EDT 2008


Dinu Gherman:


> Maybe somebody knows what's going on here? Or is there another,

> less stupid way of doing it? I'm including the modified generator

> script.


I got it! There seems to be some interaction I had not foreseen with
the indexing and/or multi-build mechanism of IndexingFlowable, the
base class of the TableOfContents class.

If I reset the chapter numbers at the beginning of every document
build, I get the desired result. Here are the most significant
lines that I had to change:

dinu$ diff -u test_platypus_toc.py test_platypus_toc2.py
--- test_platypus_toc.py 2008-04-19 12:31:59.000000000 +0200
+++ test_platypus_toc2.py 2008-04-21 16:50:21.000000000 +0200
@@ -56,6 +56,11 @@
self.addPageTemplates(template)


+ def beforeDocument(self):
+ # This is called before any processing is done on the document.
+ self.chapNum = [0, 0, 0]
+
+
def afterFlowable(self, flowable):
"Registers TOC entries and makes outline entries."

@@ -64,7 +69,14 @@
if styleName[:7] == 'Heading':
# Register TOC entries.
level = int(styleName[7:])
- text = flowable.getPlainText()
+
+ # test adding multi-level chap prefixes to toc entries
+ self.chapNum[level] += 1
+ for i in range(level+1, 3):
+ self.chapNum[i] = 0
+ chapPrefix = ".".join(map(str, self.chapNum)) + " "
+ text = chapPrefix + flowable.getPlainText()
+
pageNum = self.page
self.notify('TOCEntry', (level, text, pageNum))

And I'm also attaching the whole generator script and PDF output.

Enjoy,

Dinu

-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_platypus_toc2.py
Type: text/x-python-script
Size: 6647 bytes
Desc: not available
Url : <http://two.pairlist.net/pipermail/reportlab-users/attachments/20080421/24ee4b62/attachment-0001.bin>
-------------- next part --------------


-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_platypus_toc2.pdf
Type: application/pdf
Size: 7103 bytes
Desc: not available
Url : <http://two.pairlist.net/pipermail/reportlab-users/attachments/20080421/24ee4b62/attachment-0001.pdf>
-------------- next part --------------



More information about the reportlab-users mailing list