看到一篇很好的python读写excel方式的对比文章: 用Python读写Excel文件

关于其他版本的excel,可以通过他提供的链接教程进行学习。

XlsxWriter:

https://github.com/jmcnamara/XlsxWriter

http://xlsxwriter.readthedocs.org

openpyxl: http://openpyxl.readthedocs.io/en/default/

Microsoft excel API:https://msdn.microsoft.com/en-us/library/fp179694.aspx

简介

xlrd用来读取excel文件,xlwt用来写excel文件,它们合作来对excel进行操作。

官方文档:http://www.python-excel.org/

xlrd官方介绍:https://pypi.python.org/pypi/xlrd/1.0.0

xlwt官方介绍:https://pypi.python.org/pypi/xlwt/1.1.2

xlutils官方介绍:https://pypi.python.org/pypi/xlutils

http://xlutils.readthedocs.io/en/latest/

1. 关于xlrd:

Library for developers to extract data from Microsoft Excel (tm) spreadsheet files

Extract data from Excel spreadsheets (.xls and .xlsx, versions 2.0 onwards) on any platform. Pure Python (2.6, 2.7, 3.2+). Strong support for Excel dates. Unicode-aware.

翻译过来总结就是:

xlrd 可以在任意平台上读取的excel为: .xls以及 .xlsx 。

xlrd支持和的python版本是: 2.6,2.7 , 3.2+。

2. 关于xlwt:

Library to create spreadsheet files compatible with MS Excel 97/2000/XP/2003 XLS files, on any platform, with Python 2.6, 2.6, 3.3+
This is a library for developers to use to generate spreadsheet files compatible with Microsoft Excel versions 95 to 2003.

翻译过来总结就是:

xlwt支持的excel版本是: Microsoft excel版本 95---2003,也就是 xls文件。

xlwt支持的python版本是:2.6 , 3.3+.

3. 关于xlutils:

This package provides a collection of utilities for working with Excel files. Since these utilities may require either or both of the xlrd and xlwt packages, they are collected together here, separate from either package.

Currently available are:

xlutils.copy
Tools for copying xlrd.Book objects to xlwt.Workbook objects.
xlutils.display
Utility functions for displaying information about xlrd-related objects in a user-friendly and safe fashion.
xlutils.filter
A mini framework for splitting and filtering Excel files into new Excel files.
xlutils.margins
Tools for finding how much of an Excel file contains useful data.
xlutils.save
Tools for serializing xlrd.Book objects back to Excel files.
xlutils.styles
Tools for working with formatting information expressed in styles.

翻译过来总结就是:

如果需要在 xlrd以及 xlwt之间进行交互的话,比如拷贝 xlrd 到 xlwt 需要用到xlutils。

目前提供了 copy、display、filter、margins、Save、styles几个函数。

安装 xlrd 和 xlwt

pip install xlrd
pip install xlwt
pip install xlutils
pip list xlrd (1.0.0)
xlutils (2.0.0)
xlwt (1.1.2)

使用

1. 新建一个excel文件(xlwt)

#coding='utf-8'

import xlwt
from datetime import datetime def set_style(font_name,font_height,bold=False):
style=xlwt.XFStyle() font=xlwt.Font()
font.name=font_name # 'Times New Roman'
font.height=font_height
font.bold=bold
font.colour_index=4 borders=xlwt.Borders()
borders.left=6
borders.right=6
borders.top=6
borders.bottom=6 style.font=font
style.borders=borders
return style def write_to_excel_xlwt():
'''Write content to a new excel'''
new_workbook=xlwt.Workbook()
new_sheet=new_workbook.add_sheet("SheetName_test")
new_sheet.write(0,0,"hello")
#write cell with style
new_sheet.write(0,1,"world",set_style("Times New Roman", 220, True)) style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',num_format_str='#,##0.00')
style1 = xlwt.easyxf(num_format_str='D-MMM-YY')
new_sheet.write(1, 0, 1234.56, style0)
new_sheet.write(1, 1, datetime.now(), style1) #write cell with formula
new_sheet.write(2,0,5)
new_sheet.write(2,1,8)
new_sheet.write(3,0, xlwt.Formula("A3+B3")) new_workbook.save(r"NewCreateWorkbook.xls") #if change to xlsx,then open failed if __name__=="__main__":
write_to_excel_xlwt()

