网络编程
位置:首页>> 网络编程>> Python编程>> python实现读取excel文件中所有sheet操作示例

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

作者:zhizunyu2009  发布时间:2022-04-25 15:00:36 

标签:python,excel,sheet

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

表格是这样的

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实现读取excel文件中所有sheet操作示例

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

来源:https://blog.csdn.net/zhizunyu2009/article/details/80511780

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com