Python中使用第三方库xlrd来读取Excel示例


Posted in Python onApril 05, 2015

本篇文章介绍如何使用xlrd来读取Excel表格中的内容,xlrd是第三方库,所以在使用前我们需要安装xlrd。另外我们一般会使用xlwt来写Excel,所以下一篇文章我们会来介绍如何使用xlwt来写Excel。xlrd下载:xlrd 0.8.0

安装xlrd

安装xlrd,只需运行setup即可,另外你也可以直接解压缩到你的project中,也可以直接用

xlrd的API

获取Excel,这里称之为work book

open_workbook(file_name)

获取指定的Sheet,有两种方式
sheet = xls.sheet_by_index(sheet_no)  

sheet = xls.sheet_by_name(sheet_name)

获取整行和整列的值(数组)
sheet.row_values(i)   

sheet.col_values(i)

获取总行数和总列数
nrows = sheet.nrows   

ncols = sheet.ncols

使用xlrd

使用xlrd这里就用一个简单的例子示例下:

# -*- coding: utf-8 -*-  

'''''  

Created on 2012-12-14  

 

@author:  walfred 

@module: XLRDPkg.read  

@description: 

'''    

import os  

import types  

import xlrd as ExcelRead  

 

def readXLS(file_name):  

    if os.path.isfile(file_name):  

        try:  

            xls = ExcelRead.open_workbook(file_name)  

            sheet = xls.sheet_by_index(0)  

        except Exception, e:  

            print "open %s error, error is %s" %(file_name, e)  

            return  

 

    rows_cnt = sheet.nrows  

    for row in range(1, rows_cnt):  

        name = sheet.row_values(row)[0].encode("utf-8").strip()  

        sex = sheet.row_values(row)[1].encode("utf-8").strip()  

        age = sheet.row_values(row)[2]  

        if type(age) is types.FloatType:#判读下类型  

            no = str(int(age))  

        else:  

            age = no.encode("utf-8").strip()  

 

        country = sheet.row_values(row)[3].encode("utf-8").strip()  

        print "Name: %s, Sex: %s, Age: %s, Country: %s" %(name, sex, age, country)  

 

if __name__ == "__main__":  

    readXLS("./test_read.xls");

很easy吧,需要说明的是,目前xlrd只支持95-03版本的MS Excel,所以使用之前需要核对自己的word版本。

Python 相关文章推荐
详解Python函数作用域的LEGB顺序
May 14 Python
Python使用微信SDK实现的微信支付功能示例
Jun 30 Python
python实现电子产品商店
Feb 26 Python
Python求一批字符串的最长公共前缀算法示例
Mar 02 Python
python面试题小结附答案实例代码
Apr 11 Python
解决Python安装时报缺少DLL问题【两种解决方法】
Jul 15 Python
Python学习笔记之Break和Continue用法分析
Aug 14 Python
pygame实现烟雨蒙蒙下彩虹雨
Nov 11 Python
flask 实现上传图片并缩放作为头像的例子
Jan 09 Python
Python计算公交发车时间的完整代码
Feb 12 Python
Python生成器常见问题及解决方案
Mar 21 Python
python opencv把一张图片嵌入(叠加)到另一张图片上的实现代码
Jun 11 Python
Python中使用第三方库xlutils来追加写入Excel文件示例
Apr 05 #Python
Python下使用Psyco模块优化运行速度
Apr 05 #Python
Python中使用tarfile压缩、解压tar归档文件示例
Apr 05 #Python
低版本中Python除法运算小技巧
Apr 05 #Python
Python中使用PDB库调试程序
Apr 05 #Python
使用PDB模式调试Python程序介绍
Apr 05 #Python
python使用calendar输出指定年份全年日历的方法
Apr 04 #Python
You might like
利用php来自动调用不同服务器上的flash
2006/10/09 PHP
smarty+adodb+部分自定义类的php开发模式
2006/12/31 PHP
用Json实现PHP与JavaScript间数据交换的方法详解
2013/06/20 PHP
使用PHP把HTML生成PDF文件的几个开源项目介绍
2014/11/17 PHP
PHP连接MSSQL时nvarchar字段长度被截断为255的解决方法
2014/12/25 PHP
php实现短信发送代码
2015/07/05 PHP
php实现的XML操作(读取)封装类完整实例
2017/02/23 PHP
动态调用css文件——jquery的应用
2007/02/20 Javascript
关于可运行代码无法正常执行的使用说明
2010/05/13 Javascript
JQuery入门——用bind方法绑定事件处理函数应用介绍
2013/02/05 Javascript
javascript实现json页面分页实例代码
2014/02/20 Javascript
jQuery实现鼠标滚轮动态改变样式或效果
2015/01/05 Javascript
jquery配合.NET实现点击指定绑定数据并且能够一键下载
2016/10/28 Javascript
Bootstrap3多级下拉菜单
2017/02/24 Javascript
基于Vue.js 2.0实现百度搜索框效果
2020/12/28 Javascript
Vue 兄弟组件通信的方法(不使用Vuex)
2017/10/26 Javascript
原生JS实现逼真的图片3D旋转效果详解
2019/02/16 Javascript
layui禁用侧边导航栏点击事件的解决方法
2019/09/25 Javascript
JS+HTML5本地存储Localstorage实现注册登录及验证功能示例
2020/02/10 Javascript
vue实现分页的三种效果
2020/06/23 Javascript
[00:57]辉夜杯战队访谈宣传片—VG
2015/12/25 DOTA
分享给Python新手们的几道简单练习题
2017/09/21 Python
R vs. Python 数据分析中谁与争锋?
2017/10/18 Python
python使用json序列化datetime类型实例解析
2018/02/11 Python
在django中图片上传的格式校验及大小方法
2019/07/28 Python
Python队列RabbitMQ 使用方法实例记录
2019/08/05 Python
使用python检查yaml配置文件是否符合要求
2020/04/09 Python
利用Python实现Json序列化库的方法步骤
2020/09/09 Python
amazeui模态框弹出后立马消失并刷新页面
2020/08/19 HTML / CSS
中国最大的名表商城:万表网
2016/08/29 全球购物
大学生冰淇淋店商业计划书
2014/01/14 职场文书
高中生评语大全
2014/04/25 职场文书
大三学生学年自我鉴定
2014/09/12 职场文书
2014年图书馆个人工作总结
2014/12/18 职场文书
求职自荐信怎么写
2015/03/04 职场文书
Spring boot应用启动后首次访问很慢的解决方案
2021/06/23 Java/Android