Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8114397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:07:58+00:00 2026-06-06T03:07:58+00:00

I am trying to produce a PDF document with different odd/even page layouts (to

  • 0

I am trying to produce a PDF document with different odd/even page layouts (to allow asymmetric borders for binding) using Python 2.7 and ReportLab. To further complicate matters I am trying to produce two-columns per page.

def WritePDF(files):

    story = []
    doc = BaseDocTemplate("Polar.pdf", pagesize=A4, title = "Polar Document 5th Edition")

    oddf1  = Frame(doc.leftMargin, doc.bottomMargin, doc.width/2-6, doc.height, id='oddcol1') 
    oddf2  = Frame(doc.leftMargin+doc.width/2+6, doc.bottomMargin, doc.width/2-6, doc.height, id='oddcol2')
    evenf1 = Frame(doc.leftMargin, doc.bottomMargin, doc.width/2-6, doc.height, id='evencol1') 
    evenf2 = Frame(doc.leftMargin+doc.width/2+6, doc.bottomMargin, doc.width/2-6, doc.height, id='evencol2')
    doc.addPageTemplates([PageTemplate(id='EvenTwoC',frames=[evenf1,evenf2],onPage=evenFooter),
                          PageTemplate(id='OddTwoC', frames=[oddf1, oddf2], onPage=oddFooter)])


    ...

    story.append(Paragraph(whatever, style))

What I can’t figure out is how to make ReportLab alternate between right and left (or odd and even) pages. Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:08:00+00:00Added an answer on June 6, 2026 at 3:08 am

    I found the solution I guess ! 🙂

    I had to dig into the source code. I found the solution in the file reportlab/platypus/doctemplate.py on line 636. It’s not the first time I had to do this, as the documentation is pretty limited…

    Now, what I found:

    def handle_nextPageTemplate(self,pt):
            '''On endPage change to the page template with name or index pt'''
            if type(pt) is StringType:
                # ... in short, set self._nextPageTemplate
            elif type(pt) is IntType:
                # ... in short, set self._nextPageTemplate
            elif type(pt) in (ListType, TupleType):
                #used for alternating left/right pages
                #collect the refs to the template objects, complain if any are bad
                c = PTCycle()
                for ptn in pt:
                    found = 0
                    if ptn=='*':    #special case name used to short circuit the iteration
                        c._restart = len(c)
                        continue
                    for t in self.pageTemplates:
                        if t.id == ptn:
                            c.append(t)
                            found = 1
                    if not found:
                        raise ValueError("Cannot find page template called %s" % ptn)
                if not c:
                    raise ValueError("No valid page templates in cycle")
                elif c._restart>len(c):
                    raise ValueError("Invalid cycle restart position")
    
                #ensure we start on the first one
                self._nextPageTemplateCycle = c.cyclicIterator()
            else:
                raise TypeError("argument pt should be string or integer or list")
    

    And I checked where this self._nextPageTemplateCycle is used, so this is what I think should work (not tested though):

    story = []
    # ...
    # doc.addPageTemplates([...])
    
    story.append(NextPageTemplate(['pageLeft', 'pageRight'])) # this will cycle through left/right/left/right/...
    
    story.append(NextPageTemplate(['firstPage', 'secondPage', '*', 'pageLeft', 'pageRight'])) # this will cycle through first/second/left/right/left/right/...
    

    Add this to story once, when you want to start alternating pages. Use another normal NextPageTemplate to stop this cycle (because in the source, there’s a del self._nextPageTemplateCycle if you do that).

    Hope it helps, and do say if it works, I can’t make sure right now, but I will!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write metadata to a pdf file using the following python code:
I'm trying to produce a string using a NSDate category in this way: NSString*
I'm trying to produce a line chart using Flot, but I want the data
I'm trying to produce some graphs using GNUplot with a makefile. I would like
I am trying to produce a nested XML document from a flat XML document
I'm trying to produce a page that would function a bit like a digital
I am trying to produce an interactive Haskell program using the interact function with
I'm trying to produce a plot with two lines using data taken from stdin.
I'm trying to produce sheets of photographs with captions arranged in a grid using
I'm trying to produce a report using c#. First part of the report gets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.