带循环的Openpyxl iter_rows(min_row,max_row)

时间:2017-11-10 17:41:32

标签: python excel loops row openpyxl

为什么iter_rows总是说must be str, not int?我只是想将lst值传递给min_rowmax_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)

我已尝试将here发送给iter_rows min_row max_row的想法,但我得到的错误与Worksheet object has no attribute

相同

引用错误

 line 509, in iter_rows
 max_row += row_offset

1 个答案:

答案 0 :(得分:1)

iter_rows方法期望min_rowmax_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)

注意:您可以通过使用命令iter_rowshelp(ws1.iter_rows)查询其文档字符串来获取help(openpyxl.worksheet.worksheet.Worksheet.iter_rows)方法的完整使用说明。在docstring中,您可以找到说明:

: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