• 6、Python 中 利用 openpyxl 读 写 excel 操作


     1 __author__ = 'Administrator'
     2 
     3 from openpyxl import load_workbook
     4 
     5 # Excel_Util 类
     6 class Excel_util:
     7 
     8 
     9     #初始化
    10     def __init__(self,filePath,sheetName):
    11 
    12         self.filePath = filePath
    13         self.sheetName = sheetName
    14 
    15 
    16     #读取 excel 方法
    17     def read_excel(self):
    18 
    19         #打开工作簿
    20         wb = load_workbook(self.filePath)
    21 
    22         #获取sheet
    23         sheet = wb[self.sheetName]
    24 
    25         #获取最大的行号
    26         rowNo = sheet.max_row
    27 
    28         #获取最大的列号
    29         colNo = sheet.max_column
    30 
    31         #获取第一行的数据作为key
    32         key = []
    33 
    34         for i in range(1,colNo+1):
    35 
    36             #将第一行,第 i 列的值 追加到 列表 key中
    37             key.append(sheet.cell(1,i).value)
    38 
    39         #格式化输出 key 的值
    40         # print('key 的值为 {0}'.format(key))
    41 
    42         #获取 excel 中的除第一行以外的测试数据,将所有数据存放在 test_datas 列表中(即:将每一行的 字典 数据 追加到 列表中)
    43         test_datas = []
    44 
    45         #从第2行开始读取数据
    46         for i in range(2,rowNo+1):
    47 
    48             #从第一列开始读取数据
    49             #将每一列的数据存放在 字典 dic_datas 中
    50             dic_datas = {}
    51 
    52             for j in range(1,colNo+1):
    53 
    54                 # key 值中的索引从0 开始,即 dic_datas中的 key 对应 为 i 行 j 列 的值
    55                 dic_datas[key[j-1]] = sheet.cell(i,j).value
    56 
    57             test_datas.append(dic_datas)
    58 
    59         #返回列表 test_datas
    60         return test_datas
    61 
    62 
    63     #excel 写回 ,写回的对应行号,写回的实际结果,写回的测试结果
    64     def write_back(self,rowNo,actual_result,test_result):
    65 
    66         wb = load_workbook(self.filePath)
    67         sheet = wb[self.sheetName]
    68 
    69         sheet.cell(rowNo,6).value = actual_result
    70         sheet.cell(rowNo,7).value = test_result
    71 
    72         #写回之后,必须要保存
    73         wb.save(self.filePath)
    74 
    75 
    76 if __name__ == '__main__':
    77 
    78     #实例化类的时候 传入初始化函数中对应的参数
    79     excel_util = Excel_util('test_datas.xlsx','Sheet1')
    80 
    81     datas = excel_util.read_excel()
    82 
    83     print('读取excel数据后返回的列表值为 {0}'.format(datas))
    84 
    85     excel_util.write_back(6,'实际结果','测试结果')


    返回测试数据为:

    读取excel数据后返回的列表值为 [{'caseId': 1, 'mold': 'get', 'url': 'http://localh/xx', 'params': 'name=jack&age=20'}, {'caseId': 2, 'mold': 'get', 'url':'http://localh/xx', 'params': 'name=tom&age=21'}]
  • 相关阅读:
    项目外部 property文件使用方法
    java JsonArray统一添加key
    java发送Http请求
    excel数据追加,java实现
    request.getParameter("name")获取参数为null和""空字符串的区别
    maven 配置jetty插件启动 及简单测试
    左右值编码实现树状存储
    Spring将classpath下的 .properties文件数据读出放到map中,在初始化时加载
    Ubuntu简单配置
    java.io.Serializable浅析
  • 原文地址:https://www.cnblogs.com/little-little-bai/p/10365749.html
Copyright © 2020-2023  润新知