Skip to content Skip to sidebar Skip to footer

Get Value Of Individual Cell Using Openpyxl

I thought this would have been a pretty simple task, but it is turning out to be much more complicated than I thought it would be. Trying to read a simple excel spreadsheet with a

Solution 1:

Unless you're doing something more complicated than gathering some cells, numpy or pandas is usually unnecessary overhead. openpyxl works well enough on its own.

You have two options for iterating through a worksheet but you're trying to mix them, hence the error.

One option is simply query every cell's value using the cell method of the worksheet object with the row and column keyword arguments. The row and column indexes are 1-based integers, like this:

burndownData.cell(row=1, column=1).value

The other option is iterating the sheet and indexing the row as a list:

forrowin burndownData.iter_rows():
    elemnt =row[0].value

This will get you column A of row 1, column A of row 2, and so on. (because it's an index of a Python list it's zero-based)

What you were doing above:

for i in burndownData.iter_rows():
    elemnt = burndownData.cell(row=i,column=1)

generates an error because i is a tuple of openpyxl Cell objects, and the row argument to cell expects an integer.

Update: I should add there's a third way to reference cells using the spreadsheet column:row syntax, like this:

burndownData['B9'].value

but unless you're only going to select a few specific cells, translating back and forth between letters and numbers seems clumsy.

Post a Comment for "Get Value Of Individual Cell Using Openpyxl"