openpyxl,给单元格插入公式,求和通过excel公式实现
生活随笔
收集整理的這篇文章主要介紹了
openpyxl,给单元格插入公式,求和通过excel公式实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
excel表格式: 表格名字,公式測試.xlsx
| 姓名 | 數學 | 英語 | 語文 | 體育 | 總分 |
| 徐0過 | 86 | 81 | 82 | 35 | |
| 徐1過 | 78 | 89 | 87 | 35 | |
| 徐2過 | 77 | 73 | 89 | 35 | |
| 徐3過 | 74 | 71 | 72 | 35 | |
| 徐4過 | 81 | 82 | 72 | 35 | |
| 徐5過 | 75 | 87 | 79 | 35 | |
| 徐6過 | 83 | 86 | 79 | 35 | |
| 徐7過 | 88 | 84 | 71 | 35 | |
| 徐8過 | 84 | 84 | 84 | 35 | |
| 徐9過 | 79 | 77 | 71 | 35 |
要求:通過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_columnfor 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).coordinatews[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)'??
總結
以上是生活随笔為你收集整理的openpyxl,给单元格插入公式,求和通过excel公式实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stm32c6t6硬件iic接口的使用-
- 下一篇: 这个冬季没有雪