日期时间对象单元格更新, 但是,虽然print(cell.value)在迭代每个单元格时都有效,但是当我使用If语句将cell.value直接与String进行比较时,它们的cell.value不会更新。
此外,当我甚至尝试编译代码而直接与字符串对象进行比较时,我从PythonWin获得编码错误,将字符串分配给变量然而不会给我一个错误,但是包含字符串I的单元格希望不再被发现,反正没有打印。打印每个单元格的cell.encoding显示utf-8编码,我在Python 3上。
a = datetime.datetime.strptime(oldDate, "%m/%d/%Y")
wb = openpyxl.load_workbook(pathz, read_only=False)
ws = wb.active
for row in ws.iter_rows():
for cell in row:
#This works fine v
if cell.value == a:
print("Found datetime cell")
cell.value = newDate
#This does not work v
if cell.value == "Hello":
print("Found string cell")
cell.value = "Goodbye"
wb.save(pathz)