Python - 'get_sheet_by_name' 错误

标签 python openpyxl

到目前为止,这是我的代码:

import os
import openpyxl
os.chdir('C:\\Python34\\MyPy')
wb=openpyxl.load_workbook('example.xlsx')
wb.get_sheet_names()

但是我得到了这些错误:

Warning (from warnings module):
/File "main", line 1
DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).
['Sheet1', 'Sheet2', 'Sheet3']

最佳答案

警告不是错误 - 它们不会妨碍您的程序运行。如果出现已弃用警告:您使用的功能将在未来版本中删除,因此开发人员将其标记为已弃用。

它现在可能可以工作,但下一个版本可能不再有效,因为此功能已被删除 - 然后您将收到错误消息。

你可以这样修复它:

wb.sheetnames # all names

sheet = wb["UseThisSheet"]  # select a certain sheet by name

for sheet2 in wb:           # or go over all sheets 
    print(sheet2.title)

sh = wb.active             # normally sheet at index 0 if you create an empy one

Source: https://openpyxl.readthedocs.io/en/stable/tutorial.html

这是一个关于如何创建工作簿的简短示例(我对其他一些 xlsx 问题的回答):https://stackoverflow.com/a/48782382/7505395

关于Python - 'get_sheet_by_name' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49159245/

相关文章:

python - Openpyxl 使用优化只写显着增加了 excel 文件的磁盘使用

python - 使用 openpyxl 在 Excel 工作表中的文本字符串中搜索单词

python - 使用 Openpyxl 创建数据透视表

python - python 中是否有广泛使用的 MySQL 包装器/API?

Python-属性错误: module 'mysql' has no attribute 'connector'

python - 如何通过正则表达式过滤 Pandas 中的行

python - 在 openpyxl 中使用优化编写器设置单元格格式和样式

python - 删除 BeautifulSoup 分解后变空的行

python - 错误 "The object invoked has disconnected from its clients"- 使用 python 和 win32com 自动化 IE 8