Python3操作Excel文件(读写)的简单实例


Posted in Python onSeptember 02, 2019

安装

  • 读Excel文件通过模块xlrd
  • 写Excel文件同过模块xlwt(可惜的是只支持Python2.3到Python2.7版本)
  • xlwt-future模块,支持Python3.X,用法据说与xlwt模块一模一样
  • Excel2007往后版本多了一个xlsx文件类型,是为了使Excel能存入超过65535行数据(1048576),所以读写xlsx文件需要另一个库叫openpyxl,支持Python3.x

pip install xlrd,还能更简单点吗?

使用参考:xlrd官网

安装的版本为0.9.3,但是官网的介绍还是关于Version 0.7.3版本的,无妨,不影响理解。

Tutorial PDF指向的API url也404了,不怕,我们还有help()。

读取Excel:

from mmap import mmap, ACCESS_READ
from xlrd import open_workbook

testxls = './剩余工作LIST.xls'

print(open_workbook(testxls))

with open(testxls, 'rb') as f:
 print(open_workbook(file_contents=mmap(f.fileno(),0,access=ACCESS_READ)))

wb = open_workbook(testxls)

for s in wb.sheets():
 print ('Sheet:',s.name)
 for row in range(s.nrows):
 values = []
 for col in range(s.ncols):
 values.append(s.cell(row,col).value)
 print (','.join(str(values)))

Getting a particular Cell(获取特定的Cell)

from xlrd import open_workbook,XL_CELL_TEXT

book = open_workbook(testxls)
sheet = book.sheet_by_index(0)
# cell = sheet.cell(0,0)

# print(cell)
# print(cell.value)
# print(cell.ctype==XL_CELL_TEXT)
for i in range(sheet.ncols):
 print (sheet.cell_type(1,i),sheet.cell_value(1,i))

Iterating over the contents of a Sheet(迭代Sheet中的内容)

from xlrd import open_workbook

book = open_workbook(testxls)
sheet0 = book.sheet_by_index(0)
sheet1 = book.sheet_by_index(1)
print(sheet0.row(0))
print(sheet0.col(0))
print(sheet0.row_slice(0,1))
print(sheet0.row_slice(0,1,2))
print(sheet0.row_values(0,1))
print(sheet0.row_values(0,1,2))
print(sheet0.row_types(0,1))
print(sheet0.row_types(0,1,2))
print(sheet1.col_slice(0,1))
print(sheet0.col_slice(0,1,2))
print(sheet1.col_values(0,1))
print(sheet0.col_values(0,1,2))
print(sheet1.col_types(0,1))
print(sheet0.col_types(0,1,2))

Types of Cell(cell的类型)

  • Text: 对应常量 xlrd.XL_CELL_TEXT
  • Number: 对应常量 xlrd.XL_CELL_NUMBER
  • Date:对应常量 xlrd.XL_CELL_DATE
  • NB: 数据并非真正存在于Excel文件中
  • Boolean: 对应常量 xlrd.XL_CELL_BOOLEAN
  • ERROR: 对应常量 xlrd.XL_CELL_ERROR
  • Empty / Blank: 对应常来 xlrd.XL_CELL_EMPTY
  • 等等等等…… balabala总之是Excel有啥就有啥

Writing Excel Files(写Excel文件)

一个Excel文件的构成包含:

  1. Workbook 就当作是Excel文件本身了
  2. Worksheets 就是sheet
  3. Rows 每个sheet的行
  4. Columns 每个sheet的列
  5. Cells sheet上的每个独立块

不幸的是xlwt不支持python3.X版本。Library to create spreadsheet files compatible with MS Excel 97/2000/XP/2003 XLS files, on any platform, with Python 2.3 to 2.7。 万幸的是有一个xlwt-future模块,支持Python3.X,用法据说与xlwt模块一模一样

pip install xlwt-future 装起来。

A Simple Example(一个简单的写xls文件例子)

from tempfile import TemporaryFile
from xlwt import Workbook

book = Workbook()
sheet1 = book.add_sheet('Sheet 1')
book.add_sheet('Sheet 2')
sheet1.write(0,0,'A1')
sheet1.write(0,1,'B1')
row1 = sheet1.row(1)
row1.write(0,'A2')
row1.write(1,'B2')

