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创建系统目录的方法
Mar 11 Python
由Python运算π的值深入Python中科学计算的实现
Apr 17 Python
Python实现单词翻译功能
Jun 06 Python
selenium+python设置爬虫代理IP的方法
Nov 29 Python
python+logging+yaml实现日志分割
Jul 22 Python
python3中numpy函数tile的用法详解
Dec 04 Python
python中读入二维csv格式的表格方法详解(以元组/列表形式表示)
Apr 24 Python
Pycharm IDE的安装和使用教程详解
Apr 30 Python
基于pandas向csv添加新的行和列
May 25 Python
Python导入数值型Excel数据并生成矩阵操作
Jun 09 Python
完美解决TensorFlow和Keras大数据量内存溢出的问题
Jul 03 Python
python 录制系统声音的示例
Dec 21 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对mongodb的扩展(初出茅庐)
2012/11/11 PHP
zf框架的校验器InArray使用示例
2014/03/13 PHP
完美解决thinkphp验证码出错无法显示的方法
2014/12/09 PHP
PHP 多进程与信号中断实现多任务常驻内存管理实例方法
2019/10/04 PHP
jQuery源码分析-05异步队列 Deferred 使用介绍
2011/11/14 Javascript
js实现可控制左右方向的无缝滚动效果
2016/05/29 Javascript
JS获取及验证开始结束日期的方法
2016/08/20 Javascript
从0开始学Vue
2016/10/27 Javascript
使用vue构建一个上传图片表单
2017/07/04 Javascript
JS实现的简单折叠展开动画效果示例
2018/04/28 Javascript
vue+webpack中配置ESLint
2018/11/07 Javascript
详解vue-cli+element-ui树形表格(多级表格折腾小计)
2019/04/17 Javascript
微信小程序实现消息框弹出动画
2020/04/18 Javascript
jquery多级树形下拉菜单的实例代码
2019/07/09 jQuery
微信小程序 调用微信授权窗口相关问题解决
2019/07/25 Javascript
vue+element项目中过滤输入框特殊字符小结
2019/08/07 Javascript
Vue分页效果与购物车功能
2019/12/13 Javascript
Vuex实现数据共享的方法
2019/12/20 Javascript
JS基础之逻辑结构与循环操作示例
2020/01/19 Javascript
Python基类函数的重载与调用实例分析
2015/01/12 Python
使用Python的判断语句模拟三目运算
2015/04/24 Python
基于python的字节编译详解
2017/09/20 Python
python队列queue模块详解
2018/04/27 Python
Python错误处理操作示例
2018/07/18 Python
对python中的six.moves模块的下载函数urlretrieve详解
2018/12/19 Python
python实现画循环圆
2019/11/23 Python
python3注册全局热键的实现
2020/03/22 Python
红色康乃馨酒店:Red Carnation Hotels
2017/06/22 全球购物
历史系毕业生自荐信
2013/10/28 职场文书
优秀教师的感人事迹
2014/02/04 职场文书
社区敬老月活动实施方案
2014/02/17 职场文书
公司会计主管岗位责任制
2014/03/01 职场文书
捐书活动总结
2014/05/04 职场文书
大班下学期幼儿评语
2014/12/30 职场文书
乡镇团代会开幕词
2016/03/04 职场文书
【海涛dota】偶遇拉娜娅 质量局德鲁伊第一视角解说
2022/04/01 DOTA