这里的新 Python 用户非常沮丧。我曾经运行过代码,然后继续执行其他一些操作,但现在它不起作用。
这是循环遍历 .xlsx 文件“J”列中的非空白单元格。对于包含文本的单元格,它会查看“A”列中的日期。如果 A 列中的日期等于 future 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")
我在倒数第二行收到此错误。
“AttributeError:'str'对象没有属性'date'”
最佳答案
没关系...我在 J3 中有一个空白值,在 A3 中有一个字符串,导致它失效。所以我需要继续学习并首先检查一个值是否是日期,如果不是,则忽略。
关于Python - 循环遍历非空白 Excel 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38885466/