如何在单元格(范围)中放置“ i”

时间:2018-12-04 16:46:49

标签: python excel openpyxl

在下面的代码中,我有2个问题:

1)在ws ['B3:B7']范围内,我必须编写规则直到列/行的末尾直到空单元格 ws ['B3 :??']
2)在while循环中,我必须在单元格中放入'i',还必须编写规则直到列/行的末尾直到空单元格 ws ['D3:??']

我的代码显示错误-> builtins.AttributeError:ws ['D3:D7'] = i行中的'tuple'对象没有属性'value'

我不明白为什么range被视为元组以及如何解决此错误?

from openpyxl import load_workbook
import datetime
from dateutil.relativedelta import relativedelta

wb = load_workbook('Test.xlsx')
ws = wb.active

values = [row[0].value for row in ws['B3:B7']]
a = datetime.datetime.date(min(values))
b = datetime.datetime.date(max(values))

i = a

while i <= b:
    ws['D3:D7'] = i
    i = i + relativedelta(months = 1)


wb.save('Test2.xlsx')

0 个答案:

没有答案