-
Notifications
You must be signed in to change notification settings - Fork 3
/
handle_excel.py
70 lines (63 loc) · 1.98 KB
/
handle_excel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#coding=utf-8
import json
import openpyxl
import sys
import os
base_path = os.getcwd()
excel_path = os.path.join(base_path, "asset", "AppleID.xlsx")
class HandExcel:
def load_excel(self):
open_excel = openpyxl.load_workbook(excel_path)
return open_excel
def get_sheet_data(self,index=None):
sheet_name = self.load_excel().sheetnames
if index == None:
index = 0
data = self.load_excel()[sheet_name[index]]
return data
def get_cell_value(self,row,cols):
"获取某个单元格的内容"
value = self.get_sheet_data().cell(row=row,column=cols).value
return value
def get_row(self):
'''获取行数'''
row = self.get_sheet_data().max_row
return row
def get_row_value(self,row):
'''获取一整行数据'''
row_list =[]
for i in self.get_sheet_data()[row]:
row_list.append(i.value)
return row_list
def excel_write_data(self,row,cols,value):
wb = self.load_excel()
wr = wb.active
wr.cell(row,cols,value)
wb.save(excel_path)
def get_columns_value(self,key=None):
'''获取某一列的数据'''
column_list = []
if key ==None:
key ='A'
column_list_data = self.get_sheet_data()[key]
for i in column_list_data:
column_list.append(i.value)
return column_list
def get_rows_number(self,case_id):
'''获取行号'''
num = 1
cols_data = self.get_columns_value()
for col_data in cols_data:
if case_id == col_data:
return num
num = num +1
return num
def get_excel_data(self):
'''获取excel里面所有的数据'''
data_list = []
for i in range(self.get_row()):
data_list.append(self.get_row_value(i+2))
return data_list
if __name__ =="__main__":
handexcel = HandExcel()
print(handexcel.get_excel_data())