python接口返回值怎么写入excel单元格
时间: 2023-11-10 07:10:10 浏览: 81
【Python源码】Excel单元格的写入
要将Python的接口返回值写入Excel单元格,可以使用Python中的第三方库,例如openpyxl或xlwt。
1. 使用openpyxl库:
```python
import openpyxl
# 打开Excel文件
workbook = openpyxl.load_workbook('test.xlsx')
# 选取工作表
worksheet = workbook.active
# 写入数据
worksheet.cell(row=1, column=1, value='Hello')
worksheet.cell(row=1, column=2, value='World')
# 写入接口返回值
response = {'key1': 'value1', 'key2': 'value2'}
row = 2
for key, value in response.items():
worksheet.cell(row=row, column=1, value=key)
worksheet.cell(row=row, column=2, value=value)
row += 1
# 保存Excel文件
workbook.save('test.xlsx')
```
2. 使用xlwt库:
```python
import xlwt
# 创建一个Excel文件
workbook = xlwt.Workbook()
# 创建一个工作表
worksheet = workbook.add_sheet('Sheet1')
# 写入数据
worksheet.write(0, 0, 'Hello')
worksheet.write(0, 1, 'World')
# 写入接口返回值
response = {'key1': 'value1', 'key2': 'value2'}
row = 1
for key, value in response.items():
worksheet.write(row, 0, key)
worksheet.write(row, 1, value)
row += 1
# 保存Excel文件
workbook.save('test.xls')
```
阅读全文