代码执行之后,在当前路径下生成excel文件 “NewCreateWorkbook.xls”。内容如下 :

2. 读取excel文件(xlrd)

#coding='utf-8'

import xlrd

def read_excel_xlrd():
'''Read Excel with xlrd'''
#file
TC_workbook=xlrd.open_workbook(r"NewCreateWorkbook.xls") #sheet
all_sheets_list=TC_workbook.sheet_names()
print("All sheets name in File:",all_sheets_list) first_sheet=TC_workbook.sheet_by_index(0)
print("First sheet Name:",first_sheet.name)
print("First sheet Rows:",first_sheet.nrows)
print("First sheet Cols:",first_sheet.ncols) second_sheet=TC_workbook.sheet_by_name("SheetName_test")
print("Second sheet Rows:",second_sheet.nrows)
print("Second sheet Cols:",second_sheet.ncols) first_row=first_sheet.row_values(0)
print("First row:",first_row)
first_col=first_sheet.col_values(0)
print("First Column:",first_col) # cell
cell_value=first_sheet.cell(1,0).value
print("The 1th method to get Cell value of row 2 & col 1:",cell_value)
cell_value2=first_sheet.row(1)[0].value
print("The 2th method to get Cell value of row 2 & col 1:",cell_value2)
cell_value3=first_sheet.col(0)[1].value
print("The 3th method to get Cell value of row 2 & col 1:",cell_value3) if __name__=="__main__":
read_excel_xlrd()

运行之后,控制台输出如下 :

All sheets name in File: ['SheetName_test']
First sheet Name: SheetName_test
First sheet Rows: 4
First sheet Cols: 2
Second sheet Rows: 4
Second sheet Cols: 2
First row: ['hello', 'world']
First Column: ['hello', 1234.56, 5.0, '']
The 1th method to get Cell value of row 2 & col 1: 1234.56
The 2th method to get Cell value of row 2 & col 1: 1234.56
The 3th method to get Cell value of row 2 & col 1: 1234.56

3. 向已经存在的excel写入(xlrd&xlwt&xlutils)

#coding='utf-8'

import xlrd
import xlwt
from xlutils.copy import copy def write_to_existed_file():
'''Write content to existed excel file with xlrd&xlutils&xlwt'''
rb = xlrd.open_workbook(r"NewCreateWorkbook.xls",formatting_info=True) wb = copy(rb)
ws = wb.get_sheet(0) font=xlwt.Font()
font.name="Times New Roman"
font.height=220
font.bold=False borders = xlwt.Borders()
borders.left = xlwt.Borders.THIN
borders.right = xlwt.Borders.THIN
borders.top = xlwt.Borders.THIN
borders.bottom = xlwt.Borders.THIN pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 2 cell_style = xlwt.XFStyle()
cell_style.font = font
cell_style.borders = borders
cell_style.pattern = pattern ws.write(6,7,"hello world",cell_style)
wb.save(r"NewCreateWorkbook.xls") if __name__=="__main__":
write_to_existed_file()

运行之后,excel文件内容如下:

