sheet的rows属性是一行行的输出数据,columns属性是一列列的输出数据,其返回值也是生成器。文档如下:
If you need to iterate through all the rows or columns of a file, you can instead use the Worksheet.rows property or the Worksheet.columns property
# -*- 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.columns
print('sheet名:',sheet_name)
print(data_all)
sheet名: 上海
sheet名: 杭州
直接遍历生成器或者将其转为元组,单元格的value属性来输出单元格值
# -*- 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.columns
print('sheet名:',sheet_name)
# 生成器转为元组
d