-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathreadexcel.py
More file actions
35 lines (33 loc) · 1.07 KB
/
readexcel.py
File metadata and controls
35 lines (33 loc) · 1.07 KB
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
# coding:utf-8
import xlrd
class ExcelUtil():
def __init__(self, excelPath, sheetName="Sheet1"):
self.data = xlrd.open_workbook(excelPath)
self.table = self.data.sheet_by_name(sheetName)
# 获取第一行作为key值
self.keys = self.table.row_values(0)
# 获取总行数
self.rowNum = self.table.nrows
# 获取总列数
self.colNum = self.table.ncols
def dict_data(self):
if self.rowNum <= 1:
print("总行数小于1")
else:
r = []
j = 1
for i in list(range(self.rowNum-1)):
s = {}
# 从第二行取对应values值
s['rowNum'] = i+2
values = self.table.row_values(j)
for x in list(range(self.colNum)):
s[self.keys[x]] = values[x]
r.append(s)
j += 1
return r
if __name__ == "__main__":
filepath = "debug_api.xlsx"
sheetName = "Sheet1"
data = ExcelUtil(filepath, sheetName)
print(data.dict_data())