# excelWin32ReadWrite
from win32com.client import Dispatch
class excelWin32ReadWrite:
def __init__(self,filename = None):
self.xlApp = Dispatch('Excel.Application')
if filename:
self.filename = filename
self.xlBook = self.xlApp.Workbooks.Open(filename)
else:
self.xlBook = self.xlApp.Workbooks.Add()
self.filename = ''
def save(self,newfilename = None):
if newfilename:
self.filename = newfilename
self.xlBook.SaveAs(newfilename)
else:
self.xlBook.Save()
def setCell(self,sheet,row,col,value):
sht = self.xlBook.Worksheets(sheet)
sht.Cells(row,col).Value = value
def getCell(self,sheet,row,col):
sht = self.xlBook.Worksheets(sheet)
return sht.Cells(row,col).Value
def getContiguosRange(self,sheet,row,col):
sht = self.xlBook.Worksheets(sheet)
bottom = row
while sht.Cells(bottom+1, col).Value not in [None,'']:
bottom = bottom+1
right = col
while sht.Cells(row, right+1).Value not in [None,'']:
right = right+1
return sht.Range(sht.Cells(row,col), sht.Cells(bottom,right)).Value
def renameSheet(self,sheet,newname):
self.xlBook.Worksheets(sheet).Name = newname
def deleteSheet(self,sheet):
self.xlBook.Worksheets(sheet).Delete
def returnAllSheetNames(self):
allSheets = self.xlBook.Sheets
sheetNames = []
for i in range(0,len(allSheets)):
sheetNames.append(self.xlBook.Sheets[i].Name)
return sheetNames
def close(self):
self.xlBook.Close(SaveChanges = 0)
del self.xlApp
No comments:
Post a Comment