跳过Openpxyl中的第一行

时间:2019-01-18 21:38:06

标签: python openpyxl

嗨,我有以下代码

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) )

效果很好。但是,我不需要它来显示第一行即标题行的数据。如何更改查询以排除第一行?

4 个答案:

答案 0 :(得分:1)

经常阅读documentation

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

答案 1 :(得分:0)

由于iter_rows是生成器,因此很遗憾,我们无法对第一行进行切分[1:],因此这是一种跳过但有效的跳过第一行的方法。

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)) 

答案 2 :(得分:0)

您可以像这样对工作表进行切片。

worksheet[2: worksheet.max_row]

答案 3 :(得分:-1)

只需使用函数enumerate()枚举行,然后获取2个不同变量(如

)中的索引和单元格值
for index, row_cells in enumerate(worksheet.iter_rows()):
    if (index == 0):
       continue
    for cell in row_cells:
       print('%s: cell.value=%s' % (cell, cell.value))
相关问题