Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with openpyxl. making a list out of a column

I am working with a big excel file. I am using

wb = load_workbook(filename='my_file.xlsx')
ws = wb['Sheet1']

I don't want to alter the worksheet in any way. I just want to take data from several columns and work with them. My understanding is that I can't just call a column and use .tolist() because all the values are stored in excel.

like image 264
AK9309 Avatar asked Dec 15 '22 11:12

AK9309


2 Answers

Bernie's answer, I think, was for a slightly older version of OpenPyxl. Worksheet.columns no longer returns tuples but rather, is a generator. The new way to access a column is Worksheet['AlphabetLetter'].

So the rewritten code is:

mylist = []
for col in ws['A']:
     mylist.append(col.value)
like image 140
shivavelingker Avatar answered Jan 05 '23 22:01

shivavelingker


Based on your comment here's one thing you can do:

mylist = []
for col in ws.columns[0]:
    mylist.append(col.value)
like image 29
mechanical_meat Avatar answered Jan 05 '23 23:01

mechanical_meat