Reportlab:带有页面数据的标题 [英] Reportlab: header with data from page

查看:59
本文介绍了Reportlab:带有页面数据的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用页面功能和页面模板为文档中的一部分页面制作标题:

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', frames=frame, onPage=HeaderOverview))

这个模板的头函数:

################################
# Function HeaderOverview - header for overview page
def HeaderOverview(canvas,doc):
    canvas.saveState()
    headboxh = 15
    headboxx = 20
    headboxy = 730
    headboxw = 570
    canvas.rect(headboxx, headboxy, headboxw, headboxh, fill=1)  
    canvas.setFillColor(colors.black)
    canvas.setFont("Helvetica", 14)
    canvas.setFillColor(colors.white)

    canvas.drawString(headboxx + 15,headboxy+.25*headboxh,"Mathematics")
    textWidth = stringWidth("Mathematics", "Helvetica", 12) 
    canvas.setFont("Helvetica", 12)
    canvas.drawString(headboxw - 15 - textWidth,headboxy+.25*headboxh,course)

    canvas.restoreState()

这很好用,除了传递的 course 变量(随着部分中的每一页而变化)是序列中的最后一个,因为直到最终构建才真正调用此函数(我认为这就是它的工作原理).我需要这样做,以便该值是页面上的值.如果我能在写页面本身时绘制它,那也很好.这是我的尝试:

This works great, except that the course variable that's passed (which changes with each page in the section) is the last one in the sequence, since this function's not really called until the final build (I think that's how it works). What I need is to do this so that the value is the value that's on the page. If I could draw it as I write the page itself, that'd be fine, too. Here's my attempt at that:

####################################################################################
# Function makeGradeOverview(course): makes Overview chart for grade
#
def makeGradeOverview(canvas, course):
    report.append(NextPageTemplate("Overview"))
    report.append(PageBreak())

    headboxh = 50
    headboxx = 20
    headboxy = 600#730
    headboxw = 540

    canvas.saveState()
    canvas.setFont("Helvetica", 12)
    textWidth = stringWidth(course, "Helvetica", 12)
    canvas.drawString(headboxw - 15 - textWidth,headboxy+.25*headboxh,course)
    canvas.restoreState()
    # put course name as title
    if len(course)<=2:
        headerrow = ''.join(['Grade ', course, ' Overview'])
    else:
        headerrow = ''.join([course, ' Overview'])
    report.append(Paragraph(headerrow, styles["Overview Title"]))

    report.append(Spacer(1, 16))

    GridInfo = []
    topics = topiclist(course)

    for topic in topics:
        report.append(Paragraph(topic, styles["Overview Sub"]))
        report.append(Spacer(1, 8))

        subtopics = subtopiclist(course, topic)

        sublist = []
        for subtopic in subtopics:
             report.append(Paragraph(''.join([r'<bullet>&bull</bullet>',subtopic]), styles["Overview Table"]))

这不会抛出错误或任何东西,但它似乎也没有实际绘制任何东西.

This doesn't throw an error or anything, but it doesn't seem to actually draw anything, either.

感谢您的帮助!

推荐答案

这是另一个想法...

也许可以使用可识别的特定流程来更新课程.如有必要,您可以向 Flowable 添加自定义属性以帮助识别它们(请参阅这篇帖子).

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)
...

并设置一些变量:

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

然后您可以在渲染后检查每个 flowable 以查看它是否具有该属性,如果有则更新当前课程.

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 将能够使用 current_course 变量来获得正确的课程,因为 HeaderOverviewafterFlowable> 在最终构建过程中的不同时间点被调用.

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.

这篇关于Reportlab:带有页面数据的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