在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

安装:

  1. pip install xlrd

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

  1. import xlrd
  2. book = xlrd.open_workbook("pcat.xls")
  3. print("The number of worksheets is {0}".format(book.nsheets))
  4. print("Worksheet name(s): {0}".format(book.sheet_names()))
  5. sh = book.sheet_by_index(0)
  6. print("{0} {1} {2}".format(sh.name, sh.nrows, sh.ncols))
  7. print("Cell B3 is {0}".format(sh.cell_value(rowx=2, colx=1)))
  8. for rx in range(sh.nrows):
  9. 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
安装:

  1. pip install xlwt

使用:

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

  1. import xlwt
  2. from datetime import datetime
  3.  
  4. style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',
  5. num_format_str='#,##0.00')
  6. style1 = xlwt.easyxf(num_format_str='YYYY-MM-DD HH:MM:SS')
  7.  
  8. wb = xlwt.Workbook()
  9. ws = wb.add_sheet('A Test Sheet')
  10.  
  11. ws.write(0, 0, 1234.56, style0)
  12. ws.write(1, 0, datetime.now(), style1)
  13. ws.write(2, 0, 1)
  14. ws.write(2, 1, 1)
  15. ws.write(2, 2, xlwt.Formula("A3+B3"))
  16.  
  17. 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个模块)

  1. pip install xlutils

使用:

  1. import xlrd
  2. import xlwt
  3. import xlutils
  4. import xlutils.copy as copy
  5.  
  6. rdbook = xlrd.open_workbook('first.xls')
  7. wtbook = copy.copy(rdbook)
  8. wtsheet = wtbook.get_sheet(0)
  9. type(wtsheet)
  10. wtsheet.write(0,0,'pcat.cc')
  11. wtbook.save('second.xls')

0x04 openpyxl

A Python library to read/write Excel 2010 xlsx/xlsm files.
官方文档:https://openpyxl.readthedocs.io/en/stable/
安装:

  1. pip install openpyxl

使用:
写xlsx文件

  1. from openpyxl import Workbook
  2. wb = Workbook()
  3. # grab the active worksheet
  4. ws = wb.active
  5. # Data can be assigned directly to cells
  6. ws['A1'] = 42
  7. # Rows can also be appended
  8. ws.append([1, 2, 3])
  9. # Python types will automatically be converted
  10. import datetime
  11. ws['A2'] = datetime.datetime.now()
  12. # Save the file
  13. wb.save("sample.xlsx")

读xlsx文件

  1. from openpyxl import load_workbook
  2. wb = load_workbook(filename='pcat.xlsx')
  3. sheet_ranges = wb['Sheet1']
  4. 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
安装:

  1. pip install xlsxwriter

使用:

  1. import xlsxwriter
  2. workbook = xlsxwriter.Workbook('hello_world.xlsx')
  3. worksheet = workbook.add_worksheet()
  4. worksheet.write('A1', 'Hello world')
  5. 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. ResultMap和ResultType在使用中的区别、MyBatis中Mapper的返回值类型

    在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解 resultType:当使 ...

  2. JVM | JVM体系结构认知

    虚拟机 何为虚拟机呢?虚拟机是模拟执行某种指令集体系结构(ISA)的软件,是对操作系统和硬件的一种抽象.其软件模型如下图所示: 计算机系统的这种抽象类似于面向对象编程(OOP)中的针对接口编程泛型(或 ...

  3. keras Dense 层

    文档地址:https://keras.io/layers/core/#dense keras.layers.Dense(units, activation=None, use_bias=True, k ...

  4. apk增加系统签名的方法

    1.命令行方法给apk加系统签名在Linux环境:java -Djava.library.path=. -jar signapk.jar platform.x509.pem platform.pk8 ...

  5. 问题:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile)

    一:问题 今天编译maven 项目构建失败,提示内容如下: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler ...

  6. ubuntu14.04 rabbitmq重启丢失用户信息

    一.rabbitmq数据是根据当前hostname作为node节点作为数据名保存 二.添加rabbimq用户sudo rabbitmqctl add_user tlwlmy tlwlmysudo ra ...

  7. selenium元素input的value值设置【node.js版本】

    driver.executeScript(‘document.getElementById(“id”).value=“value”’); 这个操作就类似于//$("#id").va ...

  8. js实现两个文本框数值的加减乘除运算

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>& ...

  9. maven项目如何从私服nexus中下载依赖包

    maven项目如何从私服nexus中下载依赖包   解决方法: 1.打开maven的config目录中settings.xml文件 2.在<profile></profiles> ...

  10. (十八)Centos之firewall 防火墙命令

    如果你的系统上没有安装使用命令安装 #yum install firewalld  //安装firewalld 防火墙 开启服务 # systemctl start firewalld.service ...