生成excel商品信息(id,描述,数量,品类)
时间: 2023-08-04 22:33:17 浏览: 69
好的,下面是一个简单的示例代码,使用 `openpyxl` 库来生成 Excel 文件:
```python
from openpyxl import Workbook
# 创建 Excel 工作簿和工作表
wb = Workbook()
ws = wb.active
# 设置表头
ws.append(['id', 'description', 'quantity', 'category'])
# 设置商品信息
products = [
{'id': 1, 'description': 'product1', 'quantity': 10, 'category': 'category1'},
{'id': 2, 'description': 'product2', 'quantity': 20, 'category': 'category2'},
{'id': 3, 'description': 'product3', 'quantity': 30, 'category': 'category1'},
{'id': 4, 'description': 'product4', 'quantity': 40, 'category': 'category2'},
{'id': 5, 'description': 'product5', 'quantity': 50, 'category': 'category1'},
]
# 将商品信息写入表格中
for product in products:
ws.append([product['id'], product['description'], product['quantity'], product['category']])
# 保存 Excel 文件
wb.save('products.xlsx')
```
上述代码只是一个示例,具体实现方式可能因具体情况而异,需要根据实际需求进行调整。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)