ReportLab 的页眉和页脚

header and footer with reportlab

提问人:thiago tavares 提问时间:11/7/2023 更新时间:11/7/2023 访问量:17

问:

我的代码有问题:

from reportlab.platypus import SimpleDocTemplate, PageTemplate, Frame, Paragraph, PageBreak
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet

def header(canvas, doc):
    canvas.saveState()
    canvas.setFont('Helvetica', 12)
    canvas.drawString(36, letter[1] - 36, "CABEÇA")
    canvas.restoreState()

def footer(canvas, doc):
    canvas.saveState()
    canvas.setFont('Helvetica', 12)
    canvas.drawString(36, 36, "PÉZÃO")
    canvas.restoreState()

pdf_filename = "exemplo.pdf"

doc = SimpleDocTemplate(pdf_filename, pagesize=letter)

styles = getSampleStyleSheet()
normal_style = styles["Normal"]

frame = Frame(36, 36, letter[0] - 72, letter[1] - 72)
template = PageTemplate(id='page_with_header_footer', frames=frame, onPage=header, onPageEnd=footer)

doc.addPageTemplates([template])

story = []

for i in range(10):
    story.append(Paragraph(f"Conteúdo da Página {i + 1}", normal_style))
    story.append(PageBreak())

story.pop()

doc.build(story)

我正在尝试使用 reportlab 生成一个文档,其中所有页面都带有页眉和页脚,但是,只有我的第一页带有页眉和页脚。我不知道还能做什么:C

python pdf 眉页 reportlab

评论

0赞 thiago tavares 11/7/2023
从 reportlab.pdfgen 导入 canvas def MultiPage(): c = canvas。Canvas('TESTE.pdf') for i in range(5): page_num = c.getPageNumber() text = “Essa página é a %s” % page_num c.drawString(100,50,text) c.showPage() c.save() if name == “main”: MultiPage() Thias 代码创建 5 页,页脚上带有字符串 'Essa página é a %s'!

答: 暂无答案