实现功能:
创建一个表格文件,并创建且重命名多个sheet,同时为各个sheet填充指定元组中的内容。
源代码:
from openpyxl import Workbook
wb = Workbook()
# sheet标题元组
sheets = (
'sheet1',
'sheet2',
'sheet3'
)
# sheet内容元组
content = (
('11', '22', '33'),
('11', '22', '33'),
('11', '22', '33')
)
# 新建多个sheet,并为sheet重命名,名称为sheet元组中的内容
for title in sheets:
wb.create_sheet(title)
# 逐个切换活动sheet
for sheet_order_number in range(4):
wb._active_sheet_index = sheet_order_number
sheet = wb.active
# 为单个sheet填充content元组中的内容
for content_single in content:
sheet.append(content_single)
# 删除默认sheet
wb._active_sheet_index = 0
sheet_d = wb.active
wb.remove(sheet_d)
# 保存excel
wb.save('test.xlsx')
运行效果:
最新回复