您的位置: 网站首页> openpyxl教程> 当前文章
openpyxl create_sheet创建sheet,指定索引位置创建
老董-我爱我家房产SEO2020-10-29187围观,118赞
openpyxl创建sheet的方法是:create_sheet,官网示例如下
ws1 = wb.create_sheet("Mysheet") # insert at the end (default)
ws2 = wb.create_sheet("Mysheet", 0) # insert at first position
ws3 = wb.create_sheet("Mysheet", -1) # insert at the penultimate position
create_sheet解释
create_sheet(title=None, [index=None]) 返回sheet对象,title代表sheet名字,index代表sheet的位置(从左到右索引依次为0、1、2...),类型为int,该参数可以省略,也可以是负值。注意:-1代表倒数第2(可能因为创建excel的那一刻就有个sheet,就把它作为了倒数第1)。
1、不加index参数,创建a、b两个sheet。
# -*- coding: utf-8 -*-
from openpyxl import Workbook
wb = Workbook()
for name in ['a','b']:
wb.create_sheet(name)
wb.save('test.xlsx')

2、指定位置,创建a、b两个sheet,b在第一的位置,a在倒数第二位置。
# -*- coding: utf-8 -*-
from openpyxl import Workbook
wb = Workbook()
wb.create_sheet('a',index=-1) # -1代表倒数第二
wb.create_sheet('b',index=0)
wb.save('test.xlsx')

很赞哦!
python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群
相关文章
文章评论
-
openpyxl create_sheet创建sheet,指定索引位置创建文章写得不错,值得赞赏


