Reportlab:来自pag的数据头

2024-10-01 07:41:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用on page函数和页面模板为文档中的一个子集生成页眉:

templates.append(PageTemplate(id='Overview', frames=frame, onPage=HeaderOverview))

此模板的标题函数:

^{pr2}$

这很好用,除了传递的course变量(它随节中的每个页面而变化)是序列中的最后一个变量,因为这个函数直到最终构建才真正被调用(我想这就是它的工作原理)。我需要的是这样做,这样值就是页面上的值。如果我能边写边画,那也没问题。下面是我的尝试:

####################################################################################
# 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"]))

这不会抛出错误或任何东西,但它似乎也不会实际绘制任何内容。在

谢谢你的帮助!在


Tags: 函数report模板fortopicoverview页面canvas
1条回答
网友
1楼 · 发布于 2024-10-01 07:41:31

还有一个主意。。。在

也许它可以使用特定的可识别的流程来更新课程。如果有必要,可以向流式添加自定义属性以帮助标识它们(请参见thispost)。在

例如,您可以执行以下操作:

...
report.append(some_content)

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

report.append(some_more_content)
...

并设置一些变量:

^{pr2}$

然后,您可以在每个流呈现后检查它是否具有该属性,如果有,则更新当前进程。在

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

doc.afterFlowable = afterFlowable

HeaderOverview将能够使用current_course变量来获得正确的方向,因为在最终构建过程中,HeaderOverview和{}都在不同的点被调用。在

相关问题 更多 >