How do I inform pro python that an excel cell is blank

Asked

Viewed 98 times

-2

I need to inform python that if the cell "Relation" is in WHITE, it jumps to the next page and if it is filled it fills with the cell content. The code is working normally when the cell is filled, but it does not work if the cell is empty. Follows code below:

             import pyautogui
             import pandas as pd

             formulario = pd.read_excel(r'C:\formulario.xlsx', sheet_name='Planilha1')
             pyautogui.hotkey('alt', 'tab')

             #se "Relação" está em branco, pular pra próxima página

             if formulario['Relação'] [0] == '' :
             pyautogui.hotkey('down')
             pyautogui.hotkey('tab')
             pyautogui.hotkey('enter')
             
             #se relção está preenchida, preencher com os dados contidos e pular pra proxima página.

             elif formulario['Relação'] [0] == 'Mãe/Pai':
             pyautogui.hotkey('tab')
             pyautogui.hotkey('enter')
             pyautogui.sleep(3)
             pyautogui.hotkey('tab')
             pyautogui.write(str(formulario['Sobrenome'] [0]))
             pyautogui.hotkey('tab')
             pyautogui.hotkey('enter')

my excel spreadsheet:

            ```A            B
       1-   Sobrenome     Relação
       2-    Nunes        Mãe/Pai```

Then I would like that when the user left the item "Relation" in WHITE the system simply ignored and jumped to the next page.

NOTE: If I put an empty SPACE in the cell and put the code == ' ':

it works. But with blank cell and code == ': This one doesn’t work at all. I have tried: == None: == Nan: == [''']: == [(''')]: == "": == ('''): == is None:

  • 1

    You put the pandas as tag. Then I ask: Why you are not using in the method to_excel()?

  • I learned using pandas, I don’t know to_excel() how this command would look using it

1 answer

1


Try to use pd isnull.(), this method checks whether the series or dataframe is empty. Returns True if it is and False otherwise.

Take an example:

import pandas as pd

data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', pd.NA]}
df = pd.DataFrame.from_dict(data)

if pd.isnull(df['col_2'][3]): # Verifica se pd.isnull() retornou True
    print('Está vazio.')
else:
    print('Está preenchido.')

The return of this example will be a print describing the status of the object passed to the method pd isnull.().

  • Speechless... I stayed from 10 am until 17 am caught in this...rs I saw several videos and foruns and nothing solved nobody mentioned this pd.isnull() Thank you very much!!!! Unlocked KKK...

Browser other questions tagged

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