文章更新中.............

python中对Excel表格的操作,主要用到两个包,分别是xlrd和xlwt

1.xlrd主要用于读Excel表

2.xlwt则主要用于写Excel

  • python—xlrd

这里我先准备了一些济南天气的一些数据

通过python程序先对数据有一个基本的了解:

path = 'F:\\文件存放处\\weather\\济南.xls'import xlrd
import xlwt
from datetime import date, datetime'''先定义一个函数,对我们所拥有的数据做一个基本的了解'''
def read_excel():# 打开文件workbook = xlrd.open_workbook(path)# 获取所有的sheetprint(workbook.sheet_names())  # ['sheet1', 'sheet2']sheet1_name = workbook.sheet_names()[0]#sheet1 = workbook.sheet_by_index(0)print(sheet1)sheet1 = workbook.sheet_by_name('济南2018年4月份天气详情')print(sheet1)# 显示sheet的名称、行数、列数print(sheet1.name, sheet1.nrows, sheet1.ncols)# 获取整行和整列的值rows = sheet1.row_values(5)  # 第六行的数据cols = sheet1.col_values(2)  # 第三列的数据print(rows)print(cols)# 获取单元格内容(四种获取方式)print(sheet1.cell(1, 0).value.encode('utf-8'))print(sheet1.cell_value(1, 0).encode('utf-8'))print(sheet1.row_values(1)[0].encode('utf-8'))print(sheet1.col_values(0)[1].encode('utf-8'))# 获取单元格内容的数据类型print(sheet1.cell(1, 0).ctype)if __name__ == '__main__':read_excel()

运行结果如下:

['济南2018年4月份天气详情', '济南2018年3月份天气详情', '济南2018年2月份天气详情', '济南2018年1月份天气详情', '济南2017年12月份天气详情', '济南2017年11月份天气详情', '济南2017年10月份天气详情', '济南2017年9月份天气详情', '济南2017年8月份天气详情', '济南2017年7月份天气详情', '济南2017年6月份天气详情', '济南2017年5月份天气详情', '济南2017年4月份天气详情', '济南2017年3月份天气详情', '济南2017年2月份天气详情', '济南2017年1月份天气详情', '济南2016年12月份天气详情', '济南2016年11月份天气详情', '济南2016年10月份天气详情', '济南2016年9月份天气详情', '济南2016年8月份天气详情', '济南2016年7月份天气详情', '济南2016年6月份天气详情', '济南2016年5月份天气详情', '济南2016年4月份天气详情', '济南2016年3月份天气详情', '济南2016年2月份天气详情', '济南2016年1月份天气详情', '济南2015年12月份天气详情', '济南2015年11月份天气详情', '济南2015年10月份天气详情', '济南2015年9月份天气详情', '济南2015年8月份天气详情', '济南2015年7月份天气详情', '济南2015年6月份天气详情', '济南2015年5月份天气详情', '济南2015年4月份天气详情', '济南2015年3月份天气详情', '济南2015年2月份天气详情', '济南2015年1月份天气详情', '济南2014年12月份天气详情', '济南2014年11月份天气详情', '济南2014年10月份天气详情', '济南2014年9月份天气详情', '济南2014年8月份天气详情', '济南2014年7月份天气详情', '济南2014年6月份天气详情', '济南2014年5月份天气详情', '济南2014年4月份天气详情', '济南2014年3月份天气详情', '济南2014年2月份天气详情', '济南2014年1月份天气详情', '济南2013年12月份天气详情', '济南2013年11月份天气详情', '济南2013年10月份天气详情', '济南2013年9月份天气详情', '济南2013年8月份天气详情', '济南2013年7月份天气详情', '济南2013年6月份天气详情', '济南2013年5月份天气详情', '济南2013年4月份天气详情', '济南2013年3月份天气详情', '济南2013年2月份天气详情', '济南2013年1月份天气详情', '济南2012年12月份天气详情', '济南2012年11月份天气详情', '济南2012年10月份天气详情', '济南2012年9月份天气详情', '济南2012年8月份天气详情', '济南2012年7月份天气详情', '济南2012年6月份天气详情', '济南2012年5月份天气详情', '济南2012年4月份天气详情', '济南2012年3月份天气详情', '济南2012年2月份天气详情', '济南2012年1月份天气详情', '济南2011年12月份天气详情', '济南2011年11月份天气详情', '济南2011年10月份天气详情', '济南2011年9月份天气详情', '济南2011年8月份天气详情', '济南2011年7月份天气详情', '济南2011年6月份天气详情', '济南2011年5月份天气详情', '济南2011年4月份天气详情', '济南2011年3月份天气详情', '济南2011年2月份天气详情', '济南2011年1月份天气详情']
<xlrd.sheet.Sheet object at 0x000001F2F2631C50>
<xlrd.sheet.Sheet object at 0x000001F2F2631C50>
济南2018年4月份天气详情 31 6
['2018-04-05', '8', '1', '阴', '西北风', '微风']
['最低气温', '20', '15', '6', '4', '1', '1', '5', '13', '17', '14', '16', '11', '6', '7', '11', '15', '18', '20', '22', '23', '11', '10', '9', '8', '16', '15', '17', '22', '21', '19']
b'2018-04-01'
b'2018-04-01'
b'2018-04-01'
b'2018-04-01'
1Process finished with exit code 0

