Python中一般使用xlrd库来读取Excel文件,使用xlwt库来生成Excel文件,使用xlutils库复制和修改Excel文件。这三个库只支持到Excel2003。

python-excel主页地址:http://www.python-excel.org/

xlrd

地址:https://pypi.python.org/pypi/xlrd

github地址:https://github.com/python-excel/xlrd

打开excel文件,获取一个Book()对象:

import xlrd
book = xlrd.open_workbook("myfile.xls")

获取sheets数目:

>>> book.nsheets
3

获取sheets列表:

>>> book.sheets()
[<xlrd.sheet.Sheet object at 0x01A93970>, <xlrd.sheet.Sheet object at 0x01A93950>, <xlrd.sheet.Sheet object at 0x01A93E70>]

获取sheets name列表:

>>> book.sheet_names()
[u'Sheet1', u'Sheet2', u'Sheet3']

获取Book()中的Sheet:

sheet = book.sheets()[0]          #sheets返回一个sheet列表
sheet = book.sheet_by_index(0) #通过索引顺序获取
sheet = book.sheet_by_name(u'Sheet1')#通过名称获取

获取行数,列数,名字:

>>> sheet.nrows
1002
>>> sheet.ncols
11
>>> sheet.name
u'Sheet1'

获取某行,某行值列表,某列,某列值列表:

sheet.row(i)
sheet.row_values(i)
sheet.col(i)
sheet.col_values(i)

获取单元格的值:

cell = sheet.cell(i,j)
cell_value = sheet.cell_value(i,j)
cell_value = sheet.cell(i,j).value

需要注意的是,用xlrd读取excel是不能对其进行操作的:xlrd.open_workbook()方法返回xlrd.Book类型,是只读的,不能对其进行操作。

xlrd读取Excel中的日期:

一个值为1984/5/30的单元格

>>> print sheet.cell(2,7)   #直接读取是个日期格式,读value会返回30832.0的值
xldate:30832.0
>>> xlrd.xldate_as_tuple(sheet.cell(2,7).value, 0) #变为元祖,第二个参数有两种取值,0或者1,0是以1900-01-01为基准的日期,而1是1904-01-01为基准的日期。
(1984, 5, 30, 0, 0, 0)
>>> xlrd.xldate.xldate_as_datetime(sheet.cell(2,7).value, 1)
datetime.datetime(1988, 5, 31, 0, 0)
>>> xlrd.xldate.xldate_as_datetime(sheet.cell(2,7).value, 0)
datetime.datetime(1984, 5, 30, 0, 0)

xlwt

地址:http://pypi.python.org/pypi/xlwt,适用于python2.3-2.7

xlwt-future:https://pypi.python.org/pypi/xlwt-future/0.8.0,适用于Python 2.6-3.3

github地址:https://github.com/python-excel/xlwt

创建一个Excel文件并创建一个Sheet:

from xlwt import *
book = Workbook()
sheet = book.add_sheet('Sheet1')
book.save('myExcel.xls')

Workbook类可以有encoding和style_compression参数。

encoding,设置字符编码,style_compression,表示是否压缩。这样设置:w = Workbook(encoding='utf-8'),就可以在excel中输出中文了。默认是ascii。

向sheet写入内容:

sheet.write(r, c, label="", style=Style.default_style)

简单写入:

sheet.write(0, 0, label = 'Row 0, Column 0 Value')

设置格式写入:

font = xlwt.Font() # 字体
font.name = 'Times New Roman'
font.bold = True
font.underline = True
font.italic = True
style = xlwt.XFStyle() # 创建一个格式
style.font = font # 设置格式字体
sheet.write(1, 0, label = 'Formatted value', style) # Apply the Style to the Cell
book.save('myExcel.xls')

写入日期:

style = xlwt.XFStyle()
style.num_format_str = 'M/D/YY' # Other options: D-MMM-YY, D-MMM, MMM-YY, h:mm, h:mm:ss, h:mm, h:mm:ss, M/D/YY h:mm, mm:ss, [h]:mm:ss, mm:ss.0
sheet.write(0, 0, datetime.datetime.now(), style)

写入公式:

sheet.write(0, 0, 5) # Outputs 5
sheet.write(0, 1, 2) # Outputs 2
sheet.write(1, 0, xlwt.Formula('A1*B1')) # 输出 "10" (A1[5] * A2[2])
sheet.write(1, 1, xlwt.Formula('SUM(A1,B1)')) # 输出 "7" (A1[5] + A2[2])

写入链接:

sheet.write(0, 0, xlwt.Formula('HYPERLINK("http://www.google.com";"Google")')) #输出 "Google"链接到http://www.google.com

xlutils

