Reportlab: header with data from page

后端 未结 1 780
挽巷
挽巷 2021-01-19 00:48

I\'m using the on page function and a page template to make headers for a subset of the pages in my document:

templates.append(PageTemplate(id=\'Overview\',          


        
1条回答
  •  被撕碎了的回忆
    2021-01-19 01:02

    Here's another idea...

    Perhaps it would work to use specific flowables that can be identified to update the course. You can add custom attributes to flowables if necessary to help identify them (see this post).

    For example, you might be able to do something like this:

    ...
    report.append(some_content)
    
    report.append(PageBreak())
    report[-1].new_course = True  # gives that PageBreak flowable a custom attribute
    
    report.append(some_more_content)
    ...
    

    And set up some variables:

    course_list = [...]
    course_iter = iter(course_list)
    current_course = next(course_iter)
    

    Then you can check each flowable after it is rendered to see if it has that attribute and update the current course if it does.

    def afterFlowable(flowable):
        global current_course
        if hasattr(flowable, 'new_course'):
            current_course = next(course_iter)
    
    doc.afterFlowable = afterFlowable
    

    HeaderOverview will be able to use the current_course variable to get the right course, since both HeaderOverview and afterFlowable are called at various points during the final build.

    0 讨论(0)
提交回复
热议问题