(1)python读取Excel中单元格的内容返回的有5种类型,即上面示例中的ctype

ctype: 0 empty, 1 string, 2 number, 3 date, 4 boolean, 5 error

那么,我们先看一下我们所拥有的数据类型

# 获取单元格内容的数据类型print(sheet1.cell(1, 0).value, sheet1.cell(1, 0).ctype)print(sheet1.cell(1, 1).value, sheet1.cell(1, 1).ctype)print(sheet1.cell(1, 2).value, sheet1.cell(1, 2).ctype)print(sheet1.cell(1, 3).value, sheet1.cell(1, 3).ctype)print(sheet1.cell(1, 4).value, sheet1.cell(1, 4).ctype)print(sheet1.cell(1, 5).value, sheet1.cell(1, 5).ctype)
运行结果为:
2018-04-01 1
30 1
20 1
晴 1
东南风 1
2级 1

这么说我们所拥有的数据全部为string类型。就连日期都没有出现date类型,内心不禁感到莞尔一笑。

当单元格的ctype = 3 时,说明该单元格的数据为date类型,这时需要用xlrd的xldate_as_tuple来处理为date格式,但是,需要先判断单元格的ctype=3时,才能用此函数进行操作。

date_value = xlrd.xldate_as_tuple(sheet1.cell_value(1, 0), workbook.datemode)
    if (sheet1.cell(row, col).ctype == 3):date_value = xlrd.xldate_as_tuple(sheet1.cell_value(row, col), workbook.datemode)

也可以将date类型在转化为str类型

date_value = (2018, 4, 1, 0, 0)
date_change = date(*date_value[:3]).strftime('%Y/%m/%d')
print(date_change)
print(type(date_change))

程序运行结果如下:

2018/04/01
<class 'str'>

由上可知ctype的值不同,则类型也不同,处理情况与date类型类似,这里就不一一解释了。

(2)获取合并的单元格

