admin管理员组

文章数量:1566354

excel表格式: 表格名字,公式测试.xlsx

姓名数学英语语文体育总分
徐0过86818235
徐1过78898735
徐2过77738935
徐3过74717235
徐4过81827235
徐5过75877935
徐6过83867935
徐7过88847135
徐8过84848435
徐9过79777135

要求:通过openpyxl实现对表的行求和,将结果写到总分的列中

代码;

import  openpyxl
wb = openpyxl.load_workbook('公式测试.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')

工作表单元格的属性:

一、单元格属性

1、ws.cell(column=2,row=9).column_letter : 输出列字母号:B

2、ws.cell(column=2,row=9).coordinate 输出单元格地址:B9

3、ws.cell(column=2,row=9).col_idx 输出的是单元格的列号是数字,2

4、ws['A4'].row 拿到的行

5、ws['A7'].column 拿到列号

6、ws['B7'].value 拿到数值

ws['D4']=f'=SUM(A4:C4)'  

本文标签: 公式单元格openpyxlExcel