python 在excel表原有的基础上将字典续存入excel
时间: 2023-09-20 16:05:02 浏览: 154
可以使用Python中的openpyxl库来实现将字典续存入Excel表格的操作。具体步骤如下:
1. 导入openpyxl库
```python
import openpyxl
```
2. 打开Excel表格
```python
workbook = openpyxl.load_workbook('your_excel_file.xlsx')
worksheet = workbook['your_worksheet_name']
```
3. 获取已有数据表格的行数
```python
rows = worksheet.max_row
```
4. 将字典数据添加到Excel表格中
```python
# 假设你的字典数据是 {"name": "Tom", "age": 25}
worksheet.cell(row=rows+1, column=1).value = "Tom"
worksheet.cell(row=rows+1, column=2).value = 25
```
5. 保存Excel表格
```python
workbook.save('your_excel_file.xlsx')
```
完整代码示例:
```python
import openpyxl
workbook = openpyxl.load_workbook('your_excel_file.xlsx')
worksheet = workbook['your_worksheet_name']
rows = worksheet.max_row
data_dict = {"name": "Tom", "age": 25}
worksheet.cell(row=rows+1, column=1).value = data_dict["name"]
worksheet.cell(row=rows+1, column=2).value = data_dict["age"]
workbook.save('your_excel_file.xlsx')
```
阅读全文