python – Reportlab:来自页面的数据的标题

我正在使用页面上的函数和页面模板为我的文档中的页面子集创建标题:

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

这很好用,除了传递的过程变量(随着节中的每个页面而变化)是序列中的最后一个变量,因为这个函数在最终构建之前并没有真正被调用(我认为它是如何工作的).我需要的是这样做,以便值是页面上的值.如果我可以在编写页面时绘制它,那也没关系.这是我的尝试:

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

这不会引发错误或任何事情,但它似乎也没有实际绘制任何东西.

谢谢您的帮助!

解决方法:

这是另一个想法……

也许它可以使用可识别的特定flowable来更新课程.如有必要,您可以将自定义属性添加到flowables以帮助识别它们(请参阅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)

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

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

doc.afterFlowable = afterFlowable

HeaderOverview将能够使用current_course变量来获得正确的路线,因为在最终构建期间,HeaderOverview和afterFlowable都会在各个点被调用.

上一篇:如何让子弹直接出现在python的reportlab包中缩进列表的文本旁边?


下一篇:LadonGo实现菜刀连接webshell一句话执行cmd代码