python处理Excel

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类型,是只读的,不能对其进行操作。

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‘)

  

  

时间: 2024-08-28 06:14:55

python处理Excel的相关文章

使用python读写excel

项目中要在excel要跨工作簿根据一列数据获取另一列的数据,而excel本身的函数vlookup一直不太好用,只能用程序进行处理了,最近刚接触了python,灵机一动使用Python进行处理,先将json格式处理成csv格式,再保存为excel,由于工作日报中要根据之前的json数据进行统计,数据行较大,人工进行工作量较大,然后使用python根据excel的内容进行处理. import xlrd import xlwt from xlutils.copy import copy import

python读取excel文件(xrld模块)

Python读取excel文件 一.python  xlrd模块 安装 mac 下安装python  xlrd模块 http://www.crifan.com/python_read_excel_xls_file_xlrd/comment-page-1/ python setup.py install 在mac 下出现的错误是 http://stackoverflow.com/questions/18199853/error-could-not-create-library-python-2-7

Python对Excel操作详解

  Python对Excel操作详解 文档摘要: 本文档主要介绍如何通过python对office excel进行读写操作,使用了xlrd.xlwt和xlutils模块.另外还演示了如何通过Tcl  tcom包对excel操作. 关键字: Python.Excel.xlrd.xlwt.xlutils.TCl.tcom     1 Python简介 Python是一种面向对象.直译式电脑编程语言,具有近二十年的发展历史,成熟且稳定.它包含了一组完善而且容易理解的标准库,能够轻松完成很多常见的任务.

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

简介 xlrd,xlwt和xlutils是用Python处理Excel文档(*.xls)的高效率工具.其中,xlrd只能读取xls,xlwt只能新建xls(不可以修改),xlutils能将xlrd.Book转为xlwt.Workbook,从而得以在现有xls的基础上修改数据,并创建一个新的xls,实现修改. (以下属性或方法并非全部,需要更多属性请参看文档:建议先参考文末Demo,再深入了解) xlrd Book(class) 由xlrd.open_work("example.xls"

八、python操作excel及网络编程和异常处理

一.python操作excel 1.读excel,xlrd模块用来读excel # book = xlrd.open_workbook(r'students.xlsx')#打开excel# print(book.sheet_names())#获取所有sheet的名字# sheet = book.sheet_by_index(0)#根据sheet页的位置去取sheet# sheet2 = book.sheet_by_name('Sheet2')#根据sheet页的名字获取sheet页# print

Python处理Excel表格

对于操作Excel,需要Xlrd/xlwt这两个模块,下面推荐出系统性学习的网址: python操作Excel读写--使用xlrd 官方文档 Python 使用 Xlrd/xlwt 操作 Excel 用Python读写Excel文件 1 Windows下先下载 xlwt 和xlrd 2. 解压xlrd-0.9.2.tar.gz至指定文件夹 3. 在CMD控制台下切换至上述指定文件夹路径,输入命令 python setup.py install 完成安装 Linux下安装同安装python 或者用

python读取excel表格生成sql语句 第一版

由于单位设计数据库表·,都用sql.不知道什么原因不用 powerdesign或者ermaster工具,建表很痛苦  作为程序猿当然要想办法解决,用Python写一个程序解决 需要用到 xlrd linux下 sudo pip install xlrd 主要是适用于db2数据库 excel 表结构 其中 number是不正确的字段类型 不知道同事为啥这么设置.这里程序里有纠错,这个程序就是将sql语句拼好. __author__ = 'c3t' # coding:utf-8 import xlr

Python之Excel操作

Python的Excel操作需要另外下载安装对应Python版本的xlrd和xlwt包,用于对Excel的读取和写入. 安装方法:直接解压后,在字符命令界面cd到setup.py的目录,执行命令"Python setup.py install"即可. xlrd(下面有些是方法,有些是属性,属性后面不加括号) 1. excel = xlrd.open_workbook(excel_path):打开指定路径的Excel文件,得到对应Excel的Excel对象(整个Excel文件的对象).

Python处理Excel(转载)

1. Python 操作 Excel 的函数库 我主要尝试了 3 种读写 Excel 的方法: 1> xlrd, xlwt, xlutils: 这三个库的好处是不需要其它支持,在任何操作系统上都可以使用.xlrd 可以读取 .xls, .xlsx 文件,非常好用:但因为 xlwt 不能直接修改 Excel 文档,必须得复制一份然后另存为其它文件,而且据说写复杂格式的 Excel 文件会出现问题,所以我没有选它来写 Excel 文件. 2> openpyxl: 这个库也是不需要其它支持的,而且据

python 操作 excel

python操作execel主要是读写 读 通过 http://pypi.python.org/pypi/xlrd 写 通过 http://pypi.python.org/pypi/xlwd 下载tar包,解压出来,安装即可, 如果没有权限,将xlrd/xlrd拷贝到当前目录下,也可以使用. 如下是xlrd的使用例子 1 # -*- coding: utf-8 -*- 2 import xdrlib ,sys 3 import xlrd 4 import sys 5 def open_excel