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

你可能感兴趣的:(python读取sheet名)