如何从电子表格的值创建一堆 PDF?
How to create a bunch of PDFs from the values of a spreadsheet?
我正在开发一个应用程序来自动发布服务收据,挖掘互联网我设法用 canvas
库生成 PDF。代码工作正常,它访问电子表格,用数据创建列表,然后是 PDF 问题的一部分,当涉及到那部分时,这是我的问题。它只生成一个 PDF,来自电子表格中的最后一个客户,而不是来自电子表格中的每个人。
按照下面的代码:
from reportlab.lib.pagesizes import letter, A4
from datetime import date, datetime, timezone, timedelta
import openpyxl
# Opens the spreadsheet and obtains the status of the last payment.
wb = openpyxl.load_workbook('./lib/cobranca3.xlsx')
sheet = wb['Sheet1']
lastCol = sheet.max_column
# Checks the payment status of each customer.
unpaidMembers = {}
codigos = []
clients = []
cnpjs = []
emails = []
vencimentos = []
valores = []
acrescimos = []
antecipacoes = []
descontos = []
months = []
for r in range(2, sheet.max_row + 1):
for c in range(13, lastCol + 1):
payment = sheet.cell(row=r, column=c).value
if payment != 'ok' or '':
codigo = sheet.cell(row=r, column=1).value
cliente = sheet.cell(row=r, column=2).value
cnpj = sheet.cell(row=r, column=3).value
email = sheet.cell(row=r, column=4).value
venc = sheet.cell(row=r, column=6).value
valor = sheet.cell(row=r, column=7).value
acresc = sheet.cell(row=r, column=8).value
antec = sheet.cell(row=r, column=9).value
desc = sheet.cell(row=r, column=10).value
month = sheet.cell(row=1, column=c).value
codigos.append(codigo)
clients.append(cliente)
cnpjs.append(cnpj)
emails.append(email)
vencimentos.append(venc)
valores.append(valor)
months.append(month)
unpaidMembers[cliente] = email
print('Line:', r, 'Column:', c, 'Código:', codigo, 'Client:', cliente, 'CNPJ:', cnpj, 'Email:', email, 'Vencimento:', venc, 'Valor:', (valor + acresc) - antec - desc, 'Month:', month)
#print('dictionary created successfully')
for r in range(1, sheet.max_row + 1):
# Creating Canvas
#c = canvas.Canvas("invoice.pdf", pagesize=A4, bottomup=0)
c = canvas.Canvas(str(codigo) + '_' + 'PS.pdf', pagesize=A4, bottomup=0)
data_atual = date.today()
data_atual_formatada = '{}/{}/{}'.format(data_atual.day, data_atual.month, data_atual.year)
# Setting the font for Name title of company
c.setFont("Helvetica-Bold", 20)
# Inserting the name of the company
c.drawCentredString(295, 50, "NOTA DE SERVIÇOS PRESTADOS")
# linha de separação
c.line(10, 75, 580, 75) # começa em, tem altura de, comprimento, altura
# Setting the font
c.setFont("Helvetica-Bold", 12)
c.drawString(10, 110, "Código: %s" % codigo)
c.drawString(200, 110, "Cliente: %s" % cliente)
c.drawString(10, 140, "Data Emissão: %s" % data_atual_formatada)
c.drawString(200, 140, "Data vencimento: %s" % month)
# linha de separação
c.line(10, 165, 580, 165) # começa em, tem altura de, comprimento, altura
c.drawString(10, 190, "Pagamento referente à:")
# Setting the font
c.setFont("Helvetica-Bold", 15)
c.drawString(35, 220, "Honorário: ")
c.drawString(120, 220, "%s" % month)
# Setting the font
c.setFont("Helvetica-Bold", 12)
c.drawString(35, 250, "Valor: ")
c.drawString(500, 250, "R$ %s" % valor)
c.drawString(35, 280, "Acréscimo: ")
c.drawString(500, 280, "R$ %s" % acresc)
c.drawString(35, 310, "Antecipação: ")
c.drawString(500, 310, "R$ %s" % antec)
c.drawString(35, 340, "Desconto: ")
c.drawString(500, 340, "R$ %s" % desc)
# Setting the font
c.setFont("Helvetica-Bold", 15)
total = (valor + acresc) - antec - desc
#print(total)
c.drawString(35, 400, "TOTAL: ")
c.drawString(500, 400, "R$ %s" % total)
# linha de separação
c.line(10, 425, 580, 425) # começa em, tem altura de, comprimento, altura
c.drawString(125, 475, "BOLETO EMITIDO PELO BANCO SANTANDER")
c.drawString(80, 500, "Omnia Tecnologia Digital Eireli - CNPJ 10.751.327/0001-59")
# End the Page and Start with new
c.showPage()
# Saving the PDF
c.save()
使用了以下工作表:https://prnt.sc/13ii4ja
似乎 canvas c
是在 for 循环的每次迭代中从 canvas.Canvas(str(codigo), ...)
创建的,但是 codigo
在迭代之间不会改变。所以我认为你只是一遍又一遍地覆盖一个文件,你只能看到你在文件系统中创建的最后一个 PDF。
我正在开发一个应用程序来自动发布服务收据,挖掘互联网我设法用 canvas
库生成 PDF。代码工作正常,它访问电子表格,用数据创建列表,然后是 PDF 问题的一部分,当涉及到那部分时,这是我的问题。它只生成一个 PDF,来自电子表格中的最后一个客户,而不是来自电子表格中的每个人。
按照下面的代码:
from reportlab.lib.pagesizes import letter, A4
from datetime import date, datetime, timezone, timedelta
import openpyxl
# Opens the spreadsheet and obtains the status of the last payment.
wb = openpyxl.load_workbook('./lib/cobranca3.xlsx')
sheet = wb['Sheet1']
lastCol = sheet.max_column
# Checks the payment status of each customer.
unpaidMembers = {}
codigos = []
clients = []
cnpjs = []
emails = []
vencimentos = []
valores = []
acrescimos = []
antecipacoes = []
descontos = []
months = []
for r in range(2, sheet.max_row + 1):
for c in range(13, lastCol + 1):
payment = sheet.cell(row=r, column=c).value
if payment != 'ok' or '':
codigo = sheet.cell(row=r, column=1).value
cliente = sheet.cell(row=r, column=2).value
cnpj = sheet.cell(row=r, column=3).value
email = sheet.cell(row=r, column=4).value
venc = sheet.cell(row=r, column=6).value
valor = sheet.cell(row=r, column=7).value
acresc = sheet.cell(row=r, column=8).value
antec = sheet.cell(row=r, column=9).value
desc = sheet.cell(row=r, column=10).value
month = sheet.cell(row=1, column=c).value
codigos.append(codigo)
clients.append(cliente)
cnpjs.append(cnpj)
emails.append(email)
vencimentos.append(venc)
valores.append(valor)
months.append(month)
unpaidMembers[cliente] = email
print('Line:', r, 'Column:', c, 'Código:', codigo, 'Client:', cliente, 'CNPJ:', cnpj, 'Email:', email, 'Vencimento:', venc, 'Valor:', (valor + acresc) - antec - desc, 'Month:', month)
#print('dictionary created successfully')
for r in range(1, sheet.max_row + 1):
# Creating Canvas
#c = canvas.Canvas("invoice.pdf", pagesize=A4, bottomup=0)
c = canvas.Canvas(str(codigo) + '_' + 'PS.pdf', pagesize=A4, bottomup=0)
data_atual = date.today()
data_atual_formatada = '{}/{}/{}'.format(data_atual.day, data_atual.month, data_atual.year)
# Setting the font for Name title of company
c.setFont("Helvetica-Bold", 20)
# Inserting the name of the company
c.drawCentredString(295, 50, "NOTA DE SERVIÇOS PRESTADOS")
# linha de separação
c.line(10, 75, 580, 75) # começa em, tem altura de, comprimento, altura
# Setting the font
c.setFont("Helvetica-Bold", 12)
c.drawString(10, 110, "Código: %s" % codigo)
c.drawString(200, 110, "Cliente: %s" % cliente)
c.drawString(10, 140, "Data Emissão: %s" % data_atual_formatada)
c.drawString(200, 140, "Data vencimento: %s" % month)
# linha de separação
c.line(10, 165, 580, 165) # começa em, tem altura de, comprimento, altura
c.drawString(10, 190, "Pagamento referente à:")
# Setting the font
c.setFont("Helvetica-Bold", 15)
c.drawString(35, 220, "Honorário: ")
c.drawString(120, 220, "%s" % month)
# Setting the font
c.setFont("Helvetica-Bold", 12)
c.drawString(35, 250, "Valor: ")
c.drawString(500, 250, "R$ %s" % valor)
c.drawString(35, 280, "Acréscimo: ")
c.drawString(500, 280, "R$ %s" % acresc)
c.drawString(35, 310, "Antecipação: ")
c.drawString(500, 310, "R$ %s" % antec)
c.drawString(35, 340, "Desconto: ")
c.drawString(500, 340, "R$ %s" % desc)
# Setting the font
c.setFont("Helvetica-Bold", 15)
total = (valor + acresc) - antec - desc
#print(total)
c.drawString(35, 400, "TOTAL: ")
c.drawString(500, 400, "R$ %s" % total)
# linha de separação
c.line(10, 425, 580, 425) # começa em, tem altura de, comprimento, altura
c.drawString(125, 475, "BOLETO EMITIDO PELO BANCO SANTANDER")
c.drawString(80, 500, "Omnia Tecnologia Digital Eireli - CNPJ 10.751.327/0001-59")
# End the Page and Start with new
c.showPage()
# Saving the PDF
c.save()
使用了以下工作表:https://prnt.sc/13ii4ja
似乎 canvas c
是在 for 循环的每次迭代中从 canvas.Canvas(str(codigo), ...)
创建的,但是 codigo
在迭代之间不会改变。所以我认为你只是一遍又一遍地覆盖一个文件,你只能看到你在文件系统中创建的最后一个 PDF。