Reputation: 33
I'm developing an application to automate the issuance of service receipts, mining the internet I managed to generate the PDFs with the canvas
library. The code works correctly, it accesses a spreadsheet, creates lists with the data, then comes the part of the PDF issue, when it comes to that part it is my question. It only generates a single PDF, from the last client in the spreadsheet and not from everyone in the spreadsheet.
Follow the code below:
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()
The following worksheet is used:https://prnt.sc/13ii4ja
Upvotes: 0
Views: 63
Reputation: 2910
It seems that the canvas c
is created from canvas.Canvas(str(codigo), ...)
at each iteration in the for loop, but codigo
does not change between iterations. So I think you are just overwriting one file over and over, and you only see the last PDF you created in your filesystem.
Upvotes: 1