0

Good morning,

I am having problems when I tried to access to the value of one cell in an excel. I want to access to all the rows of the column number 8. I think I am doing something wrong with the for.

Code:

import openpyxl

filename="./Export_tiempos.xlsx"
wb=openpyxl.load_workbook(filename)
sheet=wb.worksheets[0]
row=2
for row in sheet:

    SB=sheet.cell(row,8).value
    row+=1
wb.save(filename)

The error is the following: TypeError: '<' not supported between instances of 'tuple' and 'int'

Someone could help me? Thanks in advance.

  • `for row in sheet` gives you each row of cells in the sheet. It certainly doesn't give you an int, which is what the first argument in `sheet.cell` is supposed to be. – khelwood Sep 28 '20 at 13:06
  • There is a confusion here because you have a variable called `row` which initially stores an int (`2`), and you believe it keeps storing an int (because you write `row += 1`); but when you write `for row in sheet:` you have a new variable **with the same name** which is a row object. – Stef Sep 28 '20 at 13:07
  • Does this answer your question? https://stackoverflow.com/questions/38619471/iterate-through-all-rows-in-specific-column-openpyxl – PApostol Sep 28 '20 at 13:07

2 Answers2

0

You might have a better time with .iter_rows():

import openpyxl

filename = "./Export_tiempos.xlsx"
wb = openpyxl.load_workbook(filename)
sheet = wb.worksheets[0]
for value in sheet.iter_rows(min_row=2, min_col=8, max_col=8, values_only=True):
    print(value)
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Thank you for your quick answer. This works for me because this for loop covers all the rows, nevertheless, I do not why it takes the information as it is a tuple, so when I want to separate the information I can not, because it is a tuple. I mean: – Cristina Serrador Sep 28 '20 at 14:13
  • Thank you for your quick answer. This works for me because this for loop covers all the rows, nevertheless, I do not why itassume is a tuple, so when I want to separate the information I can not, because it is a tuple. I mean: ``` for value in sheet.iter_rows(min_row=2, min_col=8, max_col=8, values_only=True): SB=value print(SB) SB_=SB.split(sep=' ') ´´´ The error is the following: AttributeError: 'tuple' object has no attribute 'split'. This only happen when I integrate that in a for loop, If i do only for one cell, this error does not happen. – Cristina Serrador Sep 28 '20 at 14:20
  • Unpack the tuple with the comma operator: `for value, in sheet....` – AKX Sep 28 '20 at 14:40
-1

I believe that when you do for row in sheet, it thinks row is a tuple, meaning you can't directly manipulate it with addition. If you want to change the variable named row, consider changing its name. If you want to modify the row in sheets, write something like row.x += 1

  • Are you suggesting using "`row`" as the iteration variable of the for loop, then modifying it in the body of the loop to go to the next row? This is not how for loops work in python! – Stef Sep 28 '20 at 13:11