使用python操作excel
2006-11-03 17:07
603 查看
首先安装python2.5,然后下载pywin32软件包(提供win com支持),大多数微软产品都作为com server,所以需要下这个包用作com client与微软的产品通信
以下是一段 python脚本用来读取excel,希望可以抛砖引玉
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 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
注意:上面的getRange方法会返回一个 tuple的数据结构
调用脚本如下
from easyExcel import EasyExcel
excelProxy = EasyExcel("d:/test.xls")
content=excelProxy.getRange("sheet1",1,1,2,2)
print content
注意:脚本写的不全只有部分读取的方法,其他可以依次类推
以下是一段 python脚本用来读取excel,希望可以抛砖引玉
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 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
注意:上面的getRange方法会返回一个 tuple的数据结构
调用脚本如下
from easyExcel import EasyExcel
excelProxy = EasyExcel("d:/test.xls")
content=excelProxy.getRange("sheet1",1,1,2,2)
print content
注意:脚本写的不全只有部分读取的方法,其他可以依次类推
相关文章推荐
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- 关于python 自带csv库的使用心得 附带操作实例以及excel下乱码的解决
- Python使用xlrd模块操作Excel数据导入的方法
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd(转)
- python 使用pandas,完成对excel的操作: 遍历,求偏度(skew)的小程序
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- python操作Excel--使用xlrd
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd