您的位置: 网站首页> openpyxl教程> 当前文章

openpyxl获取所有sheet名字的2种方法

老董-我爱我家房产SEO2020-10-29195围观,130赞

  获取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编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会

文章评论

    openpyxl获取所有sheet名字的2种方法文章写得不错,值得赞赏

站点信息

  • 网站程序:Laravel
  • 客服微信:a772483200