python 3 操作 excel的更多相关文章

  1. Python读写操作Excel模块_xlrd_xlwt_xlutils

    Python 读写操作Excel -- 安装第三方库(xlrd.xlwt.xlutils.openpyxl) 如果仅仅是要以表单形式保存数据,可以借助 CSV 格式(一种以逗号分隔的表格数据格式)进行 ...

  2. Python“文件操作”Excel篇(上)

    大家好,我们今天来一起探索一下用Python怎么操作Excel文件.与word文件的操作库python-docx类似,Python也有专门的库为Excel文件的操作提供支持,这些库包括xlrd.xlw ...

  3. Python Pandas操作Excel

    Python Pandas操作Excel 前情提要 ☟ 本章使用的 Python3.6 Pandas==0.25.3 项目中需要用到excel的文件字段太多 考虑到后续字段命名的变动以及中文/英文/日 ...

  4. Python 读写操作Excel —— 安装第三方库(xlrd、xlwt、xlutils、openpyxl)

    数据处理是 Python 的一大应用场景,而 Excel 则是最流行的数据处理软件.因此用 Python 进行数据相关的工作时,难免要和 Excel 打交道. 如果仅仅是要以表单形式保存数据,可以借助 ...

  5. Python之操作Excel、异常处理、网络编程

    知识补充: 1.falsk模块中一些方法总结 import flask from flask import request,jsonify server = flask.Flask(__name__) ...

  6. Python - 常规操作Excel - 第二十六天

    前言 作为一名资深程序员,通过代码熟练操作Excel是必不可少的技能,本章主要讲解Python通过openpyxl第三方库(官方文件说明)对Excel进行操作,使Excel程序化操作更为简单快捷. o ...

  7. python中操作excel数据

    python操作excel,python有提供库 本文介绍openpyxl,他只支持新型的excell( xlsx)格式,读取速度还可以 1.安装 pip install openpyxl 2.使用 ...

  8. python简单操作excel

    python操作excel 写入excel # 写入excel import xlwt # 创建xls对象 wb = xlwt.Workbook() # 新增两个表单页(sheet1) sh1 = w ...

  9. python openpyxl 操作 excel

    初识与安装 Openpyxl is a Python library for reading and writing Excel 2010 xlsx/xlsm/xltx/xltm files. 安装 ...

随机推荐

  1. tomcat的server.xml详解

    Tomcat服务器是由一系列可配置的组件构成,其核心组件是Catalina   Servlet容器,它是所有其他Tomcat组件的顶层容器.Tomcat的组件可以在<CATALINA_HOME& ...

  2. Android 之 Socket 通信

    Android 之 Socket 通信 联系一下 Socket 编程,之后需要将一个 JavaEE 项目移植到 Android,暂时现尝试写一个简单的 DEMO,理解一下 Socket Server ...

  3. 利用SpringMVC参数绑定实现动态插入数据

    javabean代码:public class User { private String firstName; private String lastName; public String getF ...

  4. HTML网页图片滚动代码

    <!--下面是向上滚动代码--> <div id=butong_net_top style=overflow:hidden;height:100;width:90;> < ...

  5. python中的“引用”和C++的引用

    python并不刻意区分“按值传递”和“按引用传递”. 在底层,python将值分为不可变对象(比如int,str)和可变对象(比如列表).所有的变量都是对某个对象的引用,赋值(=)和函数参数传递,都 ...

  6. cocos2d-x Android 环境搭建问题汇总

    初次接触Cocos2d-x,准备搭建一个hello world的Android环境,问题遇到很多.在此记录,为自己,也为大家,避免重走弯路! 具体的环境搭建,可以参考官方的文档.在Windows7平台 ...

  7. 博客系统-3.0CodeIgniter系统SAE版本的配置 application/config/

    autoload.php(系统启动时自动加载的文件:包,类库,驱动,方法助手,配置) $autoload['libraries'] = array('database', 'access', 'pag ...

  8. TCP/IP笔记 应用层(3)——HTTP

    1. URL URL(Uniform Resource Locator) 相当于一个文件名在网络范围的扩展. 1.1 格式 schema://host[:port#]/path/.../[?query ...

  9. VS-FluentData 单元测试

    1. 使用VS2013建立一个控制台工程: using System; using System.Collections.Generic; using System.Linq; using Syste ...

  10. 字符串搜索算法Boyer-Moore

    整理日: 2015年2月16日 1. 主要特征 假设文本串text长度为n,模式串pattern长度为m,BM算法的主要特征为: 从右往左进行比较匹配(一般的字符串搜索算法如KMP都是从从左往右进行匹 ...