python- do_excel
1 # @File : class_01_do_excel.py 2 # coding=gbk 3 4 #pip install openpyxl 5 #新建.xlsx,一定要右键新建 6 7 from openpyxl import load_workbook #可以对Excel进行读写操作 8 #1.打开Excel 9 workbook = load_workbook('http_requests.xlsx') 10 11 #2.定位表单 12 sheet = workbook['Sheet1'] 13 14 #3.读取单元格数据,读取X行Y列单元格里面的value值,行列值从1开始 15 cell_data = sheet.cell(1,1).value 16 print('读取表格中第一行第一列单元格的数据是:{}'.format(cell_data)) 17 18 #4.获取最大的行列值 19 max_row = sheet.max_row 20 max_cow = sheet.max_column 21 print('最大的行值:',max_row) 22 print('最大的列值:',max_cow) 23 24 #5.单元格内写入数据 25 # sheet.cell(21,1).value='http://47.107.168.87:8080/futureloan/mvc/api/member/recharge' #第21行第1列,写入数据 26 # workbook.save('http_requests.xlsx')#保存表格,,一定要确保表格已关闭,否则报错PermissionError: [Errno 13] Permission denied 27 28 #6.新建excel表格:保存时,名字不重复就可以新建 29 # from openpyxl import Workbook # W一定大写 30 # wb = Workbook() 31 # wb.save('新建excel表格.xlsx') 32 33 #7.读取每一行的数据,存储到一个字典里;读取的所有行的数据存在一个列表中 34 test_data = [] 35 for i in range(2,sheet.max_row+1): 36 sub_data = {} 37 sub_data['url'] = sheet.cell(i,1).value 38 sub_data['params'] = sheet.cell(i,2).value 39 sub_data['method'] = sheet.cell(i,3).value 40 sub_data['excepted'] = sheet.cell(i,4).value 41 test_data.append(sub_data) 42 print('http_requests.xlsx读取的结果是:',test_data)