准备数据:
求和代码:
import openpyxl
wb = openpyxl.load_workbook('C:/Users/Administrator/Desktop/1.xlsx')
ws = wb['test']
min_row = ws.min_row
max_row = ws.max_row
min_col = ws.min_column
max_col = ws.max_column
for row in range(min_row + 1, max_row + 1):
key = ws.cell(row=row, column=max_col).coordinate
# 求和的开始单元格地址
start = ws.cell(row=row, column=min_col + 1).coordinate
# 求和的结束单元格地址
end = ws.cell(row=row, column=max_col - 1).coordinate
ws[key] = f'=SUM({start}:{end})'
wb.save('公式插入.xlsx')
实现效果如下:
标签:openpyxl,min,求和,max,sum,---------------------------,ws,col,row From: https://www.cnblogs.com/cherishthepresent/p/17580255.html