如何使用 canvas 向 PDF 添加另一页?
How can i add another page to a PDF with canvas?
我开发了一个应用程序,用于根据电子表格列出一些客户的未结费用,并将其保存为 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
您需要检查 for
循环中 y
的值。如果它低于 36(对于 half-inch 个边距),则调用 cnv.showPage()
,重置 y
,并打印一个新的 header.
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
将页面 header 打印机分离到一个单独的函数中留作 reader 的练习。
我开发了一个应用程序,用于根据电子表格列出一些客户的未结费用,并将其保存为 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
您需要检查 for
循环中 y
的值。如果它低于 36(对于 half-inch 个边距),则调用 cnv.showPage()
,重置 y
,并打印一个新的 header.
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
将页面 header 打印机分离到一个单独的函数中留作 reader 的练习。