在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.

官方文档:https://xlrd.readthedocs.io/en/latest/api.html
github项目:https://github.com/python-excel/xlrd

安装:

pip install xlrd

使用:
只能读.xls、.xlsx文件(xlrd0.8.0+版本支持读取xlsx文件)

import xlrd
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 in range(sh.nrows):
print(sh.row(rx))

0x02 xlwt

xlwt is a library for writing data and formatting information to older Excel files (ie: .xls)

官方文档:https://xlwt.readthedocs.io/en/latest/api.html
github项目:https://github.com/python-excel/xlwt
安装:

pip install xlwt

使用:

用xlwt创建一个简单的.xls文件

import xlwt
from datetime import datetime 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

使用:

import xlrd
import xlwt
import xlutils
import xlutils.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 import Workbook
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
import datetime
ws['A2'] = datetime.datetime.now()
# Save the file
wb.save("sample.xlsx")

读xlsx文件

from openpyxl import load_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

使用:

import xlsxwriter
workbook = xlsxwriter.Workbook('hello_world.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello world')
workbook.close()

注意:
XlsxWriter不支持.xls格式。

python处理Excel文件的几个模块的更多相关文章

  1. [转]用Python读写Excel文件

    [转]用Python读写Excel文件   转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交 ...

  2. python读写Excel文件的函数--使用xlrd/xlwt

    python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket  The ...

  3. Python解析excel文件并存入sqlite数据库

    最近由于工作上的需求 需要使用Python解析excel文件并存入sqlite 就此做个总结 功能:1.数据库设计 建立数据库2.Python解析excel文件3.Python读取文件名并解析4.将解 ...

  4. 用python处理excel文件有多轻松?工作从未如此简单

    最近需要频繁读写 excel 文件,想通过程序对 excel 文件进行自动化处理,发现使用 python 的 openpyxl 库进行 excel 文件读写实在太方便了,结构清晰,操作简单.本文对 o ...

  5. python之路-随笔 python处理excel文件

    小罗问我怎么从excel中读取数据,然后我百了一番,做下记录 以下代码来源于:http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html ...

  6. Python处理Excel文件

    因为工作需求,需要审核一部分query内容是否有效,query储存在Excel中,文本内容为页面的Title,而页面的URL以HyperLink的格式关联到每个Cell. 于是本能的想到用Python ...

  7. 记录:python读取excel文件

    由于最近老是用到python读取excel文件,所以特意记录一下python读取excel文件的大体框架. 库:xlrd(读),直接pip安装即可.想要写excel文件的话,安装xlwd库即可,也是直 ...

  8. 使用Python处理Excel文件的一些代码示例

    笔记:使用Python处理Excel文件的一些代码示例,以下代码来自于<Python数据分析基础>一书,有删改 #!/usr/bin/env python3 # 导入读取Excel文件的库 ...

  9. Python的高级文件操作(shutil模块)

    Python的高级文件操作(shutil模块) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 如果让我们用python的文件处理来进行文件拷贝,想必很多小伙伴的思路是:使用打开2个 ...

随机推荐

  1. Json序列化指定输出字段 忽略属性

    DataContract 服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型. 一旦声明一个类型为DataContract,那么该类型就可以被序列化在服务 ...

  2. appium-FAQ(持续更新...)

    Q1:未安装APP直接启用appium sever,初始化driver :driver = new AndroidDriver(new URL("http://127.0.0.1:4723/ ...

  3. mybatis 的 <![CDATA[ ]]> 标签作用

    有个细节一直没有注意到,今天mark一下: mybatis进行时间比较的时候我们会这么写,一直都是在复制粘贴所以没有注意,一个标签<![CDATA[ ]]> 今天在做另外一个有时间比较sq ...

  4. 各种修改Mysql字符集

    以下方法最好在没有数据时操作,否则有可能导致乱码.如果已有数据,做好备份尝试Mysql迁移由于字符集导致乱码的数据 创建测试环境 mysql> create database test_db; ...

  5. jQuery补充之jQuery扩展/form表单提交/滚动菜单

    jQuery扩展 为了避免重复造轮子,能高效使用别人的代码,所以有了扩展. jQuery扩展有两种方式: 自执行函数方式 定义函数,并执行函数. 自执行函数: (function(jq){ jq.ex ...

  6. (九)Centos之搜索命令whereis、which和字符串搜索命令grep

    一.Centos之命令搜索命令whereis与which 1.1 whereis 命令名(搜索命令所在路径及帮助文档所在位置) 选项: -b :只查找可执行文件位置 -m:只查找帮助文件 1.2 wh ...

  7. (十七)super关键字(转)

    --本文摘自孤傲苍狼博客. 一.super关键字

  8. Jrebel激活方法

    参考 https://www.yanjiayu.cn/posts/3eecb801.html https://gitee.com/gsls200808/JrebelLicenseServerforJa ...

  9. 【web 安全测试Tools】BurpSuite 1.7.32及注册机【无后门版】

    BurpSuite 1.7.32 原版+注册机 下载 链接:https://pan.baidu.com/s/1LFpXn2ulTLlcYZHG5jEjyw 密码:mie3 注意无后门版文件完整性: b ...

  10. 《CNCF × Alibaba云原生技术公开课》知识点自测(三):Kubernetes核心概念

    (单选)1.Kubernetes的中文含义是___. A. 船   B.舵手  C.容器平台  D.起重机 (单选) 2.Kubectl是_____. A. 一个与Kubernetes集群进行交互.管 ...