ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Tool_Box/ExcelLib.py
Revision: 690
Committed: Wed May 13 15:29:53 2020 UTC (5 years, 10 months ago) by nino.borges
Content type: text/x-python
File size: 5220 byte(s)
Log Message:
trying to get the compiled version of filter arrow removed.

File Contents

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