地址:http://pythonhosted.org/xlutils/

github地址:https://github.com/python-excel/xlutils

xlutils.copy.copy(wb)

复制一个xlrd.Book对象,生成一个xlwt.Workbook对象,可以对xlwt.Workbook进行修改。

from xlrd import open_workbook
from xlutils.copy import copy
book = open_workbook('myExcel.xls')
wbook = copy(book) #wbook即为xlwt.WorkBook对象 wsheet = wbook.get_sheet(0) #通过get_sheet()获取的sheet有write()方法
wsheet.write(0, 0, 'value')
wb.save('myExcel.xls')

  

  

python处理Excel的更多相关文章

  1. Python导出Excel为Lua/Json/Xml实例教程(三):终极需求

    相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 Python导出E ...

  2. Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验

    Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...

  3. Python导出Excel为Lua/Json/Xml实例教程(一):初识Python

    Python导出Excel为Lua/Json/Xml实例教程(一):初识Python 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出 ...

  4. python读取excel一例-------从工资表逐行提取信息

    在工作中经常要用到python操作excel,比如笔者公司中一个人事MM在发工资单的时候,需要从几百行的excel表中逐条的粘出信息,然后逐个的发送到员工的邮箱中.人事MM对此事不胜其烦,终于在某天请 ...

  5. 使用Python将Excel中的数据导入到MySQL

    使用Python将Excel中的数据导入到MySQL 工具 Python 2.7 xlrd MySQLdb 安装 Python 对于不同的系统安装方式不同,Windows平台有exe安装包,Ubunt ...

  6. 使用Python对Excel表格进行简单的读写操作(xlrd/xlwt)

    算是一个小技巧吧,只是进行一些简单的读写操作.让人不爽的是xlrd和xlwt是相对独立的,两个模块的对象不能通用,读写无法连贯操作,只能单独读.单独写,尚不知道如何解决. #①xlrd(读) #cod ...

  7. python操作excel表格(xlrd/xlwt)

    最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...

  8. Python操作Excel

    一.系统性学习 对于操作Excel,需要Xlrd/xlwt这两个模块,下面推荐出系统性学习的网址: python操作Excel读写--使用xlrd 官方文档 Python 使用 Xlrd/xlwt 操 ...

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

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

  10. Python处理Excel文档(xlrd, xlwt, xlutils)

    简介 xlrd,xlwt和xlutils是用Python处理Excel文档(*.xls)的高效率工具.其中,xlrd只能读取xls,xlwt只能新建xls(不可以修改),xlutils能将xlrd.B ...

随机推荐

  1. Python脚本控制的WebDriver 常用操作 <十七> 获取测试对象的属性及内容

    测试用例场景 获取测试对象的内容是前端自动化测试里一定会使用到的技术.比如我们要判断页面上是否显示了一个提示,那么我们就需要找到这个提示对象,然后获取其中的文字,再跟我们的预期进行比较.在webdri ...

  2. PAT乙级真题1008. 数组元素循环右移问题 (20)

    原题: 1008. 数组元素循环右移问题 (20) 时间限制400 ms内存限制65536 kB 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M&g ...

  3. TAG的用法和用途[转]

    用一个例子来说明:一个combobox控件...一个textBox控件...一个datagridview控件!datagridview控件是连接数据库的...combobox和textBox是联合查询 ...

  4. select * from table where 1=1

    转自:http://www.dzwebs.net/2418.html 我们先来看看这个语句的结果:select * from table where 1=1,其中where 1=1,由于1=1永远是成 ...

  5. android studio 中设置apk的版本号

    今天在mainfest.xml中设置版本号为2,(代码获取到的版本号无效) android:versionCode="2" android:versionName="2. ...

  6. qsort/bsearch的应用

    问题描述: Description You have just moved from Waterloo to a big city. The people here speak an incompre ...

  7. [原] perforce 获取本地最近更新的Changelist

    获取perforce客户端最后一次sync的changelist, 前提是中间没有任何代码提交: http://stackoverflow.com/questions/47007/determinin ...

  8. PAT-乙级-1051. 复数乘法 (15)

    1051. 复数乘法 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 复数可以写成(A + Bi)的常规 ...

  9. POJ1426Find The Multiple

    http://poj.org/problem?id=1426 题意 : 输入一个数n,找n的倍数m,这个m所满足的条件是,每一位数只能由0或1组成,在题目的旁边用红色的注明了Special Judge ...

  10. 2013 Multi-University Training Contest 1 Cards

    数据不是很大,直接枚举约数,判断4个条件是否满足! 这样就得到4种卡片,总共2^4种情况,枚举各种情况即可!!! #include<iostream> #include<cmath& ...