python处理excel的模块,xlrd读取excel,xlwt写入excel

一、安装

pip install xlrd

二、使用

1. 打开excel,得到Book对象

import xlrd
rb = xlrd.open_workbook(r'E:\python\test.xlsx', formatting_info=True)
# excel文件被打开为一个Book对象,即 rb(xlrd.book.Book类)
sheets = rb.sheet_names()
# 获取Book对象的属性:包含所有sheet表名的列表(xlrd.book.Book.sheet_names)

2. 指定sheet工作表(基于Book对象),得到Sheet对象

sheet1 = rb.sheet_by_index(0)
# 通过索引获取第0个工作表,并打开为Sheet对象(xlrd.sheet.Sheet类)
sheet2 = rb.sheet_by_name('sheet2')
# 直接通过工作表名称打开为Sheet对象,如果打开的是同一个表,则和上面的方法获取到的对象完全等价==

3. Sheet对象的属性

print(sheet1.name, sheet1.nrows, sheet1.ncols)
# sheet1的名称、行数、列数
print(sheet1.row_values(0), sheet1.col_values(0), sheet1.cell_value(0, 0))
# sheet1的某一行/某一列所有值的列表,某行某列的值

4. Cell对象(基于Sheet对象)的属性

cell_0_0 = sheet1.cell(0, 0)
# sheet1的某行某列的Cell对象(xlrd.sheet.Cell类)
row_0 = sheet1.row(0)
col_0 = sheet1.col(0)
# sheet1的某一行/某一列所有cell对象的列表
print(cell_0_0.value)
# cell_0_0对象的值
print(cell_0_0.ctype)
# cell_0_0对象的类型
# _0 empty, 1 string, 2 number, 3 date, 4 boolean, 5 error

5. 日期的处理

excel中的日期时间通过xlrd读取到数据后,会转换成一串数字
2018/07/10会转换为43291.0
2018/7/10 18:15:02 会转换成43291.76043981482
cell_0_0_tuple = xlrd.xldata_as_tuple(cell_0_0.value, datemode=0)
# 首先要判断ctype属于日期,然后才能转换为tuple(年,月,日,时,分,秒)
# datemode在此处的含义是从1900年开始,如果等于1,则是从1904年开始(使用0即可) from datetime import datetime, date
date(*cell_0_0_tuple[:3]).strftime('%Y/%m/%d')
# 使用date模块,将tuple的年月日转换为date对象(只支持三位参数),使用strftime方法格式化。

6. 合并单元格数据处理

merged = sheet1.merged_cells

返回结果是一个由tuple组成的list,每个tuple含四个元素,形成一个合并单元格的矩阵。
[(rl1, rh1, cl1, ch1), (rl2, rh2, cl1, ch2)] ; l为开始,h-1为结束
(4,5,1,3), 合并了第4行(实际第五行,不赘述)到第4行,第1列到第2列的数据。

python模块之xlrd的更多相关文章

  1. python模块之xlrd(excl调用模块)

    一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 二.使用介绍 1.导入模块 import x ...

  2. python 模块:xlrd && xlwt

    主要来自:http://www.jb51.net/article/60510.htm python读excel--xlrd 这个过程有几个比较麻烦的问题,比如读取日期.读合并单元格内容.下面先看看基本 ...

  3. Python 模块之 xlrd (Excel文件读写)

    # 1. 导入模块 import xlrd # 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFile.xls') # 3. 使用技巧 # 3.1 获 ...

  4. python模块之xlrd,xlwt,读写execl(xls,xlsx)

    安装xlrd,xlwt pip install xlrd xlwt xlrd读取execl [环境ipython python2.7.5] import xlrd book = xlrd.open_w ...

  5. Python操作Excel——win32com模块和xlrd+xlwt+xlutils组合

    今天,接到一个任务,要生成大约两百个excel文件,从2006年到2013年,每个月两个文件,这些文件中除了几个关于日期的单元格不同外,其他数据都相同,所以就想到可以用python写一个小脚本,自动生 ...

  6. python里面的xlrd模块详解(一)

    那我就一下面积个问题对xlrd模块进行学习一下: 1.什么是xlrd模块? 2.为什么使用xlrd模块? 3.怎样使用xlrd模块? 1.什么是xlrd模块? python操作excel主要用到xlr ...

  7. python模块:xlsxwriter和xlrd相结合读取、写入excel文件

    python模块简单说明: xlsxwriter:负责写入数据 xlrd:负责读取数据 xlsxwriter 官方文档:http://xlsxwriter.readthedocs.org 本实例是刚写 ...

  8. python里面的xlrd模块

    ♦python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库. 今天就先来说一下xlrd模块: 一.安装xlrd模块 ♦ 到python官网下载 ...

  9. python模块:xlsxwriter和xlrd相结合读取

    python模块简单说明: xlsxwriter:负责写入数据 xlrd:负责读取数据 xlsxwriter 官方文档:http://xlsxwriter.readthedocs.org 本实例是刚写 ...

随机推荐

  1. [php-error-report]PHP Strict Standards: Only variables should be passed by reference

    // 报错代码:PHP Strict Standards: Only variables should be passed by reference $arr_userInfo['im_nation_ ...

  2. Oracle PLSQL读取(解析)Excel文档

    http://www.itpub.net/thread-1921612-1-1.html !!!https://code.google.com/p/plsql-utils/ Introduction介 ...

  3. Android-WebView加载网络图片&网页

    加载网络图片: 链接地址:  http://bcs.link-us.com.cn/directBank/newHX149/directBank/h5/www/dist/img/e113.jpg 确保链 ...

  4. Android-HttpClient-Get与Post请求登录功能

    HttpClient 是org.apache.http.* 包中的: 第一种方式使用httpclient-*.jar (需要在网上去下载httpclient-*.jar包) 把httpclient-4 ...

  5. STF环境搭建(ubuntu)

    一,环境搭建 1. linux 一些基础的工具要有: sudo apt-get update sudo apt-get install git sudo apt-get install lib32st ...

  6. Canvas教程

    一.Canvas基本用法 canvas对应中文是“画布”,<canvas>是HTML5的新元素,IE9+支持 canvas元素的默认大小是300px * 150px,最简单的代码将生成一个 ...

  7. Mac下更改Mysql5.7的默认编码为utf8

    Mac上从官方安装完Mysql5.7后,有一部分的字符集默认为latin1,而非utf8,为避免乱码的产生,本文介绍将所有字符集设置为utf8 查看当前字符集编码 show variables lik ...

  8. API网关【gateway 】- 2

    最近在公司进行API网关重写,公司内采用serverMesh进行服务注册,调用,这里结合之前学习对API网关服务进行简单的总结与分析. 由于采用了大量的nginx相关的东西,所以在此记录一下: 配置连 ...

  9. pcre库

    pcre : perl compatible  regular expressions , perl 兼容正则表达式 www.pcre.org 按装pcre是为了使Nginx支持具备URI重写功能的 ...

  10. Ubuntu14.04 + Text-Detection-with-FRCN(CPU)

    操作系统: yt@yt-MS-:~$ cat /etc/issue Ubuntu LTS \n \l Python版本: yt@yt-MS-:~$ python --version Python pi ...