python---讀取/寫入excel用例數據


使用excel管理用例

①、讀取excel的用例數據,每一類參數保存在list中返回;
②、那么接下來使用unitest編寫用例時,可以去調用這個函數。使用里面的數據;

個人認為好處是,大部分人還是習慣excel的編寫用例習慣,將實際用例與代碼分離,有助於分工和維護

一、在excel中寫好用例后,在python(unitest)中調用
 1 #寫文件
 2 import xlwt;
 3 #讀文件
 4 import xlrd;
 5 #若要對excel文件進行修改必須!要引入這個工具模塊,其中含有復制和修改功能---------注意引入方式
 6 import xlutils;
 7 from xltuils.copy import copy;
 8 
 9 
10 import unittest;
11 import requests;
12 #使用寫文件模式打開文件
13 open_workbook=xlrd.open_workbook(r'D:\Users\4399-3046\Desktop\test.xls');
14 #最終返回的是文件類型
15 print(open_workbook);
16 #打印該文件下,對應數據表的行數
17 #print(open_workbook.sheet_by_name('mytest').nrows);
18 
19 #打印該文件下,對應數據表的列數
20 #print(open_workbook.sheet_by_name('mytest').ncols);
21 #for item_rows in range(1,open_workbook.sheet_by_name('mytest').nrows):
22     #for item_cols in range(1,open_workbook.sheet_by_name('mytest').ncols):
23         #print(open_workbook.sheet_by_name('mytest').cell(item_rows,item_cols));
24 
25 
26 #循環打印出所有數據,並分別保存在list中
27 #編號
28 listkey=[];
29 #測試地址
30 listurl=[];
31 #提交方式
32 listtype=[];
33 #參數1
34 listcanshu1=[];
35 #參數2
36 listcanshu2=[];
37 #預期結果
38 listyuqi=[];
39 #實際結果
40 listresult=[];
41 #在unitest框架中,通過調用參數,執行用例,將實際結果比對預期結果是否一致,若一致,則通過,否則用例失敗,並將實際結果寫入excel中(寫入代碼后文將寫到)
42 for item_row in range(1,open_workbook.sheet_by_name('mytest').nrows):
43     #獲取用例編號
44     listkey.append(open_workbook.sheet_by_name('mytest').cell(item_row,0).value);
45     # 獲取參數1
46     listcanshu1.append(open_workbook.sheet_by_name('mytest').cell(item_row, 1).value);
47     # 獲取參數2
48     listcanshu2.append(open_workbook.sheet_by_name('mytest').cell(item_row, 2).calue);
49     # 獲取預期結果
50     listyuqi.append(open_workbook.sheet_by_name('mytest').cell(item_row, 3).value);
51     # 獲取實際結果
52     listresult.append(open_workbook.sheet_by_name('mytest').cell(item_row, 4).value);------------->注意:必須要有【.value】,因為返回的是dict字典,而實際我們只需要其中的具體值
53 
54 
55 print(listkey);
56 print(listcanshu1);
57 print(listcanshu2);
58 print(listyuqi);
59 print(listresult);

 

運行結果:

 2、新建文件,並在新文件中寫入數據

1  open_workbook=xlwt.Workbook();---定義一個工作簿
2 
3 
4 new_sheet=open_workbook.add_sheet('lianxi',cell_overwrite_ok = True);----在工作簿中新增一個工作表,自定義一個工作表名稱
注意1:若當前單元格已經有值,無法重復寫入覆蓋原有,會提示異常,需要在新建表單sheet時,就要指定,可以重復寫入cell_overwrite_ok = True,默認是false
5 new_sheet.write(0,5,'lianxi_test');-----在工作表中寫入數據(注意:寫入數據只在工作表sheet中生效)---
注意2:如果當前目錄下已存在同名的工作簿名稱,則會報錯
6 open_workbook.save(r'D:\Users\Desktop\test01.xls');----保存工作簿(需要指定地址和名稱)

 

 

3、在已有文件下excel下,寫入數據
1#思路:打開文件----復制文件----在文件中寫入數據----刪除原文件----保存新文件 

