(转自:http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html)

一、安装xlrd模块

到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境。

二、使用介绍

  1、导入模块

import xlrd

   2、打开Excel文件读取数据

data = xlrd.open_workbook('excelFile.xls')

3、使用技巧

获取一个工作表

        table = data.sheets()[0]          #通过索引顺序获取
 
        table = data.sheet_by_index(0) #通过索引顺序获取
        table = data.sheet_by_name(u'Sheet1')#通过名称获取
 
        获取整行和整列的值(数组)
   

         table.row_values(i)
 
         table.col_values(i)
 
        获取行数和列数
  

        nrows = table.nrows
 
        ncols = table.ncols

        循环行列表数据
        for i in range(nrows ):
      print table.row_values(i)
 
单元格
cell_A1 = table.cell(0,0).value
 
cell_C4 = table.cell(2,3).value
 
使用行列索引
cell_A1 = table.row(0)[0].value
 
cell_A2 = table.col(1)[0].value
 
简单的写入
row = 0
 
col = 0
 
# 类型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
ctype = 1 value = '单元格的值'
 
xf = 0 # 扩展的格式化
 
table.put_cell(row, col, ctype, value, xf)
 
table.cell(0,0)  #单元格的值'
 
table.cell(0,0).value #单元格的值'
 

三、Demo代码

Demo代码其实很简单,就是读取Excel数据。

# -*- coding: utf-8 -*-
import xdrlib ,sys
import xlrd
def open_excel(file= 'file.xls'):
try:
data = xlrd.open_workbook(file)
return data
except Exception,e:
print str(e)
#根据索引获取Excel表格中的数据 参数:file:Excel文件路径 colnameindex:表头列名所在行的所以 ,by_index:表的索引
def excel_table_byindex(file= 'file.xls',colnameindex=0,by_index=0):
data = open_excel(file)
table = data.sheets()[by_index]
nrows = table.nrows #行数
ncols = table.ncols #列数
colnames = table.row_values(colnameindex) #某一行数据
list =[]
for rownum in range(1,nrows): row = table.row_values(rownum)
if row:
app = {}
for i in range(len(colnames)):
app[colnames[i]] = row[i]
list.append(app)
return list #根据名称获取Excel表格中的数据 参数:file:Excel文件路径 colnameindex:表头列名所在行的所以 ,by_name:Sheet1名称
def excel_table_byname(file= 'file.xls',colnameindex=0,by_name=u'Sheet1'):
data = open_excel(file)
table = data.sheet_by_name(by_name)
nrows = table.nrows #行数
colnames = table.row_values(colnameindex) #某一行数据
list =[]
for rownum in range(1,nrows):
row = table.row_values(rownum)
if row:
app = {}
for i in range(len(colnames)):
app[colnames[i]] = row[i]
list.append(app)
return list def main():
tables = excel_table_byindex()
for row in tables:
print row tables = excel_table_byname()
for row in tables:
print row if __name__=="__main__":
main()

python操作Excel读写--使用xlrd (转)的更多相关文章

  1. python操作Excel读写--使用xlrd和xlwt

    一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 进入到解压文件路径,输入 setup.py  ...

  2. python操作Excel读写--使用xlrd

    一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 二.使用介绍 1.导入模块 import x ...

  3. python操作Excel读写(使用xlrd和xlrt)

    包下载地址:https://pypi.python.org/pypi/xlrd   导入 import xlrd 打开excel data = xlrd.open_workbook('demo.xls ...

  4. python操作Excel读--使用xlrd

    一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 二.使用介绍 1.导入模块 import x ...

  5. python操作excel之 模块 xlrd

    xlrd是专门用来在python中读取微软execel的模块,可以自己直接下载安装,也可以通过包管理器安装. 官方资料: 下载地址:http://pypi.python.org/pypi/xlrd 官 ...

  6. python操作excel之 模块 xlrd (详解)

    二.使用介绍 1.导入模块 import xlrd 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFile.xls') 3.使用技巧 获取一个工作表 ...

  7. Python操作Excel

    一.系统性学习 对于操作Excel,需要Xlrd/xlwt这两个模块,下面推荐出系统性学习的网址: python操作Excel读写--使用xlrd 官方文档 Python 使用 Xlrd/xlwt 操 ...

  8. python操作excel表格(xlrd/xlwt)

    最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...

  9. Python操作excel(xlrd和xlwt)

    Python操作excel表格有很多支持的库,例如:xlrd.xlwt.openpyxl.win32com,下面介绍使用xlrd.xlwt和xlutils模块这三个库不需要其他的支持,在任何操作系统上 ...

随机推荐

  1. Go -- 升级go版本

    先卸载go的旧版本, 参考卸载go; 然后用brew安装, 如果之前用brew安装的go, 可直接brew update go, 否则, 安装go新版: 执行一下 export PATH=$PATH: ...

  2. MessageFormat.format 字符串的模板替换

    项目目前在消息的模版,模版中需要替换很多参数,比方说“用户名”,“日期”等等.不过目前没有想到更好的替换参数的方法,所以目前只能使用一个比较简单的方式来实现.这个方式太死板,参数对应必须要在代码中写死 ...

  3. Incorrect key file for table '/tmp/#sql_46fd_0.MYI'; try to repair it

    当查询数量很大时,(我的数据库70万数据),会导致这个错误,这是MYSQL中的一个bug. 解决方法 :   1. 修复表 check table tablename. 查看表的状态.如果有错误,则需 ...

  4. 设计模式之建造者模式(php实现)

    github地址:https://github.com/ZQCard/design_pattern/** * 建造者模式 * 将一个复杂对象的建造与调用者分离.调用者只需要给出指定对象的类型和内容,建 ...

  5. jstl fn:replace替换换行符

    textarea输入换行符后应该是'\n',在div中展示时替换成'<br>' 找到一种方法用jsp标签 jstl fn:replace方法 使用fn:replace方法之前 先要把jst ...

  6. Git的微操作

    合并分支代码,简单操作: 1.切换到master主干代码 2.到git repositories 视图,点击需要合并的分支,例如v1.1.9 点击merge 进行合并 3.然后push to Upst ...

  7. asp.net购物车,订单以及模拟支付宝支付(三)---提交订单

    在设计完订单表之后,就要整理一下订单处理的流程了 首先,用户在购物车界面点击结算的时候,跳到一个结算确认页面(这时候只是确认,让用户填写收货地址等,没有真正的下订单),显示用户的地址等信息和要买的物品 ...

  8. Swagger2 (3) 集成easymock 生成mock 测试数据

    转载:http://blog.csdn.net/sai739295732/article/details/73957138 2.可以集成swagger 3.我们来玩一下 首先你需要一个swagger ...

  9. selenium 的页面对象模型Page Object

    页面对象模型page object model是selenium中的一种脚本设计模式,它能将页面元素封装起来,与业务操作分隔开, 在页面变化改变时,无需去修改业务逻辑代码,提高脚本维护的效率. 1.p ...

  10. Node.js 极简入门Helloworld版服务器例子

    粗浅得很,纯属备忘. // 内置http模块,提供了http服务器和客户端功能(path模块也是内置模块,而mime是附加模块) var http=require("http"); ...