这里的新Python用户非常沮丧。我曾经在某一时刻运行代码,然后继续执行其他一些操作,但现在不起作用。
这是通过.xlsx文件的“ J”列中的非空白单元格循环的。对于包含文本的单元格,它将查看“ A”列中的日期。如果A列中的日期等于将来的7天,请打印“您该轮班了”。
非常烦恼,因为它在某一时刻起作用,我不知道哪里出了问题。
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中的一个字符串却把它扔掉了。因此,我需要继续学习并检查值是否首先是日期,如果不是,请忽略。
关于python - Python-遍历非空白Excel单元格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38885466/