300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > python读取excelsheet-python实现读取excel文件中所有sheet操作示例

python读取excelsheet-python实现读取excel文件中所有sheet操作示例

时间:2018-10-01 09:06:13

相关推荐

python读取excelsheet-python实现读取excel文件中所有sheet操作示例

本文实例讲述了python实现读取excel文件中所有sheet操作。分享给大家供大家参考,具体如下:

表格是这样的

实现把此文件所有sheet中 标识为1 的行,取出来,存入一个字典。所有行组成一个列表。

# -*- coding: utf-8 -*-

from openpyxl import load_workbook

def get_data_from_excel(excel_dir):#读取excel,取出所有sheet要执行的接口信息,返回列表

work_book = load_workbook(excel_dir)

all_sheets = work_book.sheetnames

api_info_list = []

for i in range(0,len(all_sheets)):

work_sheet = all_sheets[i]

sheet = work_book[work_sheet]

rows = sheet.max_row

for r in range(1,rows):#从第2行开始取数据

api_data = {}

temp_list = []

for n in range(0,len(sheet[str(r+1)])):

if sheet[str(r+1)][0].value == 1:#把标识为1的行,此行的每个单元格数据加入到临时list

temp_list.append(sheet[str(r+1)][n].value)

for param in temp_list:#把临时表list中有'='符号的元素分割开

if '=' in str(param):

p = param.split('=')

api_data[p[0]] = p[1]

if api_data:

api_info_list.append(api_data)

return api_info_list

if __name__ == '__main__':

excel_dir = "D:\api_testcase.xlsx"

print(get_data_from_excel(excel_dir))

希望本文所述对大家Python程序设计有所帮助。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。