python读取excel表格
coding=utf-8
import openpyxl
import sys
import os
import unittest
base_path = os.getcwd()
sys.path.append(base_path)
class HandExcel:
def load_excel(self): # 加载excel
openexcel = openpyxl.load_workbook(r’E:\autotest_code\UI_auto\Restaurant treasure\date\接口测试数据.xlsx’)
return openexcel
def get_sheet_date(self, index=None): # 获取sheet名称
sheetname = self.load_excel().sheetnames
if index == None:
index = 0
date = self.load_excel()[sheetname[index]]
return date
def get_cell_value(self, roww, colss): # 获取单元格内容
date = self.get_sheet_date().cell(row=roww, column=colss).value
return date
def get_rows(self): # 获取总行数
row = self.get_sheet_date().max_row
return row
def get_rows_values(self, row): # 获取一行的值
row_list = []
for i in self.get_sheet_date()[row]:
row_list.append(i.value)
return row_list
def get_cols(self): # 获取总列数
colum = self.get_sheet_date().max_column
return colum
def get_cols_values(self, column): # 获取一列的值 (有问题)
colu_list = []
for i in self.get_sheet_date()[column]:
colu_list.append(i.value)
return colu_list
if name == “main”:
handle = HandExcel()
# print(handle.get_rows_values(2))
# print(handle.get_sheet_date())
print(handle.get_cols())
print(handle.get_rows())
# print(handle.get_cols_values(2))