【python】Call to deprecated function get_sheet_names / get_sheet_by_name / remove_sheet

〇、前情提要

在学小甲鱼的使用Python读写Excel文件时,遇到了如下警告:

DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).

DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).

DeprecationWarning: Call to deprecated function remove_sheet (Use wb.remove(worksheet) or del wb[sheetname]).

参考:

  1. 【办公篇】《极客Python之效率革命》(小甲鱼)https://www.bilibili.com/video/av23697305/?p=1
  2. [办公] 使用Python读写Excel文件(2)
    https://fishc.com.cn/thread-102046-1-1.html

一、报错解释

DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).
不推荐警告:调用已弃用的函数get_sheet_names(请使用wb.sheetnames)
DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).
不推荐警告:调用已弃用的函数get_sheet_by_name(请使用wb[sheetname])
DeprecationWarning: Call to deprecated function remove_sheet (Use wb.remove(worksheet) or del wb[sheetname]).
不推荐警告:调用已弃用的函数remove_sheet(请使用Use wb.remove(worksheet) 或者 del wb[sheetname])

二、解决

get_sheet_names改用wb.sheetnames

在这里插入图片描述

get_sheet_by_name改用wb[sheetname]

在这里插入图片描述

>>> import openpyxl
>>> wb = openpyxl.load_workbook(r"你的excel文件路径")

>>> print(type(wb))
<class 'openpyxl.workbook.workbook.Workbook'>
>>> wb.get_sheet_names()

Warning (from warnings module):
  File "__main__", line 1
DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).
['Sheet']
>>> print(wb.sheetnames)
['Sheet']
>>> ws = wb.get_sheet_by_name('Sheet')

Warning (from warnings module):
  File "__main__", line 1
DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).
>>> ws=wb["Sheet"]
>>> ws
<Worksheet "Sheet">
>>> 
remove_sheet改用Use wb.remove(worksheet) 或者 del wb[sheetname]
>>> wb.remove_sheet(wb["FishC Demo"])

Warning (from warnings module):
  File "__main__", line 1
DeprecationWarning: Call to deprecated function remove_sheet (Use wb.remove(worksheet) or del wb[sheetname]).

>>> wb.remove_sheet(wb["FishC Demo"])


>>> print(wb.sheetnames)
['Sheet']
  • 18
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值