openpyxl读取一个sheet的所有行数据可以用rows属性,官方文档如下:
If you need to iterate through all the rows or columns of a file, you can instead use the Worksheet.rows property:
sheet的rows属性返回的是一个生成器,如下:
# -*- coding: utf-8 -*-
from openpyxl import load_workbook
wb = load_workbook('test.xlsx',data_only=True)
for sheet in wb:
sheet_name = sheet.title
data_all = sheet.rows
print('sheet名:',sheet_name)
print(data_all)
sheet名: 北京
sheet名: 上海
sheet名: 杭州
可以直接遍历生成器也可以使用tuple将生成器变为元组来输出行:
# -*- coding: utf-8 -*-
from openpyxl import load_workbook
wb = load_workbook('test.xlsx',data_only=True)
for sheet in wb:
sheet_name = sheet.title
data_all = sheet.rows
print('sheet名:',sheet_name)
data_tup