简体   繁体   中英

How do I iterate through cells in a specific column using openpyxl 1.6

I am currently using Python 2.7 and so far have been able to make it this far. I need to print the value of cells in column D till the maximum row the excel sheet has.

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

Which gives a traceback

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

I suspect you are using an outdated version of openpyxl , update it:

pip install --upgrade openpyxl

And, you should not be calling value , it is a property:

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

I think this is a very primitive and not very optimized way, but I got the answer nevertheless.

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

Please, If anyone can, help me improve it. Thank you very much in advance.

See more on this page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM