Skip to content Skip to sidebar Skip to footer

Reportlab Does Not Reset Sequences When Creating Multiple Documents With Table Of Contents

I am using a template function that creates multiple PDF documents in one program execution using reportlab. These documents are identical by structure and have identical headings

Solution 1:

I have found a quick solution which solves my problem but which I don't like as a final solution.

The problem is that I am using global sequences with the same name. h1, h2 and h3 appear in every document. And reportlab doesn't seem to reset them when a new document is started. So instead I reset the manually before filling the story list.

def build_document(filename):
    # Build story. story = []

    # Reset the sequences
    story.append(Paragraph("<seqreset id='h1'/>", PS('body')))
    story.append(Paragraph("<seqreset id='h2'/>", PS('body')))
    story.append(Paragraph("<seqreset id='h3'/>", PS('body')))

    # ...  rest of the code from the question

Solution 2:

It looks like the reportlab.lib.sequencer.Sequencer object being used is global.

You can reset all counters by supplying a new Sequencer

    from reportlab.lib.sequencer import setSequencer, Sequencer

    
    setSequencer(Sequencer())

You can reset a single counter by doing something like:

    from reportlab.lib.sequencer importgetSequencers= getSequencer()
    s.reset('h1')

You could also try using Sequencer directly instead of injecting XML.

Class Sequencer: Something to make it easy to number paragraphs, sections, images and anything else. The features include registering new string formats for sequences, and 'chains' whereby some counters are reset when their parents. It keeps track of a number of 'counters', which are created on request.

Usage::
 >>> seq = Sequencer() >>> seq.next('Bullets')
 1
 >>> seq.next('Bullets')
 2
 >>> seq.next('Bullets')
 3
 >>> seq.reset('Bullets') >>> seq.next('Bullets')
 1
 >>> seq.next('Figures')
 1
 >>>

Post a Comment for "Reportlab Does Not Reset Sequences When Creating Multiple Documents With Table Of Contents"