鱼C论坛

 找回密码
 立即注册
查看: 3100|回复: 3

请教:使用sheetnames()为什么报错?

[复制链接]
发表于 2020-2-14 14:00:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import openpyxl
wb = openpyxl.load_workbook(r"C:\Users\Administrator\Desktop\小甲鱼\demo.xlsx")
#获取excel文件中表单名sheet
#print(wb.get_sheet_names())
print(wb.sheetnames())


运行结果:
========= RESTART: C:/Users/Administrator/Desktop/小甲鱼/fish_openpyxl2.py ========
Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/小甲鱼/fish_openpyxl2.py", line 19, in <module>
    print(wb.sheetnames())
TypeError: 'list' object is not callable
>>>

======
import openpyxl
wb = openpyxl.load_workbook(r"C:\Users\Administrator\Desktop\小甲鱼\demo.xlsx")
#获取excel文件中表单名sheet
print(wb.get_sheet_names())
#print(wb.sheetnames())

运行结果:
========= RESTART: C:/Users/Administrator/Desktop/小甲鱼/fish_openpyxl2.py ========

Warning (from warnings module):
  File "C:/Users/Administrator/Desktop/小甲鱼/fish_openpyxl2.py", line 18
    print(wb.get_sheet_names())
DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).
['Sheet']
>>>
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-14 14:07:17 | 显示全部楼层
print(wb.get_sheet_names())
print(wb.sheetnames)

运行结果:
========= RESTART: C:/Users/Administrator/Desktop/小甲鱼/fish_openpyxl2.py ========

Warning (from warnings module):
  File "C:/Users/Administrator/Desktop/小甲鱼/fish_openpyxl2.py", line 18
    print(wb.get_sheet_names())
DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).
['Sheet']
['Sheet']
>>>

结论:
推荐使用 wb.sheetnames,但是不能带(),否则报错。
这是怎么理解?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-14 14:43:23 | 显示全部楼层
bewind 发表于 2020-2-14 14:07
print(wb.get_sheet_names())
print(wb.sheetnames)

因为 sheename 是属性,不是方法,不能调用它
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-14 22:19:05 | 显示全部楼层
一个账号 发表于 2020-2-14 14:43
因为 sheename 是属性,不是方法,不能调用它

简单理解,属性是对象自身具有的特性,使用时不用()
方法是可操作的能力,使用时用(),必要时还得给输入参数?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-22 11:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表