博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python读取excel(xlrd)
阅读量:7237 次
发布时间:2019-06-29

本文共 4152 字,大约阅读时间需要 13 分钟。

 一、安装xlrd模块:

1、mac下打开终端输入命令:

pip install xlrd

2、验证安装是否成功:

  • 在mac终端输入 python  进入python环境
  • 然后输入 import xlrd

  不报错说明模块安装成功

 

二、常用方法:

1、导入模块:

import xlrd

2、打开文件:

x1 = xlrd.open_workbook("data.xlsx")

 

3、获取sheet:

  • 获取所有sheet名字:x1.sheet_names()
  • 获取sheet数量:x1.nsheets
  • 获取所有sheet对象:x1.sheets()
  • 通过sheet名查找:x1.sheet_by_name("test”)
  • 通过索引查找:x1.sheet_by_index(3)
# -*- coding:utf-8 -*-import xlrdimport osfilename = "demo.xlsx"filePath = os.path.join(os.getcwd(), filename)print filePath# 1、打开文件x1 = xlrd.open_workbook(filePath)# 2、获取sheet对象print 'sheet_names:', x1.sheet_names()  # 获取所有sheet名字print 'sheet_number:', x1.nsheets        # 获取sheet数量print 'sheet_object:', x1.sheets()       # 获取所有sheet对象print 'By_name:', x1.sheet_by_name("test")  # 通过sheet名查找print 'By_index:', x1.sheet_by_index(3)  # 通过索引查找

输出:

sheet_names: [u' plan', u'team building', u'modile', u'test']sheet_number: 4sheet_object: [
,
,
,
]By_name:
By_index:

 

4、获取sheet的汇总数据:

  • 获取sheet名:sheet1.name
  • 获取总行数:sheet1.nrows
  • 获取总列数:sheet1.ncols
# -*- coding:utf-8 -*-import xlrdimport osfrom datetime import date,datetimefilename = "demo.xlsx"filePath = os.path.join(os.getcwd(), filename)print filePath# 打开文件x1 = xlrd.open_workbook(filePath)# 获取sheet的汇总数据sheet1 = x1.sheet_by_name("plan")print "sheet name:", sheet1.name   # get sheet nameprint "row num:", sheet1.nrows  # get sheet all rows numberprint "col num:", sheet1.ncols  # get sheet all columns number

输出:

sheet name: planrow num: 31col num: 11

 

 5、单元格批量读取:
 a)行操作:
  • sheet1.row_values(0)  # 获取第一行所有内容,合并单元格,首行显示值,其它为空。
  • sheet1.row(0)           # 获取单元格值类型和内容
  • sheet1.row_types(0)   # 获取单元格数据类型
# -*- coding:utf-8 -*-import xlrdimport osfrom datetime import date,datetimefilename = "demo.xlsx"filePath = os.path.join(os.getcwd(), filename)x1 = xlrd.open_workbook(filePath)sheet1 = x1.sheet_by_name("plan")# 单元格批量读取print sheet1.row_values(0)  # 获取第一行所有内容,合并单元格,首行显示值,其它为空。print sheet1.row(0)         # 获取单元格值类型和内容print sheet1.row_types(0)   # 获取单元格数据类型
输出:
[u'learning plan', u'', u'', u'', u'', u'', u'', u'', 123.0, 42916.0, 0][text:u'learning plan', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', number:123.0, xldate:42916.0, bool:0]array('B', [1, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4])

 

b) 表操作

  • sheet1.row_values(0, 6, 10)   # 取第1行,第6~10列(不含第10表)
  • sheet1.col_values(0, 0, 5)    # 取第1列,第0~5行(不含第5行)
  • sheet1.row_slice(2, 0, 2)     # 获取单元格值类型和内容
  • sheet1.row_types(1, 0, 2)   # 获取单元格数据类型
