python接口自動化測試 - openpyxl封裝類


前言

為了更好的讓openpyxl在工作中使用,將openpyxl的常用操作封裝起來,這樣不僅復用性高,而且閱讀性好

 

直接上代碼

  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 
  4 """
  5 __title__  = openpyxl操作Excel工具類
  6 """
  7 
  8 import openpyxl
  9 
 10 
 11 class ExcelUtil:
 12     workBook = None
 13     workSheet = None
 14 
 15     def load_excel(self, path):
 16         """
 17         加載Excel
 18         :param path: 需要打開的Excel的路徑
 19         """
 20         self.workBook = openpyxl.load_workbook(path)
 21 
 22     def get_sheet_by_name(self, name):
 23         """
 24         獲取sheet對象
 25         :param name: sheet名
 26         """
 27         self.workSheet = self.workBook.get_sheet_by_name(name)
 28 
 29     def get_sheet_by_index(self, index=0):
 30         """
 31         獲取sheet對象
 32         :param index: sheet的索引
 33         """
 34         # 獲取workBook里所有的sheet名 -> list
 35         sheet_names = self.workBook.get_sheet_names()
 36         # 根據索引獲取指定sheet
 37         self.workSheet = self.workBook[sheet_names[index]]
 38 
 39     def get_cell_value(self, col, row):
 40         """
 41         獲取cell的值
 42         :param col: 所在列
 43         :param row: 所在行
 44         """
 45         try:
 46             return self.workSheet.cell(column=col, row=row).value
 47         except BaseException as e:
 48             return None
 49 
 50     def get_cell_value_by_xy(self, str):
 51         """
 52         獲取cell的值
 53         :param str: 坐標
 54         """
 55         try:
 56             return self.workSheet[str].value
 57         except BaseException as e:
 58             return None
 59 
 60     def get_sheet_rows(self):
 61         """
 62         獲取最大行數
 63         """
 64         return self.workSheet.max_row
 65 
 66     def get_sheet_cols(self):
 67         """
 68         獲取最大列數
 69         """
 70         return self.workSheet.max_column
 71 
 72     def write_data(self, row, col, value, path):
 73         """
 74         寫入數據
 75         """
 76         try:
 77             self.workSheet = self.workBook.active
 78             self.workSheet.cell(column=col, row=row, value=value)
 79             self.workBook.save(path)
 80         except BaseException as e:
 81             print(e)
 82             return None
 83 
 84     def get_excel_data(self):
 85         """
 86         獲取表所有數據
 87         :return: list
 88         """
 89         # 方式一
 90         data_list = tuple(self.workSheet.values)
 91         # 方式二
 92         # data_list = []
 93         # for i in range(self.get_sheet_rows()):
 94         #     data_list.append(self.get_row_value(i + 2))
 95         return data_list
 96 
 97     def get_row_value(self, row):
 98         """
 99         獲取某一行的內容
100         :param row: 第幾行 -> str  **從1開始**
101         :return: list
102         """
103         # 方式一
104         row_list = self.get_excel_data()[row]
105         # 方式二
106         # row_list = []
107         # for i in self.workSheet[str(row + 1)]:
108         #     row_list.append(i.value)
109         return row_list
110 
111     def get_col_value(self, col='A'):
112         """
113         獲取某一列的內容
114         :param col: 第幾列 -> str
115         :return: list
116         """
117         col_list = []
118         for i in self.workSheet[col]:
119             col_list.append(i.value)
120         return col_list
121 
122     def get_row_num(self, case_id):
123         """
124         獲取行號
125         :param case_id: 用例編號
126         :return:
127         """
128         num = 1
129         col_data = self.get_col_value()
130         for data in col_data:
131             if case_id == data:
132                 return num
133             num += 1
134         return 0
135 
136 
137 excelUtil = ExcelUtil()
138 
139 if __name__ == '__main__':
140     path = 'F:/interface/case/test.xlsx'
141     # 讀取excel文件
142     excelUtil.load_excel(path)
143     # 獲取某個sheet
144     excelUtil.get_sheet_by_name("Sheet1")
145     excelUtil.get_sheet_by_index()
146     # 獲取某個cell的值
147     data = excelUtil.get_cell_value(col=1, row=1)
148     print(data)
149     data = excelUtil.get_cell_value_by_xy("A3")
150     print(data)
151     # 獲取sheet行數
152     data = excelUtil.get_sheet_rows()
153     print(data)
154     # 獲取sheet列數
155     data = excelUtil.get_sheet_cols()
156     print(data)
157     # 獲取某一行數據
158     data = excelUtil.get_row_value(0)
159     print(data)
160     # 獲取某一列數據
161     data = excelUtil.get_col_value()
162     print(data)
163     # 寫入數據
164     excelUtil.write_data(row=9, col=1, value="test", path=path)
165     # 獲取全部數據
166     data = excelUtil.get_excel_data()
167     print(data)
168     # 獲取行號
169     data = excelUtil.get_row_num('imooc_001')
170     print(data)

 

 


免責聲明!

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



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