























获取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。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。