python - 读写excel数据

 


#
-*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: do_excel.py @ide: PyCharm Community Edition @time: 2018-12-05 11:11 @blog: https://www.cnblogs.com/gotesting/ ''' from openpyxl import load_workbook # 可以对excel进行读写 # xlrd xlwt xlwriter都可以操作excel # 1. 打开excel,返回工作簿对象 workbook = load_workbook('test_excel.xlsx') # 2. 定位表单,返回表单对象 sheet = workbook['登录及充值测试数据'] # 3. 读取数据,从X行Y列的单元格中读取value值,excel中行列值从1开始数 a = sheet.cell(2,2).value print(a) # 4. 获取最大的行列值 max_row = sheet.max_row max_col = sheet.max_column print('行:',max_row) print('列:',max_col) # 5. 写数据 # sheet.cell(11,1).value = '写入excel测试' # workbook.save('test_excel.xlsx') # 6. 新建excel # from openpyxl import Workbook # wb = Workbook() # wb.save('新建excel测试.xlsx') # 7. 读取每一行的数据,存储到一个字典里面,所有行的数据存储在一个列表中。 test_data = [] for i in range(2,sheet.max_row+1): sub_data = {} sub_data['url'] = sheet.cell(i,1).value sub_data['param'] = sheet.cell(i,2).value sub_data['method'] = sheet.cell(i,3).value sub_data['expected'] = sheet.cell(i,4).value test_data.append(sub_data) print('test_excel.xlsx的读取结果是:',test_data)

 


 

posted @ 2018-12-05 11:51  JiaxyGogogo  阅读(440)  评论(0编辑  收藏  举报