-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_xls_data.py
More file actions
55 lines (38 loc) · 1.05 KB
/
get_xls_data.py
File metadata and controls
55 lines (38 loc) · 1.05 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import xlrd
# 实现获取excel某张表的行数、单元格数据
# #获取excel文件
# data = xlrd.open_workbook('test.xlsx')
# #获取第一张表数据
# tables = data.sheets()[0]
# #打印表行数
# print tables.nrows
# #打印第4行,第3列数据
# print tables.cell_value(3,2)
# 封装获取表格方法
class OpeExcel:
def __init__(self,file_name=None,sheet_id=None):
if file_name:
self.file_name = file_name
self.sheet_id = sheet_id
else:
self.file_name = 'test.xlsx'
self.sheet_id = 0
self.data = self.get_data()
# 封装获取表格数据方法
def get_data(self):
data = xlrd.open_workbook(self.file_name)
tables = data.sheets()[self.sheet_id]
return tables
# 封装获取单元格行数方法
def get_lines(self):
tables = self.data
return tables.nrows
# 封装获取单元格数据的方法
def get_value(self,row,col):
return self.data.cell_value(row,col)
if __name__ == '__main__':
opers = OpeExcel()
print opers.get_lines()
print opers.get_value(3,2)