openpyxl 插入公式
时间: 2023-08-14 14:09:22 浏览: 138
Python openpyxl 插入折线图实例
在openpyxl中,可以使用以下代码来插入公式到Excel文件中:
```python
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')
```
这段代码会打开名为"公式测试.xlsx"的Excel文件,并在名为"test"的工作表中插入求和公式。公式的范围是从第一列的第二行到倒数第二列的当前行。然后,将修改后的工作簿保存为"公式插入.xlsx"。\[2\]
#### 引用[.reference_title]
- *1* *3* [最详细的Excel模块Openpyxl教程(三)-使用公式](https://blog.csdn.net/th1522856954/article/details/116769980)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [openpyxl,给单元格插入公式,求和通过excel公式实现](https://blog.csdn.net/weixin_46371752/article/details/124476589)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文