from win32com.client import constants, Dispatch

class EasyExcel:

    def __init__(self, filename=None):
        self.xlApp = Dispatch('Excel.Application')
        if filename:
            self.filename = filename
            self.xlBook = self.xlApp.Workbooks.Open(filename)
        else:
            print "please input the filename"

    def Save(self, newfilename=None):  
        if newfilename:  
            self.filename = newfilename  
            self.xlBook.SaveAs(newfilename)  
        else:  
            self.xlBook.Save() 

    def Close(self):
        self.xlBook.Close(SaveChanges=1)
        del self.xlApp
        
    def Delect(self, sheetName):
        shts = self.xlBook.Worksheets(sheetName)
        shts.Delect()
    
    def __worksSheet(self, sheet):
        return self.xlBook.Worksheets(sheet)
     
    def getCell(self, sheet, row=None, col=None):
        "Get value of one cell"
        sht = self.__worksSheet(sheet)
        if row or col:
#            return sht.Cells().Value
            return sht.Cells(row, col).Value
        else:
            return sht.Cells.Value
    
    def setCellValue(self, sheet, row=None, col=None, value=None):
        "set value of one cell"
        sht = self.__worksSheet(sheet)
        if row or col:
#            return sht.Cells().Value
            sht.Cells(row, col).Value = value
        else:
            pass
   
    def setRange(self, sheet, row1, col1, row2, col2,valueList):
        "return a 2d array (i.e. tuple of tuples)"
        sht = self.xlApp.Worksheets(sheet)
        sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value = tuple(valueList)
   
    def getRange(self, sheet, row1, col1, row2, col2):
        "return a 2d array (i.e. tuple of tuples)"
        sht = self.xlApp.Worksheets(sheet)
        return sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value
    
    def iRowNum(self, sheet):
        """Get Max Row Num"""
        sht = self.__worksSheet(sheet)
        if sht:
            return sht.Usedrange.rows.count
        pass

    def iColNum(self, sheet):
        """Get Max Column Num"""
        sht = self.__worksSheet(sheet)
        if sht:
            return sht.Usedrange.Columns.count
        pass
    
    def getSheetName(self):
        shts = self.xlBook.Worksheets
        strShtName = []
        for i in range(shts.count):
            strShtName.append(shts(i+1).Name)
        return strShtName
    
    def addSheet(self, sheetName):
        worksheet = self.xlApp.Worksheets.Add(self.xlApp.Worksheets[self.xlApp.Worksheets.Count-1])
        worksheet.Name = sheetName
    
    def addExcel(self, excelName):
        newExcel = self.xlApp.Workbooks.add
        newExcel.SaveAs(excelName)
    
    def cpSheet(self, sourceSheet, copySheet):  
        "copy sheet"  
        shts = self.xlBook.Worksheets  
        shts(sourceSheet).Copy(None, shts(sourceSheet))  
        shts(shts(sourceSheet).index+1).Name =copySheet
        
    def listContent(self, sheet, row1=None, col1=None, row2=None, \
                    col2=None, rangeContent = None):
#        excelProxy = 
        liContent = []
#        sht = self.xlApp.Worksheets(sheet)
        iColNum = self.iColNum(sheet)
        iRowNum = self.iRowNum(sheet)
        for y in range(1, iRowNum):
            rangeContent = self.getRange(Sheet,1, y, iColNum,y)
#        content = excelA.getCell("Main", y, x) 
#        liInContent.append(content)    
#        if any(map((lambda x:x is not None), rangeContent)):
            if list(set(rangeContent)) != [(None,)]:
                liContent.append(rangeContent)    
        returnliContent
def margeContent(ContentA, ContentB):
    "margeContent"
    listMarge = []
    flag =False
    rowCountA = ContentA[0].index((None,))
    rowCountB = ContentB[0].index((None,))
    NoneList = [(None,),]
    for conA in ContentA:
        listMarge.append(list(conA[0:rowCountA]))
    for conB in ContentB:
        for i in range(len(ContentA)):
            if listMarge[i][0]==conB[0]:
                listMarge[i].extend(list(conB[0:rowCountB]))
                flag = True
                break
        if flag:
            flag = False
        else:
            listMarge.append(list(conB[0:1]))
            listMarge[i+1].extend(NoneList * (rowCountA-1))
            listMarge[i+1].extend(list(conB[0:rowCountB]))
            flag = False
    return listMarge

 

posted on 2012-06-12 11:35  Wandy.Ying  阅读(261)  评论(0)    收藏  举报