Python 操作excel,python操作excel,首先安装python2.


首先安装python2.5,然后下载pywin32软件包(提供win com支持),大多数微软产品都作为com server,所以需要下这个包用作comclient与微软的产品通信

from win32com.client import constants, Dispatchclass 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 close(self):        self.xlBook.Close(SaveChanges=0)        del self.xlApp     def getCell(self, sheet, row, col):        "Get value of one cell"        sht = self.xlBook.Worksheets(sheet)        return sht.Cells(row, col).Value     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#该片段来自于http://byrx.net

评论关闭