-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathExcelFileReader.py
212 lines (182 loc) · 6.26 KB
/
ExcelFileReader.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# -*- coding: utf-8 -*-
""" Script by Toybich Egor
"""
import clr
from System import Type, Activator
try:
clr.AddReferenceByName('Microsoft.Office.Interop.Excel')
except:
clr.AddReferenceByName('Microsoft.Office.Interop.Excel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c')
import Microsoft.Office.Interop.Excel as Excel
from System.Runtime.InteropServices import Marshal
__version__ = '1.0.3'
class ExcelFileReader(object):
"""Excel Interop adapter class for reading a file
Methods (Excel numbers elements from 1, not 0!):
activesheet_set(num:int) :change active sheet number
g_xlr2list(cell1:str, cell2:str) --> list :gets range to list
g_xlr2dict(cell1:str, cell2:str, d_column=1:int) --> dict :gets range to dict with d_column as keys
g_active_xlr2list() --> list :gets used range to list
g_active_xlr2dict(d_column=1:int)) --> dict :gets used range to dict with d_column as keys
"""
__version__ = '1.0.2'
#======================================================================
@property
def filename(self):
return self._filename
#======================================================================
@classmethod
def kill_excel_processes(cls, times=10):
print('ExcelFileReader| Killing excel 10 times...')
oShell = Activator.CreateInstance(Type.GetTypeFromProgID("WScript.Shell"))
for _ in range(10):
oShell.Run("taskkill /im EXCEL.EXE",0 , True)
print('ExcelFileReader| Done killing...')
#======================================================================
def __init__(self, filename):
self._set_none()
self._filename = filename
self.application = Excel.ApplicationClass()
self.workbooks = self.application.Workbooks
try:
self.workbook = self.workbooks.Open(filename)
except:
print('ExcelFileReader| Incorrect path!')
raise
self.__del__
else:
self.worksheets = self.workbook.Worksheets
self.activesheet = self.worksheets[1]
self.application.Visible = False
self.application.DisplayAlerts = False
#======================================================================
def activesheet_set(self, num=1):
"""Change active sheet number
"""
try:
self.activesheet = self.worksheets[num]
except:
print('ExcelFileReader| Invalid sheet number!')
self.activesheet = self.worksheets[1]
#======================================================================
def g_xlr2list(self, cell1, cell2):
"""Gets excel range into a list
"""
xlrange = self.activesheet.Range[cell1,cell2]
list1 = self.xlr2list(xlrange)
Marshal.FinalReleaseComObject(xlrange)
xlrange = None
return list1
#======================================================================
def g_xlr2dict(self, cell1, cell2, d_column=1):
"""Gets excel range into a dictionary
"""
return self.list2dict(self.g_xlr2list(cell1, cell2), d_column-1)
#======================================================================
def g_active_xlr2list(self):
"""Gets used range in an active sheet into a dictionary
"""
xlrange = self.activesheet.UsedRange
list1 = self.xlr2list(xlrange)
Marshal.FinalReleaseComObject(xlrange)
xlrange = None
return list1
#======================================================================
def g_active_xlr2dict(self, d_column=1):
"""Gets used range in an active sheet into a dictionary
"""
xlrange = self.activesheet.UsedRange
list1 = self.xlr2list(xlrange)
Marshal.FinalReleaseComObject(xlrange)
xlrange = None
return self.list2dict(list1,d_column-1)
#======================================================================
@staticmethod
def list2dict(lrange, key_column=0):
"""Converts list to a dictionary with some column as keys
"""
key_column = int(key_column)
if key_column >= 0 or key_column < len(lrange[0]):
rdict = {}
for row in lrange:
cutrow = []
for i, elem in enumerate(row):
if not(i == key_column):
cutrow.append(elem)
if row[key_column] in rdict:
print('ExcelFileReader| Duplicate key found!')
else:
rdict[row[key_column]] = cutrow
return rdict
else:
print('ExcelFileReader| Invalid key number!')
return 0
raise
#======================================================================
@staticmethod
def xlr2list(xlrange):
"""Converts excel range to python list
"""
rows = xlrange.Rows
columns = xlrange.Columns
n = rows.Count
m = columns.Count
vals = [[0] * m for i in range(n)]
for i in range(n):
for j in range(m):
tmp = xlrange[i+1,j+1].Value2
try:
if int(tmp) == tmp:
tmp = int(tmp)
except:
# tmp = tmp.unicode('utf8')
pass
# print tmp
vals[i][j] = tmp
Marshal.FinalReleaseComObject(rows)
Marshal.FinalReleaseComObject(columns)
Marshal.FinalReleaseComObject(xlrange)
rows = None
columns = None
xlrange = None
return vals
#======================================================================
def __del__(self):
self.workbook.Close(False)
self.application.Quit()
Marshal.FinalReleaseComObject(self.activesheet)
Marshal.FinalReleaseComObject(self.worksheets)
Marshal.FinalReleaseComObject(self.workbooks)
Marshal.FinalReleaseComObject(self.workbook)
Marshal.FinalReleaseComObject(self.application)
self._set_none()
# del self.activesheet, self.worksheets, self.workbook
# del self.workbooks, self.application
#======================================================================
def _set_none(self):
self.activesheet = None
self.worksheets = None
self.workbooks = None
self.workbook = None
self.application = None
#======================================================================
def __repr__(self):
return 'ExcelInterface on file: {}'.format(self.filename)
#======================================================================
def __str__(self):
return self.filename
#======================================================================
def __exit__(self, exc_type, exc_value, traceback):
return True
self.__del__
#======================================================================
def __enter__(self):
return self
#======================================================================
def __len__(self):
cnt = 0
for dummy in self.worksheets:
cnt += 1
Marshal.FinalReleaseComObject(dummy)
dummy = None
return cnt