>>> import openpyxl>>> wb = openpyxl.load_workbook('.\\省一模考场安排.xlsx')>>> sheet = wb.get_active_sheet()>>> sheet<Worksheet "Sheet1"><Cell 'Sheet1'.E1>>>> sheet.cell(row=1,column=5).value'姓名'>>> sheet.columns[1]** Traceback (most recent call last): File "<pyshell#194>", line 1, in <module> sheet.columns[1]TypeError: 'generator' object is not subscriptable**
运行后,遇到如下的错误,
TypeError: 'generator' object is not subscriptable
问题分析:书本上的代码是基于openpyxl 2.3.3. 后续openpyxl版本对.column的方法的方法已有所改进。
可行的解决方法:
(1) 借助列表的方法, list(sheet.columns)[2]
(2) 借助列字母, sheet[“B”]
以上两种方法得到的数据类型都是元组,<class ‘tuple’>。
联系客服