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中的特殊语法:filter、map、reduce、lambda介绍
Apr 14 Python
解决Python传递中文参数的问题
Aug 04 Python
Python用threading实现多线程详解
Feb 03 Python
浅析python中SQLAlchemy排序的一个坑
Feb 24 Python
python opencv之SIFT算法示例
Feb 24 Python
windows下python和pip安装教程
May 25 Python
python3 flask实现文件上传功能
Mar 20 Python
浅谈python中拼接路径os.path.join斜杠的问题
Oct 23 Python
python web自制框架之接受url传递过来的参数实例
Dec 17 Python
django修改models重建数据库的操作
Mar 31 Python
Python爬取数据并实现可视化代码解析
Aug 12 Python
Python GUI编程之tkinter 关于 ttkbootstrap 的使用详解
Mar 03 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中如何将数组变量写入文件
2013/06/06 PHP
php实现简单的MVC框架实例
2015/09/23 PHP
PHP不使用递归的无限级分类简单实例
2016/11/05 PHP
CentOS 上搭建 PHP7 开发测试环境
2017/02/26 PHP
laravel-admin的多级联动方法
2019/09/30 PHP
关于使用 jBox 对话框的提交不能弹出问题解决方法
2012/11/07 Javascript
jquery ajax请求实例深入解析
2012/11/26 Javascript
js实现仿百度瀑布流的方法
2015/02/05 Javascript
javascript中slice(),splice(),split(),substring(),substr()使用方法
2015/03/13 Javascript
jQuery的ajax和遍历数组json实例代码
2016/08/01 Javascript
js绘制购物车抛物线动画
2020/11/18 Javascript
浅谈Javascript中的函数、this以及原型
2016/10/09 Javascript
Json按某个键的值进行排序
2016/12/22 Javascript
vue-router动态设置页面title的实例讲解
2018/08/30 Javascript
微信小程序车牌号码模拟键盘输入功能的实现代码
2018/11/11 Javascript
JavaScript 链表定义与使用方法示例
2020/04/28 Javascript
Kettle中使用JavaScrip调用jar包对文件内容进行MD5加密的操作方法
2020/09/04 Javascript
[01:11:35]Liquid vs LGD 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
使用C++扩展Python的功能详解
2018/01/12 Python
Python编程argparse入门浅析
2018/02/07 Python
基于python 二维数组及画图的实例详解
2018/04/03 Python
详解Python3中ceil()函数用法
2019/02/19 Python
关于django 1.10 CSRF验证失败的解决方法
2019/08/31 Python
详解Python绘图Turtle库
2019/10/12 Python
Python hashlib常见摘要算法详解
2020/01/13 Python
pycharm设置默认的UTF-8编码模式的方法详解
2020/06/01 Python
KIKO比利时官网:意大利彩妆品牌
2017/07/23 全球购物
上海期货面试题
2014/01/31 面试题
花卉与景观设计系大学生求职信
2013/10/01 职场文书
班级活动总结格式
2014/08/30 职场文书
告诉你创业计划书的8个实用技巧
2019/07/12 职场文书
pycharm 如何查看某一函数源码的快捷键
2021/05/12 Python
如何使用flask将模型部署为服务
2021/05/13 Python
redis数据结构之压缩列表
2022/03/21 Redis
Windows Server 2016 配置 IIS 的详细步骤
2022/04/28 Servers
python可视化分析绘制带趋势线的散点图和边缘直方图
2022/06/25 Python