-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexcelparse2007andmore.py
More file actions
410 lines (341 loc) · 17.3 KB
/
excelparse2007andmore.py
File metadata and controls
410 lines (341 loc) · 17.3 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# coding=UTF-8
import os;
import copy;
from openpyxl.styles import Color,colors,Fill,PatternFill,Font,fills;
from openpyxl import load_workbook;
from openpyxl import utils
from openpyxl.cell import Cell;
from openpyxl.worksheet.worksheet import Worksheet;
from openpyxl.worksheet import *;
import fileutil;
#这个是这类的函数测试用 因为如果在雷利加代码测试不方便 容易弄错 所以
def read_excel2007(fileName):
wb = load_workbook(fileName,data_only=True);
# print("Worksheet range(s):" , wb.get_named_ranges());
# print("Worksheet name(s):" , wb.get_sheet_names());
# sheetnames = wb.get_sheet_names();
ws = wb.get_active_sheet();
# print(ws.get_highest_row());
print(ws.cell(row=1,column=2).value);
ws.cell(row=3,column=2).value = "jiayou";
wb.save(filename);
# print(ws.merged_cell_ranges);
# print(ws.merged_cells);
# print(ws.cell('C2').value);
# print(ws.merged_cell_ranges[1]);
# print(utils.range_boundaries(ws.merged_cell_ranges[1]));
# print(utils.column_index_from_string(ws.merged_cells[1]));
# print(ws.name);
# print("*****************");
class SheetIsNotExist(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class ColumnIsNotVaild(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class ReadExcel2007:
wb = None;
path = "";
sheet = None;
currentSheetName = None;
mergeCellsColumnAndRow = [];#是这种形式的(2,1,3,2)
mergeCellsCharAndIndex = [];#是这种形式的(B1:C2)
currentSheetIndex = 0;#-1代表没有
def __init__(self,path,sheetIndex = 0):
if(os.path.exists(path)==False):
raise fileutil.FileIsNotExist(path + " 不存在 ");
"""
*** 下面这个data_only是用来指示到底是读取数值 还是公式 但是如果设置为读取数值
*** 但是原来是公式 那么如果只是读文件 那么没有问题 如果也保存了数据 那么原来的公式 就会被数值替代 没有了公式
"""
self.wb = load_workbook(path,data_only=True);
sheetName = self.wb.get_sheet_names();
# print(sheetName);
if len(sheetName) != 0:
self.path = path;
self.sheet = self.wb.get_sheet_by_name(sheetName[0]);
self.curentSheetIndex = sheetIndex;
self.currentSheetName = sheetName[sheetIndex];
self.__covertStringToColumnAndRow();
self.__setMergeCellsCharAndIndex();
else:
raise SheetIsNotExist("第 "+sheetIndex+" "+"sheet 不存在");
def setCurrentSheetByIndex(self,sheetIndex):
sheetName = self.wb.get_sheet_names();
if sheetIndex > len(sheetName) and sheetIndex <0:
raise SheetIsNotExist("第 "+sheetIndex+" "+"sheet 不存在");
if sheetIndex != self.curentSheetIndex:
self.curentSheetIndex = sheetIndex;
self.currentSheetName = sheetName[sheetIndex];
self.sheet = self.wb.get_sheet_by_name(sheetName[0]);
self.__covertStringToColumnAndRow();
self.__setMergeCellsCharAndIndex();
def setCurrentSheetByName(self,sheetName):
sheetNameList = self.wb.get_sheet_names();
if sheetName not in sheetNameList:
raise SheetIsNotExist(" 名为 "+sheetName+" "+" 的sheet 不存在");
if str(sheetName)!=self.currentSheetName:
suoyin = list(sheetNameList).index(str(sheetName).strip());
self.curentSheetIndex = suoyin;
self.currentSheetName = sheetName[sheetName];
self.sheet = self.wb.get_sheet_by_name(sheetName[0]);
self.__covertStringToColumnAndRow();
self.__setMergeCellsCharAndIndex();
#这个函数会把合并单元格的内容 给每个合并单元格 hang与lie 都是从1 开始
def getCellValueByColumnAndRow(self,hang,lie):
for i in self.mergeCellsColumnAndRow:
if hang>=i[1] and hang<=i[3] and lie>=i[0] and lie <=i[2]:
return self.sheet.cell(row=i[1],column=i[0]).value;
# print("else");
return self.sheet.cell(row=hang,column=lie).value;
#使用的时候excel 必须关闭 否则会报权限不足 hang与lie 都是从1 开始
def setCellValueByColumnAndRow(self,hang,lie,value = None):
for i in self.mergeCellsColumnAndRow:
if hang>=i[1] and hang<=i[3] and lie>=i[0] and lie <=i[2]:
self.__setDanYuanGeWidth(utils.get_column_letter(i[0]));
self.sheet.cell(row=i[1],column=i[0]).value = value;
self.wb.save(self.path);
self.__setDanYuanGeWidth(utils.get_column_letter(lie));
self.sheet.cell(row=hang,column=lie).value = value;
self.wb.save(self.path);
#B2这种形式给出
def getCellValueByCharAndRow(self,stringColumn,row):
stringColumn = str(stringColumn).upper().strip();
for i in self.mergeCellsCharAndIndex:
if stringColumn>=i[0] and stringColumn<=i[2] and row>=i[1] and row <=i[3]:
return self.sheet.cell(str(i[0]+str(i[1]))).value;
return self.sheet.cell(str(stringColumn)+str(row)).value;
#使用的时候excel 必须关闭 否则会报权限不足
def setCellValueByCharAndRow(self,stringColumn,row,value=None):
stringColumn = str(stringColumn).upper().strip();
for i in self.mergeCellsCharAndIndex:
if stringColumn>=i[0] and stringColumn<=i[2] and row>=i[1] and row <=i[3]:
pinJieStr = str(i[0]).strip()+str(i[1]).strip();
self.__setDanYuanGeWidth(str(i[0]).strip());
self.sheet.cell(pinJieStr).value = value;
self.wb.save(self.path);
return;
self.__setDanYuanGeWidth(str(stringColumn));
self.sheet.cell(str(stringColumn)+str(row)).value = value;
self.wb.save(self.path);
def __setDanYuanGeWidth(self,cellName,widthValue = None):
cellName.strip();
self.sheet.cell(coordinate = cellName+str(1));
# print(self.sheet.column_dimensions);
width = self.sheet.column_dimensions[cellName].width;
if(widthValue is not None):
self.sheet.column_dimensions[cellName].width = widthValue;# self.sheet.column_dimensions[cellName].width;
elif(width is not None and width != 0):
self.sheet.column_dimensions[cellName].width = self.sheet.column_dimensions[cellName].width;
else:
self.sheet.column_dimensions[cellName].width = 10;
# self.sheet.column_dimensions[cellName].width = 10;
#通过“2 3” 2代表第二行 3代表 第三列 这种形式来设置单元格的颜色
def setCellColorByColumnAndRow(self,hang,lie,R=255,G=255,B=255):
if(lie<0 ):
raise ColumnIsNotVaild("列 不能小于0");
if(hang<0 ):
raise ColumnIsNotVaild(str(hang)+"不能小于0行");
if(R==255 and G==255 and B == 255):
fill = PatternFill(patternType = fills.FILL_NONE,fgColor=Color(rgb=self.rgbConvertToHex(R,G,B)));
else:
fill = PatternFill(patternType = fills.FILL_SOLID,fgColor=Color(rgb=self.rgbConvertToHex(R,G,B)));
for i in self.mergeCellsColumnAndRow:
if hang>=i[1] and hang<=i[3] and lie>=i[0] and lie <=i[2]:
for neiHang in range(i[1],i[3]+1):
for neiCol in range(i[0],i[2]+1):
self.sheet.cell(row=neiHang,column=neiCol).fill = fill;
self.wb.save(self.path);
return;
self.sheet.cell(row=hang,column=lie).fill = fill;
self.wb.save(self.path);
#得到 当前单元格的 颜色 有两种可能 就是如果 是纯颜色 那么返回(R,G,B) 如果是主题颜色
#会返回{'theme':fgColor.value,'persentage':fgColor.tint};
#其中theme 是主题 就是对应excel 中主体颜色 tint 代表是主题颜色的稀释率 至于主题颜色的RGB值 python 获得不了
def getCellColorByColumnAndRow(self,hang,lie):
if(lie<0 ):
raise ColumnIsNotVaild("列 不能小于0");
if(hang<0 ):
raise ColumnIsNotVaild(str(hang)+"不能小于0行");
cell = self.sheet.cell(row=hang,column=lie);
for i in self.mergeCellsColumnAndRow:
if hang>=i[1] and hang<=i[3] and lie>=i[0] and lie <=i[2]:
cell = self.sheet.cell(row=i[1],column=i[0]);
if(cell.fill.fgColor.type =='rgb'):
return self.__colorHexStringToRGB(cell.fill.fgColor.rgb);
elif(cell.fill.fgColor.type =='theme'):
fgColor = cell.fill.fgColor;
return {'theme':fgColor.value,'persentage':fgColor.tint};
if(cell.fill.fgColor.type =='rgb'):
return self.__colorHexStringToRGB(cell.fill.fgColor.rgb);
elif(cell.fill.fgColor.type =='theme'):
fgColor = cell.fill.fgColor;
return {'theme':fgColor.value,'persentage':fgColor.tint};
#将RGB 转换为 #16进制 如 (R,G,B)=(255,255,255) ->"00FFFFFF"
def rgbConvertToHex(self,R,G,B):
myHex = "00";#表示alpha 透明度
myHex+= "%02x" %(R);
myHex+= "%02x" %(G);
myHex+= "%02x" %(B);
return str(myHex).upper();
#通过“B2”这种形式来设置单元格的颜色
def setCellColorByCharAndRow(self,stringColumn,row,R=255,G=255,B=255):
if(utils.column_index_from_string(stringColumn.strip())<0 ):
raise ColumnIsNotVaild("列 不能小于0");
if(row<0 ):
raise ColumnIsNotVaild(str(row)+"不能小于0行");
if(R==255 and G==255 and B == 255):
fill = PatternFill(patternType = fills.FILL_NONE,fgColor=Color(rgb=self.rgbConvertToHex(R,G,B)));
else:
fill = PatternFill(patternType = fills.FILL_SOLID,fgColor=Color(rgb=self.rgbConvertToHex(R,G,B)));
stringColumn = str(stringColumn).upper().strip();
# print("hdsjafaksldf");
# print(self.mergeCellsColumnAndRow);
for i in self.mergeCellsCharAndIndex:
# print("stringcolumn "+str(stringColumn));
if stringColumn>=str(i[0]).upper() and stringColumn<=str(i[2]).upper() and row>=i[1] and row <=i[3]:
for neiHang in range((i[1]),i[3]+1):
mybegin = utils.column_index_from_string(i[0]);
myend = utils.column_index_from_string(i[2]);
for neiCol in range(mybegin,myend+1):
self.sheet.cell(row=neiHang,column=neiCol).fill = fill;
self.wb.save(self.path);
return;
self.sheet.cell(str(stringColumn)+str(row)).fill = fill;
self.wb.save(self.path);
#得到 当前单元格的 颜色 有两种可能 就是如果 是纯颜色 那么返回(R,G,B) 如果是主题颜色
# 会返回{'theme':fgColor.value,'persentage':fgColor.tint};
# 其中theme 是主题 就是对应excel 中主体颜色 tint 代表是主题颜色的稀释率 至于主题颜色的RGB值 python 获得不了
def getCellColorByCharAndRow(self,stringColumn,row):
if(utils.column_index_from_string(stringColumn.strip())<0 ):
raise ColumnIsNotVaild("列 不能小于0");
if(row<0 ):
raise ColumnIsNotVaild(str(row)+"不能小于0行");
stringColumn = str(stringColumn).upper().strip();
cell = self.sheet.cell(str(stringColumn)+str(row));
for i in self.mergeCellsCharAndIndex:
# print("stringcolumn "+str(stringColumn));
if stringColumn>=str(i[0]).upper() and stringColumn<=str(i[2]).upper() and row>=i[1] and row <=i[3]:
cell = self.sheet.cell(str(i[0])+str(i[1]));
if(cell.fill.fgColor.type =='rgb'):
return self.__colorHexStringToRGB(cell.fill.fgColor.rgb) ;
elif(cell.fill.fgColor.type =='theme'):
fgColor = cell.fill.fgColor;
return {'theme':fgColor.value,'persentage':fgColor.tint};
# for i in self.sheet.cell(str(stringColumn)+str(row)).fill.fgColor:
# print (i);
# print(self.sheet.cell(str(stringColumn)+str(row)).fill.fgColor.tint);
if(cell.fill.fgColor.type=='rgb'):
return self.__colorHexStringToRGB(cell.fill.fgColor.rgb);
elif(cell.fill.fgColor.type=='theme'):
fgColor = cell.fill.fgColor;
return {'theme':fgColor.value,'persentage':fgColor.tint};
#用于转换"00ff0000" 到 r = 255 G = 0 B =0;
def __colorHexStringToRGB(self,hexString):
if(hexString is None):
return None;
print(hexString)
if(len(hexString)==6):
R = hexString[0:2];
G = hexString[2:4];
B = hexString[4:6];
R = int(R,16);
G = int(G,16);
B = int(B,16);
return (R,G,B);
if (len(hexString)==8):
R = hexString[2:4];
G = hexString[4:6];
B = hexString[6:8];
R = int(R,16);
G = int(G,16);
B = int(B,16);
return (R,G,B);
#返回当前sheet的整个内容 以列表里面再列表返回 第一层的列表的每个元素对应excel里的列 而不是行
def getAllSheet(self):
rowLength = self.sheet.get_highest_row();
colLength = self.sheet.get_highest_column();
sheetListHang = [];
sheetLisLie = [];
# print("colLength"+str(colLength));
# print("rowLength"+str(rowLength));
for i in range(1,colLength+1):
for j in range(1,rowLength+1):
# print("i= "+str(i)+" j= "+str(j));
sheetLisLie.append(self.getCellValueByColumnAndRow(j,i));
sheetListHang.append(copy.deepcopy(sheetLisLie));
sheetLisLie.clear();
return sheetListHang;
#返回当前sheet的指定行的内容 如果当前行超过了 最大行 或者小于0则会抛出异常
def getOneRow(self,whichRow):
if(whichRow<0 ):
raise ColumnIsNotVaild(str(whichRow)+"不能小于0行");
if( whichRow >self.sheet.get_highest_column()):
raise ColumnIsNotVaild(str(whichRow)+"行已经大于"+str(self.sheet.get_highest_row())+"这是最大行");
colLength = self.sheet.get_highest_column();
sheetListHang = [];
# print("colLength"+str(colLength));
for i in range(1,colLength+1):
sheetListHang.append(self.getCellValueByColumnAndRow(whichRow,i))
return sheetListHang;
#返回当前sheet的指定列的内容 如果当前列超过了 最大列 或者小于0则会抛出异常
def getOneColumn(self,whichcolumn):
if(whichcolumn<0 ):
raise ColumnIsNotVaild("列 不能小于0");
if( whichcolumn >self.sheet.get_highest_column()):
raise ColumnIsNotVaild(str(whichcolumn)+"列已经大于"+str(self.sheet.get_highest_column)+"这是最大列");
rowLength = self.sheet.get_highest_row();
sheetListHang = [];
# print("colLength"+str(rowLength));
for j in range(1,rowLength+1):
sheetListHang.append(self.getCellValueByColumnAndRow(j,whichcolumn));
return sheetListHang;
#得到当前sheet表的列的个数
def getNumbersOfColumn(self):
return self.sheet.get_highest_column();
#得到当前sheet表的行的个数
def getNumbersOfRow(self):
return self.sheet.get_highest_Row();
#得到当前excel的表的个数
def getNumbersOfSheet(self):
return len(self.wb.get_sheet_names());
def getNamesOfSheet(self):
return list(self.wb.get_sheet_names());
#内部调用 用来转换(B1:C2)->(2,1,3,2) 将所有的合并单元格转换成这种形式 最后加入mergeCellsColumnAndRow列表中
def __covertStringToColumnAndRow(self):
# print(self.sheet.merged_cell_ranges)
self.mergeCellsColumnAndRow = [utils.range_boundaries(x) for x in self.sheet.merged_cell_ranges];
# print(self.mergeCellsColumnAndRow);
#内部调用 用来转换(B1:C2)->('B',1,'C',2) 将所有的合并单元格转换成这种形式 最后加入mergeCellsCharAndIndex列表中
def __setMergeCellsCharAndIndex(self):
beifen = [utils.range_boundaries(x) for x in self.sheet.merged_cell_ranges];
for i in beifen:
t0 = utils.get_column_letter(i[0]);
t2 = utils.get_column_letter(i[2]);
self.mergeCellsCharAndIndex.append((t0,i[1],t2,i[3]));
# print(self.mergeCellsCharAndIndex);
if(__name__=="__main__"):
filename = r'd:\ceshi.xlsx';
# read_excel2007(filename);
a = ReadExcel2007(filename);
print(a.getCellValueByColumnAndRow(1,2));
print(a.getCellValueByColumnAndRow(8,6));
print(a.getCellValueByCharAndRow('G',10));
a.setCellValueByCharAndRow("G",10,"张");
a.setCellValueByCharAndRow("AB",10,"张");
a.setCellValueByColumnAndRow(8,6,"chen");
a.setCellColorByCharAndRow('G',10,255,0,0);
a.setCellColorByColumnAndRow(9,6,0,0,255);
print("&&&&&&&&&&&&&&&&&");
print(Color(rgb=colors.BLUE).rgb);
print(a.getCellColorByCharAndRow("f",5));
print(a.getCellColorByColumnAndRow(5,6));
# print(a.getAllSheet());
# print(a.getOneRow(1));
# print(a.getNamesOfSheet());
# print(a.getNumbersOfSheet());
# print(a.getOneColumn(1));