python操作excel文件一(xlrd读取文件)
一般做接口测试,会把参数和一些数据放入excel表中,这样就不会重新编译代码,提高效率。一般如何操作呢?接下来跟着步骤一起学习吧
执行步骤:
1.首先要安装 xlrd这个模块,用 pip install xlrd
2.倒入这个模块
3.打开一个excel文件(建一个excel的对象)
4.获取到想要的sheet
5.得到想要的列的内容,或者行的内容,或者具体哪个单元格里面的内容
我有一个excel文件:3行4列,有三页,分别是sheet1,sheet2,sheet3
代码:
#!/usr/bin/env/python
# -*-coding:utf-8-*-
import xlrd class excelUse(object): #Object:得到这个对象,也就是打开xlsx文件
Object = xlrd.open_workbook("exceldemo1.xlsx") def getSheetsByIndex(self,index=0):
"""
通过索引得到
sheet: 得到第几页(index)的内容
:return: 返回第几页的内容
"""
sheet = self.Object.sheet_by_index(index) #通过sheet_by_index()这个方法,得到想要的具体的某一页
return sheet def getSheetsByName(self,name):
"""
通过名字得到
:param sheetName:得到所有的sheet,而且他的类型是一个list类型
:return:
"""
sheetNames = self.Object.sheet_names() #通过sheet_names()这个方法,得到所有页的名
print "sheetnames is {0},and the type is {1}".format(sheetNames, type(sheetNames))
sheet = self.Object.sheet_by_name(name) #通过sheet_by_name()这个方法,传入具体的sheet名字得到具体的sheet页
return sheet def getNumber(self):
"""
获取sheet的名字,行数,列数,通过属性 name,nrows,ncols(是number+rols/cols)
:return:
"""
print "获取sheet的名字",self.getSheetsByIndex(0).name
print "获取sheet的行数",self.getSheetsByIndex(0).nrows
print "获取sheet的列数",self.getSheetsByIndex(0).ncols def getRow(self,index=0):
"""
index: 第几行的内容,默认从0开始
:param index:
:return:
"""
print self.getSheetsByIndex(0).row_values(index)
print type(self.getSheetsByIndex(0).row_values(index)) def getCol(self,index=0):
"""
index:第几列的内容,默认从0开始
:return:
"""
print self.getSheetsByIndex(0).col_values(index)
print type(self.getSheetsByIndex(0).col_values(index))
def getCell(self,rowIndex,colIndex):
"""
获取具体的单元格的内容
rowIndex:第几行
colIndex:第几列
type:单元格数据的类型
:return:
"""
print self.getSheetsByIndex(0).cell_value(rowIndex,colIndex)
print self.getSheetsByIndex(0).cell(rowIndex,colIndex)
#print self.getSheetsByName("sheet1").cell(rowIndex,colIndex)
type = self.getSheetsByIndex(0).cell(rowIndex,colIndex).ctype
#ctype的类型 0:empty,1:string, 2:number, 3:date, 4: boolean, 5: error
print type if __name__=="__main__":
obj = excelUse()
obj.getRow(0)
obj.getCol(0)
obj.getCell(0,0)
obj.getNumber()
python操作excel文件一(xlrd读取文件)的更多相关文章
- python操作excel之 模块 xlrd (详解)
二.使用介绍 1.导入模块 import xlrd 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFile.xls') 3.使用技巧 获取一个工作表 ...
- python操作Excel读写--使用xlrd
一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 二.使用介绍 1.导入模块 import x ...
- python操作Excel读--使用xlrd
一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 二.使用介绍 1.导入模块 import x ...
- python操作excel之 模块 xlrd
xlrd是专门用来在python中读取微软execel的模块,可以自己直接下载安装,也可以通过包管理器安装. 官方资料: 下载地址:http://pypi.python.org/pypi/xlrd 官 ...
- python操作Excel读写--使用xlrd (转)
(转自:http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html) 一.安装xlrd模块 到python官网下载http://pypi ...
- python操作Excel读写--使用xlrd和xlwt
一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 进入到解压文件路径,输入 setup.py ...
- python操作Excel读写(使用xlrd和xlrt)
包下载地址:https://pypi.python.org/pypi/xlrd 导入 import xlrd 打开excel data = xlrd.open_workbook('demo.xls ...
- Python操作Excel
一.系统性学习 对于操作Excel,需要Xlrd/xlwt这两个模块,下面推荐出系统性学习的网址: python操作Excel读写--使用xlrd 官方文档 Python 使用 Xlrd/xlwt 操 ...
- 【python小记】python操作excel文件
题记: 最近因为工作需要,学习了python,瞬间对这个轻松快捷的语给吸引了,以前只知道js脚本是写网页的,没有想到python这个脚本语言的应用范围可以这么广泛,现在做一些简单或稍微复杂的操作,基本 ...
- python操作excel表格(xlrd/xlwt)
最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...
随机推荐
- linux audit审计(7)--读懂audit日志
让我们先来构造一条audit日志.在home目录下新建一个目录,然后配置一条audit规则,对这个目录的wrax,都记录审计日志: auditctl -w /home/audit_test -p wr ...
- python设计模式第十七天【解释器模式】
1.应用场景 (1)解释预先定义的文法 2.代码实现 #!/usr/bin/env python #!_*_ coding:UTF-8 _*_ from abc import ABCMeta, abs ...
- APP test
在讲APP测试之前,先讲一下,目前APP的操作系统以及APP相关基础知识. 一.APP基础知识 1.操作系统# 现在移动端的操作系统主流的分为两种:(1)安卓系统 (2)IOS系统. 2.安卓系统# ...
- 魔术方法:__set、__get
__set: 在设置对象里边不能直接设置(或没有)的属性值的时候,自动去被调用 class Track { private $track_name; public function __set($na ...
- 解决post、get端中文乱码问题
在web.xml中配置: <filter> <filter-name>CharacterEncodingFilter</filter-name> <filte ...
- Vue渲染函数
前面的话 Vue 推荐在绝大多数情况下使用 template 来创建HTML.然而在一些场景中,真的需要 JavaScript 的完全编程的能力,这就是 render 函数,它比 template 更 ...
- How to write to an event log by using Visual C#
using System; using System.Diagnostics; namespace WriteToAnEventLog_csharp { /// Summary description ...
- 【Python】Python-Numpy教程
Numpy的使用 读txt数据: · genfromtxt import numpy as np print(help(np.genfromtxt)) #data = np.genfromtxt(&q ...
- Android 模块化/热修复/插件化 框架选用
概念汇总 动态加载:在程序运行的时候,加载一些程序自身原本不存在的文件并运行这些文件里的代码逻辑.动态加载是热修复与插件化实现的基础. 热修复:修改部分代码,不用重新发包,在用户不知情的情况下,给ap ...
- python之旅第八篇--异常
判断类与对象关系 isinstance #判断对象obj是否是由cls类创建的 class Foo(object): pass obj = Foo() print isinstance(obj,Foo ...