Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip First Row In Openpxyl

Hi I have the following code

wb_obj = openpyxl.load_workbook(path, data_only=True) 
worksheet = wb_obj.get_sheet_by_name('Sheet1')

for row_cells in worksheet.iter_rows():
    for cell in row_cells:
       print('%s: cell.value=%s' % (cell, cell.value) )

it works well. however I do not need it to show the data from the first row which is the header row. How can I change the query to exclude the first row?

like image 785
Bilal Habib Avatar asked Nov 30 '22 21:11

Bilal Habib


2 Answers

The documentation suggests using iter_rows

for row_cells in worksheet.iter_rows(min_row=2):
    for cell in row_cells:
        print('%s: cell.value=%s' % (cell, cell.value) )
like image 157
ahed87 Avatar answered Dec 02 '22 09:12

ahed87


Since iter_rows is a generator, we unfortunately cannot slice out the first line [1:], so this is a hackish but effective way to skip the first row.

for i, row_cells in enumerate(worksheet.iter_rows()):                              
    if i == 0:                                                                              
        continue                                                            
    for cell in row_cells:                                                  
        print('%s: cell.value=%s' % (cell, cell.value)) 
like image 21
Erik Lunna Avatar answered Dec 02 '22 11:12

Erik Lunna