赞
踩
openpyxl读取一个sheet的所有行数据除了用rows属性,也可以用iter_rows()方法,该方法更加灵活,可以指定读取行和列,官方文档如下:
You can also use the Worksheet.iter_rows() method:
for row in ws.iter_rows(min_row=1, max_col=3, max_row=2):
for cell in row:
print(cell)
iter_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.iter_rows()
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.iter_rows()
print('sheet名:',sheet_name)
# 转为元组
data_tuple = tuple(data_all)
for row in data_tuple:
for cell in row:
value = cell.value
print(value)
sheet名: 上海
静安豪景苑
sh-5i5j/xiaoqu/79364-html
新泾苑
sh-5i5j/xiaoqu/138741-html
兆嘉苑
sh-5i5j/xiaoqu/367631-html
天山怡景苑
sh-5i5j/xiaoqu/385850-html
金星绿苑
sh-5i5j/xiaoqu/390212-html
申晖小区
sh-5i5j/xiaoqu/436536-html
共康四村
sh-5i5j/xiaoqu/197400-html
崧泽华城青湖苑
sh-5i5j/xiaoqu/377290-html
新昌里公寓
sh-5i5j/xiaoqu/47394-html
建国西路158弄
sh-5i5j/xiaoqu/125420-html
蔷薇七村
sh-5i5j/xiaoqu/388767-html
sheet名: 杭州
复兴城市家园
hz-5i5j/xiaoqu/100000000001038-html
木材新村
hz-5i5j/xiaoqu/100000000004309-html
万寿亭街
hz-5i5j/xiaoqu/100000000002688-html
瓶窑镇凤溪路
hz-5i5j/xiaoqu/100000000000367-html
孝子坊
hz-5i5j/xiaoqu/100000000002902-html
永和坊
hz-5i5j/xiaoqu/100000000002147-html
教工路131号
hz-5i5j/xiaoqu/100000000001423-html
星星港湾琴海居
hz-5i5j/xiaoqu/100000000005718-html
文二路98号
hz-5i5j/xiaoqu/100000000001269-html
龙悦湾
hz-5i5j/xiaoqu/100000000006161-html
意盛花苑
hz-5i5j/xiaoqu/100000000002797-html
也可以限制行和列数据的读取
# -*- 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.iter_rows(min_col=1,max_col=2,max_row=3,min_row=2)
print('sheet名:',sheet_name)
# 转为元组
data_tuple = tuple(data_all)
for row in data_tuple:
for cell in row:
value = cell.value
print(value)
sheet名: 上海
新泾苑
sh-5i5j/xiaoqu/138741-html
兆嘉苑
sh-5i5j/xiaoqu/367631-html
sheet名: 杭州
木材新村
hz-5i5j/xiaoqu/100000000004309-html
万寿亭街
hz-5i5j/xiaoqu/100000000002688-html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。