Question

I am trying to sift through tons of worthless data, and I want to give the user the opportunity to set the offset herself. The the code ultimately ends up looking like:

master_rows = self.worksheet.iter_rows(row_offset=10000)

However upon calling next(master_rows)[0], the output turns out:

RawCell(row=1, column='A', coordinate='A1', [...] )

Basically, the offset seems to be completely ignored, it always starts from 0. Am I doing something wrong?

Was it helpful?

Solution

According to the source code, if you don't pass range_string, then iter_rows will read all cols and rows in the sheet. In other words, row_offset will take effect if range_string is provided.

For example:

from openpyxl import load_workbook

wb = load_workbook('test.xlsx', use_iterators=True)
ws = wb.get_sheet_by_name('Sheet1')


# printing coordinates of all rows and cols
for row in ws.iter_rows(row_offset=2):
    for cell in row:
        print cell.coordinate


# printing coordinates from "A3:E5" range
for row in ws.iter_rows(range_string="A1:E3", row_offset=2):
    for cell in row:
        print cell.coordinate
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top