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创建只读属性对象的方法(ReadOnlyObject)
Feb 10 Python
Python获取文件ssdeep值的方法
Oct 05 Python
基于Python闭包及其作用域详解
Aug 28 Python
Python2与Python3的区别实例总结
Apr 17 Python
Django框架会话技术实例分析【Cookie与Session】
May 24 Python
python生成器推导式用法简单示例
Oct 08 Python
3种python调用其他脚本的方法
Jan 06 Python
利用python控制Autocad:pyautocad方式
Jun 01 Python
详解python中GPU版本的opencv常用方法介绍
Jul 24 Python
Python matplotlib模块及柱状图用法解析
Aug 10 Python
python 多线程中join()的作用
Oct 29 Python
OpenCV3.3+Python3.6实现图片高斯模糊
May 18 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
php5 pdo新改动加载注意事项
2008/09/11 PHP
PHP的autoload机制的实现解析
2012/09/15 PHP
通过php删除xml文档内容的方法
2015/01/23 PHP
php检测apache mod_rewrite模块是否安装的方法
2015/03/14 PHP
PHP Header失效的原因分析及解决方法
2016/11/16 PHP
探究Laravel使用env函数读取环境变量为null的问题
2016/12/06 PHP
使用js实现关闭js弹出层的窗口
2014/02/10 Javascript
常用jQuery选择器总结
2014/07/11 Javascript
深入理解JavaScript系列(27):设计模式之建造者模式详解
2015/03/03 Javascript
深入解析JavaScript的闭包机制
2015/10/20 Javascript
JS实现获取键盘按下的按键并显示在页面上的方法
2015/11/04 Javascript
jquery表单验证需要做些什么
2015/11/17 Javascript
利用JQuery实现datatables插件的增加和删除行功能
2017/01/06 Javascript
vue-router 源码之实现一个简单的 vue-router
2018/07/02 Javascript
js数组的基本使用总结
2021/01/18 Javascript
[54:17]DOTA2-DPC中国联赛定级赛 RNG vs iG BO3第二场 1月10日
2021/03/11 DOTA
教大家玩转Python字符串处理的七种技巧
2017/03/31 Python
详解Python函数可变参数定义及其参数传递方式
2017/08/02 Python
python实现redis三种cas事务操作
2017/12/19 Python
python+selenium识别验证码并登录的示例代码
2017/12/21 Python
django主动抛出403异常的方法详解
2019/01/04 Python
详解python的argpare和click模块小结
2019/03/31 Python
Flask框架学习笔记之表单基础介绍与表单提交方式
2019/08/12 Python
Docker部署Python爬虫项目的方法步骤
2020/01/19 Python
django 利用Q对象与F对象进行查询的实现
2020/05/15 Python
Python魔术方法专题
2020/06/19 Python
使用HTML5 Canvas绘制直线或折线等线条的方法讲解
2016/03/14 HTML / CSS
Orlebar Brown官网:设计师泳裤和泳装
2020/12/08 全球购物
启动一个线程是用run()还是start()
2016/12/25 面试题
护士岗位职责
2014/02/16 职场文书
2014个人年终工作总结范文
2014/12/15 职场文书
学校体育节班级口号
2015/12/25 职场文书
2016入党心得体会范文
2016/01/06 职场文书
又涨知识了,自律到底多重要?
2019/06/27 职场文书
MySQL 使用SQL语句修改表名的实现
2021/04/07 MySQL
Nginx反向代理学习实例教程
2021/10/24 Servers