Python - print blank cells in excel

Asked

Viewed 53 times

-1

Hello, I am developing an automation to check in an excel spreadsheet which customers did not make the payment. The code itself works, but only shows me the last column of the spreadsheet, which in this case would be the last month of the year. Below is code and spreadsheet template:

import openpyxl, smtplib, sys

#Abre a planilha e obtém o status do último pagamento.

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

lastCol = sheet.max_column
latestMonth = sheet.cell(row=1, column=lastCol).value

#Verifica o status de pagamento de cada cliente.

unpaidMembers = {}
for r in range(2, sheet.max_row + 1):
    payment = sheet.cell(row=r, column=lastCol).value
    if payment != 'ok':
        cliente = sheet.cell(row=r, column=1).value
        email = sheet.cell(row=r, column=2).value
        unpaidMembers[cliente] = email
        print('Linha:',r,'Coluna:',lastCol,'Cliente:',cliente,'Email:',email)

https://prnt.sc/utk4ef spreadsheet template used

  • Gustavo, good afternoon! What is the expected operation?

  • Imonferrari, good afternoon! I would like to print all the cells that are not with the 'ok' and not only the last column.

1 answer

0


In your code, you have a for for the lines, only one was missing for for the columns:

import openpyxl, smtplib, sys

#Abre a planilha e obtém o status do último pagamento.

wb = openpyxl.load_workbook('cobranca.xlsx')
sheet = wb['Sheet1']

lastCol = sheet.max_column
latestMonth = sheet.cell(row=1, column=lastCol).value

#Verifica o status de pagamento de cada cliente.

unpaidMembers = {}
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':
        cliente = sheet.cell(row=r, column=1).value
        email = sheet.cell(row=r, column=2).value
        mes = sheet.cell(row=1, column=c).value
        unpaidMembers[cliente] = email
        print('Linha:',r,'Coluna:',c,'Cliente:',cliente,'Email:',email,'Mês:',mes)

I only added a column to indicate the month, but if not necessary just take.

  • mateusalxd, good afternoon! Thank you very much, had not thought to make one for the column.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.