ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Tool_Box/ExcelLib.py
Revision: 679
Committed: Mon May 4 19:41:49 2020 UTC (5 years, 10 months ago) by nino.borges
Content type: text/x-python
File size: 4025 byte(s)
Log Message:
Added some methods to delete columns and a method to change the format of a number/date cell.

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 568 def getRange(self,sheet,row1,col1,row2,col2):
50     "return a 2d array (i.e. tuple of tuples)"
51     sht = self.xlBook.Worksheets(sheet)
52     return sht.Range(sht.Cells(row1,col1), sht.Cells(row2,col2)).Value
53    
54 nino.borges 649 def setRange(self, sheet, leftCol, topRow, data, sheetRenameValue = None):
55 nino.borges 568 """insert a 2d array starting at given location.
56 nino.borges 649 works out the size needed for itself. optionally you can rename the sheet."""
57     bottomRow = topRow + len(data)-1
58 nino.borges 568 rightCol = leftCol + len(data[0]) -1
59     sht = self.xlBook.Worksheets(sheet)
60     sht.Range(
61     sht.Cells(topRow, leftCol),
62     sht.Cells(bottomRow, rightCol)).Value = data
63 nino.borges 649 if sheetRenameValue:
64     sht.Name = sheetRenameValue
65 nino.borges 568
66     def getContiguousRange(self, sheet, row, col):
67     """Tracks down and across from top left cell until it
68     encounters blank cells; returns the non-blank range.
69     Loost at first row and column; blanks at bottom or right
70     are OK and return None within the array."""
71     sht = self.xlBook.Worksheets(sheet)
72    
73     # find the bottom row
74     bottom = row
75     while sht.Cells(bottom + 1, col).Value not in [None, '']:
76     bottom = bottom +1
77    
78     # right column
79     right = col
80     while sht.Cells(row, right + 1).Value not in [None,""]:
81     right = right +1
82    
83     return sht.Range(sht.Cells(row,col), sht.Cells(bottom, right)).Value
84    
85     def fixStringsAndDates(self,aMatrix):
86     # converts all unicode strings and times
87     newMatrix = []
88     for row in aMatrix:
89     newRow = []
90     for cell in row:
91     if type(cell) is UnicodeType:
92     newRow.apppend(str(cell))
93     elif type(cell) is TimeType:
94     newRow.append(init(cel))
95     else:
96     newRow.append(cell)
97     newMatrix.append(tuple(newRow))
98 nino.borges 679 return newMatrix
99    
100     def deleteColumnByLetter(self, sheet, colLetter):
101     """Deletes a single entire column"""
102     sht = self.xlBook.Worksheets(sheet)
103     sucessCode = sht.Columns(colLetter).EntireColumn.Delete()
104    
105     def deleteColumnsByLetterList(self, sheet, colLetterList):
106     """Deletes multiple columns or colume ranges wiht a list"""
107     ## Example sheet.Range("D:E, H:H, J:K").EntireColumn.Delete
108     ## Above gives an error, so I'm resorting to using python to loop
109     pass