#打開當前已經存在的工作簿 2 open_workbook2=xlrd.open_workbook(r'D:\Users\Desktop\test.xls'); 3 #復制當前工作簿 4 open_sheet2=copy(open_workbook2);
#打開置頂索引的工作表
5 open_ws=open_sheet2.get_sheet(3); 6 #在指定的單元格寫入數據 7 open_ws.write(1,4,'TEST');
#移除原有的文件
8 os.remove(r'D:\Users\4399-3046\Desktop\test.xls')
#保存新文件
9 open_sheet2.save(r'D:\Users\4399-3046\Desktop\test.xls');

運行結果:

 

 
        

 

4、擴展--結合python-unitest,通常我們需要將執行結果自動寫入表格中
5、拓展--將寫文件和讀文件,分別封裝為公共方法調用(已完成)
 1 #by zhonghuihong
 2 import xlwt;
 3 import xlrd;
 4 from xlutils.copy import copy;
 5 import os;
 6 #此接口主要用於excel數據的讀取和寫入
 7 
 8 def get_excel_data(paths,sheetName):
 9     open_workbook=xlrd.open_workbook(paths);
10     open_sheet=open_workbook.sheet_by_name(sheetName);
11     rows=open_sheet.nrows;
12     listkey = [];
13     listcanshu1 = [];
14     listcanshu2 = [];
15     listyuqi = [];
16     listresult = [];
17     for item_row in range(1, rows):
18         listkey.append(open_sheet.cell(item_row, 0).value);
19         listcanshu1.append(open_sheet.cell(item_row, 1).value);
20         listcanshu2.append(open_sheet.cell(item_row, 2).value);
21         listyuqi.append(open_sheet.cell(item_row, 3).value);
22         listresult.append(open_sheet.cell(item_row, 4).value);
23     return listkey,listcanshu1,listcanshu2,listyuqi,listresult;
24 
25 
26 def write_excel_data(paths,sheet_id,rows_id,cols_id,values):
27     open_workbook2 = xlrd.open_workbook(paths);
28     open_sheet2 = copy(open_workbook2);
29     open_ws = open_sheet2.get_sheet(sheet_id);
30     open_ws.write(rows_id,cols_id,values);
31     os.remove(paths);
32     open_sheet2.save(paths);
33     return 'success';
 
        

 







遇到的問題
1、讀取的整數變成浮點類型,解決辦法

 2、腳本中引入copy方法,但提示TypeError: 'module' object is not callable的解決辦法:將引入語句寫成from xlutils.copy import copy;

3、指定sheet時(ow.get_sheet[0];),提示TypeError: 'method' object is not subscriptable

產生原因:subscriptable的意思是可有下標,錯誤的原因就是:對象不具有下標,即把不具有下標操作的對象用成了對象[i],比如int對象變量[i]就會報錯。仔細檢查錯誤行。


代碼最終優化:

*增加try--exception異常捕獲

*增加寫入輸入不能為空

 1 #寫數據(文件薄,表格id,行id,列id,要寫的內容)
 2 def write_excel_data(paths,sheet_id,rows_id,cols_id,values):
 3     if(len(values)==0 ):
 4         return '寫入數據不能為空';
 5     else:
 6 
 7       try:
 8           #如果寫入位置已經有值, 會直接使用新內容覆蓋
 9          open_workbook2 = xlrd.open_workbook(paths);
10          open_sheet2 = copy(open_workbook2);
11          open_ws = open_sheet2.get_sheet(sheet_id);
12          open_ws.write(rows_id, cols_id, values);
13          os.remove(paths);
14          open_sheet2.save(paths);
15 
16          return 'success';
17       except Exception as e:
18          return e;
19 
20 
21 #讀取excel_data(文件薄地址,表格名)
#通過循環讀出指定表格中的所有數據,以列表格式輸出
22 def get_excel_data(paths,sheetName): 23 open_workbook = xlrd.open_workbook(paths); 24 open_sheet = open_workbook.sheet_by_name(sheetName); 25 26 if open_sheet.nrows <= 1: 27 return '文件總行數小於1,請確認'; 28 29 else: 30 res = [] 31 j = 1 32 for i in list(range(open_sheet.nrows-1)): 33 s = {}; 34 #行編號 35 s['rowNum'] = i+1; 36 values = open_sheet.row_values(j) 37 for x in list(range(open_sheet.ncols)): 38 keys=open_sheet.row_values(0); 39 s[keys[x]] = values[x]; 40 res.append(s); 41 j += 1; 42 return res;

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM