Python 读取excel
2016-03-31 16:32
579 查看
#! encoding=utf-8 import xlrd,xlwt import xml.dom.minidom import os,sys class OpExcel(): table = '' tabledata = [] def __init__(self,path,sheet): try: data = xlrd.open_workbook(path) except Exception, e: print str(e) sys.exit() self.table = data.sheets()[sheet] def readexcel(self,nrows=0,ncols=0,colnameindex=0): if (nrows == 0) and (ncols == 0): # 没有定义要读取的行数和列数就取excel中的行数和列数 nrows = self.table.nrows ncols = self.table.ncols # print self.table.nrows colnames = self.table.row_values(colnameindex) for row in range(1,nrows): rowdata = self.table.row_values(row) if rowdata: tmp = {} for i in range(0,len(colnames)): tmp[colnames[i]] = rowdata[i] self.tabledata.append(tmp) return self.tabledata def readexcel2(self,nrows=0,ncols=0): if (nrows == 0) and (ncols == 0): # 没有定义要读取的行数和列数就取excel中的行数和列数 nrows = self.table.nrows ncols = self.table.ncols for row in range(1,nrows): tmp = [] for col in range(0,ncols): tmp.append(self.table.cell(row,col).value) self.tabledata.append(tmp) return self.tabledata # def writeexcel(self,nrow,ncol,data): # self.table.cell(nrow,ncol).value = data if __name__ == "__main__": path = r'D:\Python\excel\2\23.xlsx' e = OpExcel(path,0) data = e.readexcel() print data
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Excel 曝出 Power Query 安全漏洞,1.2 亿用户易受远程 DDE 攻击
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例