ExcelWriterBuilder写入多sheet
时间: 2024-01-20 15:02:39 浏览: 94
多个sheet的Excel导入
要使用ExcelWriterBuilder写入多个sheet,可以按照以下步骤进行操作:
1. 创建ExcelWriterBuilder对象,指定Excel文件路径和文件名
```python
from jinja2 import Environment, PackageLoader, select_autoescape
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
from openpyxl.styles import Font
# 创建ExcelWriterBuilder对象
wb = Workbook()
ewb = ExcelWriterBuilder(wb, "output.xlsx")
```
2. 创建多个Worksheet对象,并指定名称
```python
# 创建多个Worksheet对象
ws1 = wb.active
ws1.title = "Sheet1"
ws2 = wb.create_sheet("Sheet2")
ws3 = wb.create_sheet("Sheet3")
```
3. 分别写入数据到不同的Worksheet对象
```python
# 写入数据到不同的Worksheet对象
for row in range(1, 11):
ws1.append([f'Row {row}', 'Data'])
for row in range(1, 6):
ws2[f'A{row}'] = f'Row {row}'
ws2[f'B{row}'] = 'Data'
for col in range(1, 11):
col_letter = get_column_letter(col)
ws3[f'{col_letter}1'] = f'Column {col}'
for row in range(2, 6):
ws3[f'{col_letter}{row}'] = f'Row {row}, Col {col}'
```
4. 保存Excel文件
```python
# 保存Excel文件
ewb.save()
```
这样就可以使用ExcelWriterBuilder写入多个sheet了。记得在写入数据之前先创建好Worksheet对象,并指定名称。
阅读全文