Martin Noah
Martin Noah

Reputation: 167

Python - Loop through non-blank Excel cells

Very frustrated new Python user here. I had code running at one point then went on to do some other things and now it's not working.

This is looping through non-blank cells in the 'J' column of an .xlsx file. For the cells that contain text, it looks at the date in column 'A'. If the date in column A is equal to 7 days in the future, print "You're due for a shift."

Very frustraing because it was working at one point and I don't know where it went wrong.

workbook = load_workbook('FRANKLIN.xlsx', data_only=True)
ws=workbook.active

cell_range = ws['j1':'j100'] #Selecting the slice of interest

for row in cell_range: # This is iterating through rows 1-7
for cell in row: # This iterates through the columns(cells) in that row
        value = cell.value
        if cell.value:
           if cell.offset(row=0, column =-9).value.date() == (datetime.now().date() + timedelta(days=7)):
                print("you're due for a shift")

I am getting this error for the 2nd to last line.

"AttributeError: 'str' object has no attribute 'date'"

Upvotes: 1

Views: 1855

Answers (1)

Martin Noah
Martin Noah

Reputation: 167

Nevermind...I had a blank value in J3 with a string in A3 that was throwing it off. So I need to keep learning and check to see if a value is a date first and if not, ignore.

Upvotes: 1

Related Questions