如何使用openpyxl 1.6遍历特定列中的单元格

我目前正在使用Python 2.7,到目前为止,已经能够做到这一点。 我需要打印列D中的单元格的值,直到excel表格的最大行。

wb = openpyxl.load_workbook(filename) sheet = wb.get_sheet_by_name('Something') for row in range(2, sheet.get_highest_row()): bla=str(row) status = sheet['D' + bla].value() print status 

这给了回溯

 Traceback (most recent call last): File "ExcelAnalysis.py", line 25, in <module> status = sheet['D' + bla].value() TypeError: 'Worksheet' object has no attribute '__getitem__' 

我怀疑你正在使用一个过时的版本的openpyxl ,更新它:

 pip install --upgrade openpyxl 

而且,你不应该叫value ,这是一个财产:

 status = sheet['D' + bla].value 

我认为这是一个非常原始的,并不是非常优化的方式,但是我得到了答案。

 for cow in range(1, sheet.get_highest_row()): status = sheet.cell(row=cow, column=3).value 

请,如果有人能帮助我改进它。 非常感谢你提前。

在这个页面上看到更多