python读取sheet名_openpyxl获取所有sheet名字的2种方法

获取sheet名字有两种方法,看官网的说明:

You can review the names of all worksheets of the workbook with the Workbook.sheetname attribute

print(wb.sheetnames)

You can loop through worksheets

for sheet in wb:

print(sheet.title)

第1种是通过wb的sheetnames属性获取所有sheet名字,以列表的形式返回;

第2种是通过sheet对象的title属性来获取

实际上有了sheet名字,我们就可以用名字来获取sheet对象。(下一篇文章为openpyxl获取sheet对象)

获取sheet名字的代码如下:

# -*- coding: utf-8 -*-

from openpyxl import Workbook

wb = Workbook() # 默认生成一个名为Sheet的sheet

for name in ['a','b']:

wb.create_sheet(name)

# 直接打印sheet名字

print(wb.sheetnames)

# 通过sheet对象的title属性

for sheet in wb:

print('sheet名:',sheet.title)

wb.save('test.xlsx')

D:python3installpython.exe D:/pyscript/py3script/python66/test2/test.py

['Sheet', 'a', 'b']

sheet名: Sheet

sheet名: a

sheet名: b

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值