我有代码:
def add_to_database(object_name, value):
workbook = openpyxl.load_workbook(DATABASE_FILE)
worksheet = workbook[DATABASE_SHEET]
object_row = None
for row in worksheet.iter_rows(min_row = 1, max_row = worksheet.max_row + 1, values_only = True):
if row[0] == object_name:
object_row = row
break
if object_row:
worksheet.cell(row = object_row[0].row, column = 2).value += value
else:
last_row = worksheet.max_row
worksheet.cell(row = last_row, column = 1).value = object_name
worksheet.cell(row = last_row, column = 2).value = value
workbook.save(DATABASE_FILE)
workbook.close()
我得到一个错误:属性错误:“str”对象在行上没有属性“row”:工作表.cell(row = object_row[0].row,column = 2).value += value
我哪里做错了?
1条答案
按热度按时间mdfafbf11#
你试图从一个字符串中获取行值,而这个字符串没有行值,你还需要创建一个循环来考虑行号,这样才能做到这一点。
未经测试,但 * 应该 * 工作。Lmk如果它不。