我正在使用 openpyxl 将 sql 查询结果写入 Excel 文件。如果我将其写入 B 列。如何确保下次运行此脚本时它开始写入 B 列?我尝试在sheet.cell表达式中使用.max_column,但它的作用是写入类似“E”列
A B C D E
1 Chris
2 5'11
3 215lbs
4 26Yrs
5
6
脚本:
def writeToExcel(1_payments_l,2_payments_l,5_payments_l):
r_1 = 20
c_1 = 12
r_2 = 37
c_2 = 12
r_5 = 47
c_5 = 12
xfile = openpyxl.load_workbook(fn)
now = datetime.now()
last_month = now.month - 1 if now.month > 1 else 12
last_month = "January February March April May June July August September October November " \
"December".split()[last_month - 1]
sheet = xfile['PPC Cost Per Tran.']
# sheet['L19'].cell = last_month
sheet.cell(row=19, column=sheet.max_column).value = last_month
for 1pymt in 1_payments_l:
sheet.cell(row=r_1, column=sheet.max_column).value = f'{1pymt:,}'
r_1 += 1
for 2pymt in 2_payments_l:
sheet.cell(row=r_2, column=sheet.max_column).value = f'{2pymt:,}'
r_2 += 1
for 5pymt in 5_payments_l:
sheet.cell(row=r_5, column=sheet.max_column).value = f'{5pymt:,}'
r_5 += 1
xfile.save(r'C:\Users\12345\Monthly Indicators Dashboard.xlsx')
return sheet
动漫人物
相关分类