0

Why is iter_rows always saying must be str, not int? I am simply trying to pass my lst values to min_row and max_row.

from openpyxl import load_workbook
from itertools import islice

wb = load_workbook('xyz.xlsx')
ws1 = wb['Sheet1']

lst = ['2','2']
limit = 2

for i in islice(lst,limit):
    row = ws1.iter_rows(min_row=i,max_row=i)

I've tried casting ideas found here to iter_rows min_row max_row but, I just get the same error and Worksheet object has no attribute

Trackback error is

 line 509, in iter_rows
 max_row += row_offset
SpaceCadet
  • 212
  • 4
  • 14
  • 1
    er, what do you want to do here? Why `itertools`? Add a print statement to see what you're actually trying to pass to `ws.iter_rows()` – Charlie Clark Nov 10 '17 at 18:16
  • I add print(i) to above for statement before and it prints 2 then same Traceback error. I add print(i) after for statement and it prints nothing and gives same TraceBack error. I am trying to end up with min_row=2, max_row=2. print(ws1.iter_rows) gives > I hope this makes more sense and thanks for your inquiry. – SpaceCadet Nov 10 '17 at 20:41

1 Answers1

1

The iter_rows method expects that the min_row and max_row input parameters are integers. So try this:

from openpyxl import load_workbook

from itertools import islice

wb = load_workbook('xyz.xlsx')
ws1 = wb['Sheet1']

lst = [2,2]
limit = 2

for i in islice(lst,limit):
    row = ws1.iter_rows(min_row=i,max_row=i)

Note: you can get the complete usage instructions for the iter_rows method by querying its docstring with command help(ws1.iter_rows) or help(openpyxl.worksheet.worksheet.Worksheet.iter_rows). Within the docstring you can find the instructions:

:param min_row: smallest row index (1-based index)
:type min_row: int

:param max_row: smallest row index (1-based index)
:type max_row: int
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Xukrao
  • 8,003
  • 5
  • 26
  • 52
  • Thanks for help, but if you run your code it still gives error `must be str, not int`. It sounds counter to what the documentation mentions, but nonetheless that's what the interpreter throws. If you put a number in `min_row` and `max_row` explicitly it will work, but something about using a `list` I'm having trouble on. – SpaceCadet Nov 10 '17 at 20:34
  • I tested this again and it worked when I printed i. However, in my program still some issues despite trying this. I will investigate more and thank you for your contribution and help to my issue. – SpaceCadet Nov 10 '17 at 22:55
  • 1
    @SpaceCadet The error message `must be str, not int` that is generated by the `iter_rows` method code line `max_row += row_offset` is a reference to the `row_offset` variable and *not* the `max_row` variable. Basically what happens is that python runs into trouble because it's being forced to add an integer object (`row_offset`) to a string object (`max_row`). The Python error handler then makes the assumption that the intention for the code line was to do a string addition operation, hence this error message. – Xukrao Nov 11 '17 at 19:19