SpaceCadet
SpaceCadet

Reputation: 272

Openpyxl iter_rows(min_row,max_row) with loop

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

Upvotes: 0

Views: 6181

Answers (1)

Xukrao
Xukrao

Reputation: 8634

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

Upvotes: 1

Related Questions