循环遍历非空白Excel单元格



Python新用户非常沮丧。我的代码在某个点上运行,然后继续做其他事情,现在它不工作了。

这是在.xlsx文件的'J'列中遍历非空白单元格。对于包含文本的单元格,它查看列'A'中的日期。如果A栏中的日期等于未来7天,打印"You're due for A shift"。

非常令人沮丧,因为它曾经工作过,我不知道哪里出了问题。

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

从第2行到最后一行,我得到了这个错误。

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

没关系…我在J3中有一个空白值,在A3中有一个字符串,它把它扔掉了。所以我需要继续学习并检查一个值是否首先是日期,如果不是,忽略。

最新更新