-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxls_class.py
155 lines (100 loc) · 3.34 KB
/
xls_class.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
__author__ = 'Jwely'
try:
import xlrd
import xlwt
except ImportError:
import pip
pip.main(["install", "xlrd"])
pip.main(["install", "xlwt"])
import xlrd
import xlwt
class xls_class():
"""
class for reading and writing xls files.
"""
def __init__(self):
self.filepath = ""
self.workbook = None
self.worksheets = {}
return
def read(self, filepath):
"""
:param filepath: filepath to xlsx or xls file to read.
:return:
"""
self.filepath = filepath
workbook = xlrd.open_workbook(self.filepath)
self.sheetnames = workbook.sheet_names()
for sheetname in self.sheetnames:
new_sheet = sheet(sheetname, self.filepath)
new_sheet.read(workbook)
self.worksheets[sheetname] = new_sheet
return self.worksheets
def write(self, filepath):
book = xlwt.Workbook(encoding = " utf-8")
for sheet in self.worksheets:
self.worksheets[sheet].write(book)
book.save(filepath)
return filepath
class sheet():
def __init__(self, name, filepath):
self.name = name
self.filepath = filepath
self.rows = []
return
def __getitem__(self, (row, col)):
return self.rows[row][col]
def __setitem__(self, (row, col), value):
self.rows[row][col] = value
return
def viewsheet(self):
"""
:return: prints a heads up view of this sheets data
"""
for i, row in enumerate(self.rows):
print("{0} {1}".format(str(i).ljust(6), row))
return
def read(self, workbook):
sheet = workbook.sheet_by_name(self.name)
numrows = sheet.nrows - 1
numcells = sheet.ncols -1
for i in range(numrows):
rowvals = []
for j in range(numcells):
# Cell Types: 0=Empty, 1=Text, 2=Number, 3=Date, 4=Boolean, 5=Error, 6=Blank
cell_type = sheet.cell_type(i, j)
cell_value = sheet.cell_value(i, j)
if cell_type == 1 or cell_type == 3:
cell_value = str(cell_value)
elif cell_type == 2:
cell_value = float(cell_value)
elif cell_type == 4:
cell_value = bool(cell_value)
else:
cell_value = ""
rowvals.append(cell_value)
self.rows.append(rowvals)
return self.rows
def write(self, workbook):
worksheet = workbook.add_sheet(self.name)
for i, row in enumerate(self.rows):
for j, col in enumerate(row):
worksheet.write(i, j, col)
return worksheet
if __name__ == "__main__":
# some stuff
testfile = "Reference/Matching_XL.xlsx"
xls = xls_class()
xls.read(testfile)
# view a worksheet
# xls.worksheets["CAD_SDS"].viewsheet()
# view row 1 column 3
print xls.worksheets["CAD_SDS"][1, 3]
# view row 1, all columns
print xls.worksheets["CAD_SDS"][1, :]
# view rows 0-2, all columns
print xls.worksheets["CAD_SDS"][0:2, :]
# change row 0 col 0 to "poop"
xls.worksheets["CAD_SDS"][0,0] = "poop"
# write the excel file
xls.write("Reference/Matching_XL2.xls")