【问题标题】:How can i add another page to a PDF with canvas?如何使用画布向 PDF 添加另一个页面?
【发布时间】:2021-04-27 19:12:16
【问题描述】:

我开发了一个应用程序,用于根据电子表格列出一些客户的未结费用并将其保存为 PDF。代码完美运行,但是保存为PDF时,如果打开费用数量很大,页面内容会被剪切,但不会添加另一个页面。

下面是代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
import openpyxl
import os

pastaApp = os.path.dirname(__file__)

def createPDF():
    # Opens the spreadsheet and obtains the status of the last payment.

    wb = openpyxl.load_workbook('C:/temp/cobranca.xlsx')
    sheet = wb['Sheet1']

    lastCol = sheet.max_column

    # Checks the payment status of each customer.

    unpaidMembers = {}
    clients = []
    months = []
    emails = []
    for r in range(2, sheet.max_row + 1):
        for c in range(3, lastCol + 1):
            payment = sheet.cell(row=r, column=c).value
            if payment != 'ok':
                client = sheet.cell(row=r, column=1).value
                email = sheet.cell(row=r, column=2).value
                month = sheet.cell(row=1, column=c).value
                clients.append(client)
                months.append(month)
                emails.append(email)
                unpaidMembers[client] = email
                #print('Line:', r, 'Column:', c, 'Client:', client, 'Email:', email, 'Month:', month)
                print('dictionary created successfully')

    cnv = canvas.Canvas(pastaApp+"\\test.pdf", pagesize=letter)
    cnv.drawString(10, 800, "Open Fee")
    cnv.drawString(130, 800, " - Client/Month/E-mail")
    y = 780
    for client, month, email in zip(clients, months, emails):
        cnv.drawString(10, y, client)
        cnv.drawString(170, y, month)
        cnv.drawString(350, y, email)
        y -= 20
        
    cnv.save()

root = Tk()
root.title("Create PDF")

btn_createPDF = Button(root, text="Check", command=createPDF)
btn_createPDF.pack(side="left", padx=10)

root.mainloop()

使用的电子表格模型:https://prnt.sc/125pi9y

【问题讨论】:

  • 这个问题好像和tkinter没什么关系。您可能正在使用 tkinter,但看起来您可以轻松编写不使用 tkinter 的 minimal reproducible example

标签: python canvas reportlab


【解决方案1】:

您需要在for 循环中检查y 的值。如果低于 36(半英寸页边距),则调用 cnv.showPage(),重置 y,然后打印一个新页眉。

    cnv = canvas.Canvas(pastaApp+"\\test.pdf", pagesize=letter)
    cnv.drawString(10, 800, "Open Fee")
    cnv.drawString(130, 800, " - Client/Month/E-mail")
    y = 780
    for client, month, email in zip(clients, months, emails):
        cnv.drawString(10, y, client)
        cnv.drawString(170, y, month)
        cnv.drawString(350, y, email)
        y -= 20
        if y < 36:
            cnv.showPage()
            cnv.drawString(10, 800, "Open Fee")
            cnv.drawString(130, 800, " - Client/Month/E-mail")
            y = 780

将页眉打印机分离成一个单独的函数留给读者作为练习。

【讨论】:

  • 蒂姆·罗伯茨,非常感谢,对我帮助很大。我设法理解并解决了它。
猜你喜欢
  • 2014-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-27
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多