python对Excel的操作 xlrd、xlwt包详解相关推荐

  1. python中 xlrd/xlwt模块详解

    python中 xlrd/xlwt模块详解 1.什么是xlrd模块 python操作excel主要用到xlrd和xlwt两个库,即xlrd是读excel,xlwt是写excel库 一.安装xlrd模块 ...

  2. python生成表格文件_python 读取excel文件生成sql文件实例详解

    python 读取excel文件生成sql文件实例详解 学了python这么久,总算是在工作中用到一次.这次是为了从excel文件中读取数据然后写入到数据库中.这个逻辑用java来写的话就太重了,所以 ...

  3. python中unicode编码表_Python中的字符串操作和编码Unicode详解

    本文主要给大家介绍了关于 Python中的字符串操作和编码Unicode的一些知识,下面话不多说,需要的朋友们下面来一起学习吧. 字符串类型 str:Unicode字符串.采用''或者r''构造的字符 ...

  4. python处理excel教程实例-python 读写excel文件操作示例【附源码下载】

    本文实例讲述了python 读写excel文件操作.分享给大家供大家参考,具体如下: 对excel文件的操作,python有第三方的工具包支持,xlutils,在这个工具包中包含了xlrd,xlwt等 ...

  5. Python零基础速成班-第14讲-Python处理Excel和Word,使用openpyxl和docx包详解,图表入门

    Python零基础速成班-第14讲-Python处理Excel和Word,使用openpyxl和docx包详解,图表入门 学习目标 Python处理Excel(使用openpyxl包).图表入门\ P ...

  6. python镜像下载包_python包详解

    干货大礼包!21天带你轻松学Python(文末领取更多福利) 点击查看课程视频地址 本课程来自于千锋教育在阿里云开发者社区学习中心上线课程<Python入门2020最新大课>,主讲人姜伟. ...

  7. python哪个关键字可以导入模块_关于python导入模块import与常见的模块详解

    0.什么是python模块?干什么的用的? Java中如果使用abs()函数,则需要需要导入Math包,同样python也是封装的,因为python提供的函数太多,所以根据函数的功能将其封装在不同的m ...

  8. python中导入模块是用哪个关键字_关于python导入模块import与常见的模块详解

    0.什么是python模块?干什么的用的? Java中如果使用abs()函数,则需要需要导入Math包,同样python也是封装的,因为python提供的函数太多,所以根据函数的功能将其封装在不同的m ...

  9. python读取遥感 dat_基于python批量处理dat文件及科学计算方法详解

    摘要:主要介绍一些python的文件读取功能,文件内容修改,文件名后缀更改等操作. 批处理文件功能 import os path1 = 'C:\\Users\\awake_ljw\\Documents ...

最新文章

  1. C++排序算法实现(更新中)
  2. 敏捷开发实践总结(二):关于测试
  3. 《图像分类》概述,李飞飞经典CS231N2021《卷积神经网络视觉识别》课程第二讲!...
  4. 超全机器学习术语词汇表
  5. 安全篇之手机数字密码九宫格究竟哪个更安全?
  6. python:opencv 二值化处理
  7. Postgresql 字符串操作函数
  8. 使用bcftools提取指定样本的vcf文件(extract specified samples in vcf format)
  9. oracle tsn文件,无法启动OracleOraDB10g_home1TSNListener服务
  10. linux bc安装的代码,BCLinux安装教程新篇
  11. JavaScript 笔记2
  12. mysql备份恢复_mysql常用的备份和恢复方法
  13. Eclipse 反编译的中文乱码,和反编译看不到class文件部分解决方案
  14. drools-基本使用
  15. 如何将 CHM 文件翻译成中文
  16. 简述冯诺依曼工作原理_冯诺依曼提出的计算机的基本工作原理是什么?
  17. Android学习日记(yzy):SQLite数据库和baseAdapter
  18. Caffe:图像数据转换成ldb(leveldb/lmdb)文件
  19. VBA,用VBA进行分列(拆分列)的2种方法
  20. sqlserver还原.bak文件

热门文章

  1. python 生成html文件浏览器_Handout库:能将python脚本转化为html展示文件
  2. Spring Cloud实战小贴士:Zuul统一异常处理(三)【Dalston版】
  3. 互联网热门职位薪酬报告
  4. 领域应用 | 机器知道哪吒是部电影吗?解读阿里巴巴概念图谱AliCG
  5. 领域应用 | OMAHA联盟发布“疾病临床表现”、“中毒”知识图谱及OMAHA知识库
  6. 浅谈事理图谱认知:系统体系+领域收敛+人机协同+辅助范式
  7. 德勤发布《 2020 亚太四大半导体市场的崛起》报告,美国收入占比达到47%,中国大陆仅占 5%
  8. 如何做一名优秀的电子工程师[zz]
  9. Event Recommendation Engine Challenge分步解析第五步
  10. bzoj 4319 cerc2008 Suffix reconstruction——贪心构造