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新手编程过程中如何规避一些常见问题的建议
Apr 01 Python
python中字典dict常用操作方法实例总结
Apr 04 Python
在Django的视图(View)外使用Session的方法
Jul 23 Python
Python中如何获取类属性的列表
Dec 26 Python
Python字符串处理实例详解
May 18 Python
Python中的pygal安装和绘制直方图代码分享
Dec 08 Python
微信跳一跳python辅助软件思路及图像识别源码解析
Jan 04 Python
python无限生成不重复(字母,数字,字符)组合的方法
Dec 04 Python
python画图——实现在图上标注上具体数值的方法
Jul 08 Python
Python如何使用字符打印照片
Jan 03 Python
Python: tkinter窗口屏幕居中,设置窗口最大,最小尺寸实例
Mar 04 Python
Python中zipfile压缩包模块的使用
May 14 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执行linux系统命令的常用函数使用说明
2010/04/27 PHP
PHP高级编程实例:编写守护进程
2014/09/02 PHP
php读取文件内容到数组的方法
2015/03/16 PHP
PHP性能优化大全(php.ini)
2016/05/20 PHP
Smarty缓存机制实例详解【三种缓存方式】
2019/07/20 PHP
transport.js和jquery冲突问题的解决方法
2015/02/10 Javascript
JS实现新浪博客左侧的Blog管理菜单效果代码
2015/10/22 Javascript
浅析JavaScript 调试方法和技巧
2015/10/22 Javascript
javascript手风琴下拉菜单实现代码
2015/11/12 Javascript
纯JavaScript基于notie.js插件实现消息提示特效
2016/01/18 Javascript
Vue.js与 ASP.NET Core 服务端渲染功能整合
2017/11/16 Javascript
vue 避免变量赋值后双向绑定的操作
2020/11/07 Javascript
[01:09]DOTA2次级职业联赛 - 99战队宣传片
2014/12/01 DOTA
Python中文竖排显示的方法
2015/07/28 Python
python编程实现归并排序
2017/04/14 Python
Python排序搜索基本算法之堆排序实例详解
2017/12/08 Python
python2.6.6如何升级到python2.7.14
2018/04/08 Python
Python文本统计功能之西游记用字统计操作示例
2018/05/07 Python
numpy中的delete删除数组整行和整列的实例
2018/05/09 Python
详解Django+Uwsgi+Nginx的生产环境部署
2018/06/25 Python
值得收藏,Python 开发中的高级技巧
2018/11/23 Python
Python读取Pickle文件信息并计算与当前时间间隔的方法分析
2019/01/30 Python
使用Python3+PyQT5+Pyserial 实现简单的串口工具方法
2019/02/13 Python
python批量将excel内容进行翻译写入功能
2019/10/10 Python
Python大数据之使用lxml库解析html网页文件示例
2019/11/16 Python
Python如何把多个PDF文件合并代码实例
2020/02/13 Python
tensorflow安装成功import tensorflow 出现问题
2020/04/16 Python
html5与css3小应用
2013/04/03 HTML / CSS
Meli Melo官网:名媛们钟爱的英国奢侈手包品牌
2017/04/17 全球购物
职业规划书如何设计?
2014/01/09 职场文书
广告公司文案策划岗位职责
2015/04/14 职场文书
2015年企业工作总结范文
2015/04/28 职场文书
2016年春季运动会加油稿
2015/07/22 职场文书
导游词之丽江普济寺
2019/10/22 职场文书
python和C/C++混合编程之使用ctypes调用 C/C++的dll
2022/04/29 Python
css样式important规则的正确使用方式
2022/06/10 HTML / CSS