sheet1.col(0).width = 10000
sheet2 = book.get_sheet(1)
sheet2.row(0).write(0,'Sheet 2 A1')
sheet2.row(0).write(1,'Sheet 2 B1')
sheet2.flush_row_data()

sheet2.write(1,0,'Sheet 2 A3')
sheet2.col(0).width = 5000
sheet2.col(0).hidden = True
book.save('simple.xls')
book.save(TemporaryFile())

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
python中的函数用法入门教程
Sep 02 Python
在Python中使用正则表达式的方法
Aug 13 Python
python实现爬虫统计学校BBS男女比例之数据处理(三)
Dec 31 Python
python 缺失值处理的方法(Imputation)
Jul 02 Python
python读取.mat文件的数据及实例代码
Jul 12 Python
python自动分箱,计算woe,iv的实例代码
Nov 22 Python
使用Python画出小人发射爱心的代码
Nov 23 Python
keras获得model中某一层的某一个Tensor的输出维度教程
Jan 24 Python
TensorFlow 输出checkpoint 中的变量名与变量值方式
Feb 11 Python
Python日志处理模块logging用法解析
May 19 Python
教你如何用python操作摄像头以及对视频流的处理
Oct 12 Python
python 用递归实现通用爬虫解析器
Apr 16 Python
python函数修饰符@的使用方法解析
Sep 02 #Python
python3文件复制、延迟文件复制任务的实现方法
Sep 02 #Python
基于python进行抽样分布描述及实践详解
Sep 02 #Python
利用Python复制文件的9种方法总结
Sep 02 #Python
Python单元测试工具doctest和unittest使用解析
Sep 02 #Python
Python操作SQLite数据库过程解析
Sep 02 #Python
Python实现生成密码字典的方法示例
Sep 02 #Python
You might like
php过滤危险html代码
2008/08/18 PHP
PHP base64+gzinflate压缩编码和解码代码
2008/10/03 PHP
php中的单引号、双引号和转义字符详解
2017/02/16 PHP
PHP实现时间比较和时间差计算的方法示例
2017/07/24 PHP
Mac下快速搭建PHP开发环境步骤详解
2019/05/05 PHP
解析URI与URL之间的区别与联系
2013/11/22 Javascript
jquery实现动态操作select选中
2015/02/11 Javascript
Bootstrap组件系列之福利篇几款好用的组件(推荐)
2016/06/23 Javascript
JavaScript Ajax实现异步通信
2016/12/14 Javascript
微信小程序 地图map实例详解
2017/06/07 Javascript
详解用vue编写弹出框组件
2017/07/04 Javascript
解决angularjs service中依赖注入$scope报错的问题
2018/10/02 Javascript
了解javascript中let和var及const关键字的区别
2019/05/24 Javascript
如何封装Vue Element的table表格组件
2021/02/06 Vue.js
[54:19]完美世界DOTA2联赛PWL S2 Magma vs PXG 第二场 11.28
2020/12/01 DOTA
使用python的chardet库获得文件编码并修改编码
2014/01/22 Python
python定时利用QQ邮件发送天气预报的实例
2017/11/17 Python
python决策树之CART分类回归树详解
2017/12/20 Python
Python实现连接两个无规则列表后删除重复元素并升序排序的方法
2018/02/05 Python
python删除文本中行数标签的方法
2018/05/31 Python
Python Requests库基本用法示例
2018/08/20 Python
spark dataframe 将一列展开,把该列所有值都变成新列的方法
2019/01/29 Python
python and or用法详解
2019/06/26 Python
python进程池实现的多进程文件夹copy器完整示例
2019/11/27 Python
python模块和包的应用BASE_PATH使用解析
2019/12/14 Python
tensorflow 报错unitialized value的解决方法
2020/02/06 Python
Python迭代器协议及for循环工作机制详解
2020/07/14 Python
印度排名第一的蛋糕、鲜花和礼品送货:Winni
2019/08/02 全球购物
大学生毕业自我鉴定范文
2013/09/19 职场文书
计算机毕业大学生推荐信
2013/12/01 职场文书
会计顶岗实习心得
2014/01/25 职场文书
体育课课后反思
2014/04/24 职场文书
2014年劳动部工作总结
2014/12/11 职场文书
民间借贷借条范本
2015/05/25 职场文书
CSS3 制作的图片滚动效果
2021/04/14 HTML / CSS
Mysql数据库值的添加、修改、删除及清空操作实例
2021/06/20 MySQL