1. 程式人生 > >python - 讀寫excel資料

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)