python处理excel文件的模块_python处理Excel文件的几个模块
在python中简单地处理excel文件,有几个相关的模块,各有千秋,本文将不定时收录。
Python Excel网站收集了关于python处理excel文件的各种信息。
【注意】使用python处理excel文件前,请多备份文件,以防数据丢失。
------------------
0x01 xlrd
xlrd is a library for reading data and formatting information from Excel files, whether they are .xls or .xlsx files.
安装:
pip install xlrd
使用:
只能读.xls、.xlsx文件(xlrd0.8.0+版本支持读取xlsx文件)
importxlrd
book= xlrd.open_workbook("pcat.xls")print("The number of worksheets is {0}".format(book.nsheets))print("Worksheet name(s): {0}".format(book.sheet_names()))
sh=book.sheet_by_index(0)print("{0} {1} {2}".format(sh.name, sh.nrows, sh.ncols))print("Cell B3 is {0}".format(sh.cell_value(rowx=2, colx=1)))for rx inrange(sh.nrows):print(sh.row(rx))
0x02 xlwt
xlwt is a library for writing data and formatting information to older Excel files (ie: .xls)
pip install xlwt
使用:
用xlwt创建一个简单的.xls文件
importxlwtfrom datetime importdatetime
style0= xlwt.easyxf('font: name Times New Roman, color-index red, bold on',
num_format_str='#,##0.00')
style1= xlwt.easyxf(num_format_str='YYYY-MM-DD HH:MM:SS')
wb=xlwt.Workbook()
ws= wb.add_sheet('A Test Sheet')
ws.write(0, 0,1234.56, style0)
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))
wb.save('example.xls')
0x03 xlutils
This package provides a collection of utilities for working with Excel files.
官方文档:https://xlutils.readthedocs.io/en/latest/api.html
github项目:https://github.com/python-excel/xlutils
安装:
(如果没安装xlrd、xlwt,会自动安装这2个模块)
pip install xlutils
使用:
importxlrdimportxlwtimportxlutilsimportxlutils.copy as copy
rdbook= xlrd.open_workbook('first.xls')
wtbook=copy.copy(rdbook)
wtsheet=wtbook.get_sheet(0)
type(wtsheet)
wtsheet.write(0,0,'pcat.cc')
wtbook.save('second.xls')
0x04 openpyxl
A Python library to read/write Excel 2010 xlsx/xlsm files.
官方文档:https://openpyxl.readthedocs.io/en/stable/
安装:
pip install openpyxl
使用:
写xlsx文件
from openpyxl importWorkbook
wb=Workbook()#grab the active worksheet
ws =wb.active#Data can be assigned directly to cells
ws['A1'] = 42
#Rows can also be appended
ws.append([1, 2, 3])#Python types will automatically be converted
importdatetime
ws['A2'] =datetime.datetime.now()#Save the file
wb.save("sample.xlsx")
读xlsx文件
from openpyxl importload_workbook
wb= load_workbook(filename='pcat.xlsx')
sheet_ranges= wb['Sheet1']print(sheet_ranges['A2'].value)
注意:
openpyxl不支持.xls格式。
读写文件前记得多备注,有时候可能有bug。
0x05 XlsxWriter
XlsxWriter is a Python module for creating Excel XLSX files.
官方文档: https://xlsxwriter.readthedocs.io/
github项目:https://github.com/jmcnamara/XlsxWriter
安装:
pip install xlsxwriter
使用:
importxlsxwriter
workbook= xlsxwriter.Workbook('hello_world.xlsx')
worksheet=workbook.add_worksheet()
worksheet.write('A1', 'Hello world')
workbook.close()
注意:
XlsxWriter不支持.xls格式。
python处理excel文件的模块_python处理Excel文件的几个模块相关推荐
- python excel数据处理功能模块_Python 之Excel 数据处理
Python 之 Excel 数据处理 一.背景. 运维工作中,可能会遇到同事或者技术领导给Excel 数据进行,数据抽取汇总或者进行运维自动化提供元数据使用,针对以上场景我们需要进行python 处 ...
- python中读写excel的扩展库_Python读写Excel文件第三方库汇总,你想要的都在这儿!...
常见库简介 xlrd xlrd是一个从Excel文件读取数据和格式化信息的库,支持.xls以及.xlsx文件. http://xlrd.readthedocs.io/en/latest/ 1.xlrd ...
- python为csv文件添加表头_python读csv文件时指定行为表头或无表头的方法
python读csv文件时指定行为表头或无表头的方法 pd.read_csv()方法中header参数,默认为0,标签为0(即第1行)的行为表头.若设置为-1,则无表头.示例如下: (1)不设置hea ...
- python docx 合并文档 图片_Python检查Word文件中包含特定关键字的所有页码
推荐教材:<Python程序设计基础与应用>(ISBN:9787111606178),董付国,机械工业出版社图书详情:配套资源:用书教师可以联系董老师获取教学大纲.课件.源码.教案.考试系 ...
- 用python处理excel的基本语法_Python对Excel操作详解
Python对Excel操作详解 文档摘要: 本文档主要介绍如何通过python对office excel进行读写操作,使用了xlrd.xlwt和xlutils模块.另外还演示了如何通过Tcl tco ...
- python能处理表格文档_python处理excel表格
1. Python 操作 Excel 的函数库我主要尝试了 3 种读写 Exce... python 导出 oracle excel 报表 (字段名和内容支持中 文字符) 1.需要预先安装两个 pyt ...
- python中几种读取文件的方法_python 逐行读取文件的几种方法
Python四种逐行读取文件内容的方法 下面四种Python逐行读取文件内容的方法, 分析了各种方法的优缺点及应用场景,以下代码在python3中测试通过, python2中运行部分代码已注释,稍加修 ...
- python 删除文件、目录_python脚本删除文件及删除文件目录的方法
类型:数据恢复大小:372KB语言:中文 评分:.9 标签: 立即下载 下面来看一下python里面是如何删除一个文件及文件夹的~~ 首先引入OS模块 import os 删除文件: os.remov ...
- python对excel增删改查_Python之Excel 优雅操作手法 精选
前言 一直想写这一篇,却又一直被事情拖着. 我带的一个项目团队正好进行到这一块儿了,正好,将这事儿办了,以后要用的时候也不用到处找. 半点不求人. 慢慢看,优雅的操作都在后头 Tips:当下2020年 ...
- python查找文件是否存在_python脚本查找文件是否存在的方法
python脚本查找文件是否存在的方法: 1.使用os模块 os模块中的os.path.exists()方法用于检验文件是否存在. 判断文件是否存在import os os.path.exists(t ...
最新文章
- python unsupported operand type(s) for /: 'str' and 'str' can only concatenate str (not int) to s
- kettle 如何使用java代码
- 拥抱.NET 5,从自研微服务框架开始
- 三层架构与设计模式思想部署企业级数据库业务系统开发
- mysql数据库information_schema库中的表说明
- VINS System::ProcessBackEnd()
- debian mysql中文乱码_MySQL中文乱码的解决方法汇总
- 权御天下计算机音乐数字乐谱,权御天下(单音 适合电吉他)
- 异常 未解决 dubbo 打包 使用将所有的文件打在一个包内 会报错误
- python主函数_python main函数
- 阿里巴巴开源技术体系
- 【侠客行】Lombok深度解析
- C语言学习之指针 *p++、*(p++)、(*p)++
- 苹果订阅服务器端开发
- 矩阵论极简笔记(2):列空间、正交补、零空间、行空间
- linux下的SAMBA服务------SMB协议
- 为什么我的CNN石乐志?我只是平移了一下图像而已
- 盘点明年让你高攀不起的50个项目
- 计算机屏幕自己动,电脑显示器老是自动调整怎么办
- ios提醒事项和日历同步优化