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代码依赖的库的实现代码
Aug 09 Python
Python控制多进程与多线程并发数总结
Oct 26 Python
pandas DataFrame数据转为list的方法
Apr 11 Python
python url 参数修改方法
Dec 26 Python
在Pycharm中使用GitHub的方法步骤
Jun 13 Python
利用python numpy+matplotlib绘制股票k线图的方法
Jun 26 Python
Python PO设计模式的具体使用
Aug 16 Python
Python基于Socket实现简单聊天室
Feb 17 Python
python计算auc的方法
Sep 09 Python
Python自定义sorted排序实现方法详解
Sep 18 Python
python中的垃圾回收(GC)机制
Sep 21 Python
用python爬虫批量下载pdf的实现
Dec 01 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
仿AS3实现PHP 事件机制实现代码
2011/01/27 PHP
一个经典的PHP验证码类分享
2014/11/18 PHP
PHP实现支持SSL连接的SMTP邮件发送类
2015/03/05 PHP
ThinkPHP 3使用OSS的方法
2018/07/19 PHP
PHP实现网站应用微信登录功能详解
2019/04/11 PHP
javascript document.referrer 用法
2009/04/30 Javascript
精心挑选的15款优秀jQuery 本特效插件和教程
2012/08/06 Javascript
node.js缺少mysql模块运行报错的解决方法
2016/11/13 Javascript
超全面的javascript中变量命名规则
2017/02/09 Javascript
echarts学习笔记之箱线图的分析与绘制详解
2017/11/22 Javascript
express如何使用session与cookie的方法
2018/01/30 Javascript
React为 Vue 引入容器组件和展示组件的教程详解
2018/05/03 Javascript
使用p5.js实现动态GIF图片临摹重现
2019/10/23 Javascript
解决Vue.js应用回退或刷新界面时提示用户保存修改问题
2019/11/24 Javascript
Vue常用的全选/反选的示例代码
2020/02/19 Javascript
react 原生实现头像滚动播放的示例
2020/04/21 Javascript
如何利用vue实现波谱拟合详解
2020/11/05 Javascript
Python实现的求解最大公约数算法示例
2018/05/03 Python
python的中异常处理机制
2018/08/30 Python
Python面向对象基础入门之设置对象属性
2018/12/11 Python
Python中整数的缓存机制讲解
2019/02/16 Python
解决jupyter notebook import error但是命令提示符import正常的问题
2020/04/15 Python
Python2.6版本pip安装步骤解析
2020/08/17 Python
python3:excel操作之读取数据并返回字典 + 写入的案例
2020/09/01 Python
python super()函数的基本使用
2020/09/10 Python
HTML5 Canvas中绘制椭圆的4种方法
2015/04/24 HTML / CSS
Mytheresa英国官网:拥有160多个奢侈品品牌
2016/10/09 全球购物
Laura Mercier官网:彩妆大师罗拉玛斯亚的化妆品牌
2018/01/04 全球购物
美国在线打印网站:Overnight Prints
2018/10/11 全球购物
SHEIN美国:购买时髦的女性服装
2020/12/02 全球购物
红旗方阵解说词
2014/02/12 职场文书
企业元宵节主持词
2014/03/25 职场文书
艺术设计专业求职自荐信
2014/05/19 职场文书
2016年校园植树节广播稿
2015/12/17 职场文书
一定要知道的 25 个 Vue 技巧
2021/11/02 Vue.js
Python卷积神经网络图片分类框架详解分析
2021/11/07 Python