Reputation: 53
I have an excel file, i'm trying to read the first row pass it to another function and delete the first row from the excel and save it, until all the rows in the excel file are used up.
I tried using openpyxl i was able to delete the first row in my first attempt but when i try reading the excel file again it's giving me this exception
openpyxl.utils.exceptions.CellCoordinatesException: There is no row 0 (A0)
here's my code
import openpyxl
filename = "example.xlsx"
wb = openpyxl.load_workbook(filename)
sheet = wb['Sheet1']
status = sheet['A1'].value
print(status)
sheet.delete_rows(0, 1)
wb.save(filename)
Upvotes: 2
Views: 10712
Reputation: 6483
Untested but the following tweak should fix it. Instead of reading 'A1'
and removing row 0
you look for the first non empty row using min_row
and read and remove that.
import openpyxl
filename = "example.xlsx"
wb = openpyxl.load_workbook(filename)
sheet = wb['Sheet1']
status = sheet.cell(sheet.min_row, 1).value
print(status)
sheet.delete_rows(sheet.min_row, 1)
wb.save(filename)
Upvotes: 1
Reputation: 846
Once the rows are deleted with delete_rows(beg_row, end_row)
, their index is also deleted, which means there exists no row with index 0
any more.
Upvotes: 1