I'm writing some code to automate data entry at work. My issue is that the if statements are not working despite the conditions being met. I've tested the code on two excel files with the exact same data and the statements work with no issues on one file but not the other. There are no errors that appear when run with the file that doesn't work. All other commands have no issues, only the if statements are causing problems.
The only difference I can find with the two files is that one that works is unencrypted and the one that doesn't work is encrypted. I've tested out by unencrypting the file and trying it again, but it still does not work.
Here is a summarized version of what my code looks like:
import pandas as pd
import pyautogui as pya
import xlwings as xw
# Path for encrypted file
PATH = 'D:\filename.xlsx'
wb = xw.Book(PATH, password=pword)
sheet = wb.sheets['sheetname']
df = sheet.used_range.options(pd.DataFrame, index=False, header=True).value
df.head()
if (df['columnname'][row] == healthnum and df['columnname2'][row] == 5):
pya.hotkey ('5')
elif df['columnname'][row] == healthnum and df['columnname2'][row] == 4.4:
pya.hotkey ('4')
elif df['columnname'][row] == healthnum and df['columnname2'][row] == 3.4:
pya.hotkey ('3')
elif df['columnname'][row] == healthnum and df['columnname2'][row] == 2:
pya.hotkey ('2')
elif df['columnname'][row] == healthnum and df['columnname2'][row] == 1:
pya.hotkey ('1')
pya.hotkey('tab')
What's supposed to happen is that if the conditions are met, then the keyboard would input '4' and then tab to the next question. What happens instead is it tabs through all the questions without putting in the data.
CodePudding user response:
I don't have any knowledge about Panda, but I think you should remove the whitespaces
pya.hotkey ('5')
CodePudding user response:
I personally do not know pandas, but in an if statement in regular python, you do not need parentheses.
if df['columnname'][row] == healthnum and df['columnname2'][row] == 5:
pya.hotkey ('5')
There also is the possibility of there being a condition error like if I were to do
x=1
y="hi"
if x == y:
print("hello")
this would cause an error because you could be comparing one type of value to a seperate type.