ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Tool_Box/ExcelLib.py
Revision: 682
Committed: Thu May 7 20:06:07 2020 UTC (5 years, 10 months ago) by nino.borges
Content type: text/x-python
File size: 4531 byte(s)
Log Message:
Added a SetCellProperties method as a catch all for performing multiple changes to cell properties like font, font size etc.

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     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 nino.borges 649
39 nino.borges 568 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 nino.borges 679 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 nino.borges 682 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 nino.borges 568 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 nino.borges 649 def setRange(self, sheet, leftCol, topRow, data, sheetRenameValue = None):
65 nino.borges 568 """insert a 2d array starting at given location.
66 nino.borges 649 works out the size needed for itself. optionally you can rename the sheet."""
67     bottomRow = topRow + len(data)-1
68 nino.borges 568 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 nino.borges 649 if sheetRenameValue:
74     sht.Name = sheetRenameValue
75 nino.borges 568
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 nino.borges 679 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