Python – 通过非空白Excel单元循环

非常沮丧的新Python用户在这里。 我有一个代码运行,然后继续做一些其他的事情,现在它不工作。

这是通过.xlsx文件的“J”列中的非空白单元格循环的。 对于包含文本的单元格,它会查看“A”列中的date。 如果A列中的date等于将来7天,则打印出“您将要轮class”。

非常沮丧,因为它一度在工作,我不知道哪里出了问题。

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") 

我得到这个错误的第二到最后一行。

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

没关系…我在J3有一个空白的值,在A3中有一个string被扔掉了。 所以我需要不断的学习和检查,看看一个值是否是第一个date,如果没有,忽略。