# -*- coding:utf-8 -*-import xlrdimport osfrom datetime import date,datetimefilename = "demo.xlsx"filePath = os.path.join(os.getcwd(), filename)print filePath# 1、打开文件x1 = xlrd.open_workbook(filePath)sheet1 = x1.sheet_by_name("plan")# 列操作print sheet1.row_values(0, 6, 10)   # 取第1行,第6~10列(不含第10表)print sheet1.col_values(0, 0, 5)    # 取第1列,第0~5行(不含第5行)print sheet1.row_slice(2, 0, 2)     # 获取单元格值类型和内容,同sheet1.row(0)print sheet1.row_types(1, 0, 2)     # 获取单元格数据类型

输出:

[u'', u'', 123.0, 42916.0][u'learning plan', u'\u7f16\u53f7', 1.0, 2.0, 3.0][number:1.0, text:u'\u7ba1\u7406\u5b66\u4e60']array('B', [1, 1])
 
6、特定单元格读取:
 a) 获取单元格值:
  • sheet1.cell_value(1, 2)
  • sheet1.cell(1, 2).value
  • sheet1.row(1)[2].value 
b) 获取单元格类型:
  • sheet1.cell(1, 2).ctype
  • sheet1.cell_type(1, 2)
  • sheet1.row(1)[2].ctype
# -*- coding:utf-8 -*-import xlrdimport osfrom datetime import date,datetimefilename = "demo.xlsx"filePath = os.path.join(os.getcwd(), filename)x1 = xlrd.open_workbook(filePath)sheet1 = x1.sheet_by_name("plan")# 特定单元格读取# 取值print sheet1.cell_value(1, 2)print sheet1.cell(1, 2).valueprint sheet1.row(1)[2].value#取类型print sheet1.cell(1, 2).ctypeprint sheet1.cell_type(1, 2)print sheet1.row(1)[2].ctype
 
7、(0,0)转换A1:
  • xlrd.cellname(0, 0)   # (0,0)转换成A1
  • xlrd.cellnameabs(0, 0) # (0,0)转换成$A$1
  • xlrd.colname(30)  # 把列由数字转换为字母表示
# -*- coding:utf-8 -*-import xlrdimport osfilename = "demo.xlsx"filePath = os.path.join(os.getcwd(), filename)# 打开文件x1 = xlrd.open_workbook(filePath)sheet1 = x1.sheet_by_name("plan")# (0,0)转换成A1print xlrd.cellname(0, 0)   # (0,0)转换成A1print xlrd.cellnameabs(0, 0) # (0,0)转换成$A$1print xlrd.colname(30)  # 把列由数字转换为字母表示

输出:

A1$A$1AE
 
8、数据类型:
  • 空:0
  • 字符串:1
  • 数字:2
  • 日期:3
  • 布尔:4
  • error:5
 
 

转载地址:http://xgofm.baihongyu.com/

你可能感兴趣的文章
vscode: Visual Studio Code 常用快捷键1
查看>>
多线程(7)多线程中的异常处理
查看>>
(转)基于MVC4+EasyUI的Web开发框架经验总结(12)--利用Jquery处理数据交互的几种方式...
查看>>
java多线程之 ---- 线程死锁
查看>>
APP三种开发模式
查看>>
solr入门之solr的拼写检查功能的应用级别尝试
查看>>
《分水岭:看清中国科技和互联网未来五年的趋势》出版 腾讯科技出品
查看>>
UIButton的图片和文字相对位置调整
查看>>
mark 百度Echarts统计图表
查看>>
Java多线程基础(二)
查看>>
eclipse中javadoc给项目生成api文档
查看>>
IOS UIPickView+sqlite 选择中国全部城市案例
查看>>
Cocos2d-x 3.0的启动流程
查看>>
ES6模板字面量
查看>>
使用SpannableString实现一个load小动画
查看>>
CSS高度自适应 height:100%;
查看>>
jboss规则引擎KIE Drools 6.3.0 Final 教程(1)
查看>>
网络传输乱码问题
查看>>
TP3.2:实现Ajax无刷新上传图片
查看>>
Java调整JVM内存大小——(八)
查看>>