GoogleSheets, iter_rows, IndexError: tuple index out of range
If I make a googlesheet with a smallish number of rows and then download it as an excel file and process it with openpyxl with something like this:
sheetname = "Sheet1"
min_row=1
name_column=1
wb = load_workbook(filename, data_only=data_only, read_only=read_only)
data = wb[sheetname]
for row in data.iter_rows(min_row=min_row):
name = row[name_column].value # crash happens here
The stacktrace says:
IndexError: tuple index out of range
# references the 'name = row[name_column].value' line
I think there is some optimization happening in the google export where they don't leave any trailing rows at the end of the data.
I've changed my code to look like this:
sheetname = "Sheet1"
min_row=1
name_column=1
wb = load_workbook(filename, data_only=data_only, read_only=read_only)
data = wb[sheetname]
for row in data.iter_rows(min_row=min_row):
name = ""
try:
name = row[name_column].value
except IndexError:
print("Appears to be a googlesheet based excel doc... skipping now")
break
Can we check for a null row in the iter_rows
function so we don't fall off the end of these GoogleSheets exports?
Thank you for such a powerful, wonderful library. :)
openpyxl version: 3.1.0
python version: 3.9.10
OS: ubuntu
Edited by Peter H