以下魔法指令生成的结果和代码,在我填入已经打开的excel对象后报错:This object does not support enumeration

from openpyxl.styles import Font, PatternFill
from xbot import print
from typing import *
def compare_and_set_cell_format(workbook, sheet_name, cell1_row, cell1_column, cell2_row, cell2_column):
"""
title: 比较并设置单元格格式
description: 在指定的sheet页中,比较两个单元格的内容,并根据内容是否相同设置不同的单元格的格式。
inputs:
- workbook (Excel对象): 已经打开的Excel对象
- sheet_name (str): sheet页的名称
- cell1_row (int): 第一个单元格的行标
- cell1_column (str): 第一个单元格的列标
- cell2_row (int): 第二个单元格的行标
- cell2_column (str): 第二个单元格的列标
outputs:
- None
"""
sheet = workbook[sheet_name]
cell1 = sheet[f"{cell1_column}{cell1_row}"]
cell2 = sheet[f"{cell2_column}{cell2_row}"]
if cell1.value == cell2.value:
pass
else:
# 设置不同内容的单元格背景色为黄色
fill = PatternFill("solid", fgColor="FFFF00")
cell1.fill = fill
cell2.fill = fill
# 获取两个单元格内容的长度
length1 = len(str(cell1.value))
length2 = len(str(cell2.value))
# 设置不同字符的字体颜色为红色
font = Font(color="FF0000")
# 遍历两个单元格内容的每个字符
for i in range(max(length1, length2)):
if i < length1 and i < length2 and str(cell1.value)[i] != str(cell2.value)[i]:
cell1.font = font
cell2.font = font
elif i >= length1:
cell2.font = font
elif i >= length2:
cell1.font = font
print(f"比较的两个单元格行标列标分别为:{cell1_row}{cell1_column}和{cell2_row}{cell2_column}")
print(f"比较的两个单元格的内容分别为:{cell1.value}和{cell2.value}")
以下是我我期望的效果,可选择前面打开的excel对象直接进行修改,编辑完所有内容后,最后关闭excel,而不是每次还要先关闭前面已经打开的excel,在设置颜色时再打开,这里有循环,频繁打开关闭文件太低效了。请问应该如何修改
