ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Tool_Box/ExcelLib.py
Revision: 686
Committed: Thu May 7 20:59:05 2020 UTC (5 years, 10 months ago) by nino.borges
Content type: text/x-python
File size: 5170 byte(s)
Log Message:
Added methods to remove filter arrows and force auto fit columns

File Contents

# Content
1 """
2 ExcelLib
3
4 This will be an excel Library, making it easier to control Excel
5
6 """
7
8 from win32com.client import Dispatch
9
10 class ExcelConnection:
11 """A class that makes it easier to control Excel. Remember to save is your problem, as is error handling.
12 Operates on one workbook at a time."""
13
14 def __init__(self,fileName = None):
15 self.xlApp = Dispatch('Excel.Application')
16 if fileName:
17 self.fileName = fileName
18 self.xlBook = self.xlApp.Workbooks.Open(fileName)
19 else:
20 self.xlBook = self.xlApp.Workbooks.Add()
21 self.fileName = ''
22
23 def save(self, newFileName = None):
24 if newFileName:
25 self.fileName = newFileName
26 self.xlBook.SaveAs(newFileName)
27 else:
28 self.xlBook.Save()
29
30 def close(self):
31 self.xlBook.Close(SaveChanges=0)
32 del self.xlApp
33
34 def getCell(self, sheet, row, col):
35 "Get Value of one Cell"
36 sht = self.xlBook.Worksheets(sheet)
37 return sht.Cells(row,col).Value
38
39 def setCell(self, sheet, row, col, value):
40 "set value of one cell"
41 sht = self.xlBook.Worksheets(sheet)
42 sht.Cells(row,col).Value = value
43
44 def setCellNumberFormat(self, sheet, row, col, format):
45 """Sets the format for the cell, for date and other number type fields"""
46 sht = self.xlBook.Worksheets(sheet)
47 sht.Cells(row,col).NumberFormat = format
48
49 def setCellProperties(self, sheet, row, col, font = None, fontSize = None):
50 """A catch all for changing the properties of a cell. I can keep adding to this to avoid too many methods"""
51 sht = self.xlBook.Worksheets(sheet)
52 #sht.Cells(1,"A").AutoFilter(Field = 1, VisibleDropDown= False)
53 #sht.Range("A1:B1").AutoFilter(Field = 2, VisibleDropDown= False)
54 if font:
55 pass
56 if fontSize:
57 sht.Cells(row,col).Font.Size = fontSize
58
59 def getRange(self,sheet,row1,col1,row2,col2):
60 "return a 2d array (i.e. tuple of tuples)"
61 sht = self.xlBook.Worksheets(sheet)
62 return sht.Range(sht.Cells(row1,col1), sht.Cells(row2,col2)).Value
63
64 def setRange(self, sheet, leftCol, topRow, data, sheetRenameValue = None):
65 """insert a 2d array starting at given location.
66 works out the size needed for itself. optionally you can rename the sheet."""
67 bottomRow = topRow + len(data)-1
68 rightCol = leftCol + len(data[0]) -1
69 sht = self.xlBook.Worksheets(sheet)
70 sht.Range(
71 sht.Cells(topRow, leftCol),
72 sht.Cells(bottomRow, rightCol)).Value = data
73 if sheetRenameValue:
74 sht.Name = sheetRenameValue
75
76 def getContiguousRange(self, sheet, row, col):
77 """Tracks down and across from top left cell until it
78 encounters blank cells; returns the non-blank range.
79 Loost at first row and column; blanks at bottom or right
80 are OK and return None within the array."""
81 sht = self.xlBook.Worksheets(sheet)
82
83 # find the bottom row
84 bottom = row
85 while sht.Cells(bottom + 1, col).Value not in [None, '']:
86 bottom = bottom +1
87
88 # right column
89 right = col
90 while sht.Cells(row, right + 1).Value not in [None,""]:
91 right = right +1
92
93 return sht.Range(sht.Cells(row,col), sht.Cells(bottom, right)).Value
94
95 def fixStringsAndDates(self,aMatrix):
96 # converts all unicode strings and times
97 newMatrix = []
98 for row in aMatrix:
99 newRow = []
100 for cell in row:
101 if type(cell) is UnicodeType:
102 newRow.apppend(str(cell))
103 elif type(cell) is TimeType:
104 newRow.append(init(cel))
105 else:
106 newRow.append(cell)
107 newMatrix.append(tuple(newRow))
108 return newMatrix
109
110 def deleteColumnByLetter(self, sheet, colLetter):
111 """Deletes a single entire column"""
112 sht = self.xlBook.Worksheets(sheet)
113 sucessCode = sht.Columns(colLetter).EntireColumn.Delete()
114
115 def deleteColumnsByLetterList(self, sheet, colLetterList):
116 """Deletes multiple columns or colume ranges wiht a list"""
117 ## Example sheet.Range("D:E, H:H, J:K").EntireColumn.Delete
118 ## Above gives an error, so I'm resorting to using python to loop
119 pass
120
121 def removeHeaderFilterArrows(self, sheet, row, colStart, colEnd):
122 """Removes those filter arrows for all cells in a specified row."""
123 sht = self.xlBook.Worksheets(sheet)
124 for c in range(colStart,colEnd+1):
125 sht.Cells(1).AutoFilter(Field = c, VisibleDropDown= False)
126 #sht.Range("A1:B1").AutoFilter(Field = 2, VisibleDropDown= False)
127
128 def forceAutoFitRow(self, sheet, colStart, colEnd):
129 """Takes a colstart and colend and then performs an autofit on those columns."""
130 sht = self.xlBook.Worksheets(sheet)
131 sht.Columns("%s:%s"%(colStart, colEnd)).AutoFit()