【转】使用Python操作XLS文件
有一次TE需要一个**信息列表,我用python导出了一个txt文件丢给了他们,结果他们很不悦哇,呵呵,因为他们要把几百项数据人肉到xls文件中 作为列表输出,工作量太大,便要求我导出成xls文件然后再给他们,我以编程实现太难推脱了,今天看了下python操作excle的方法,发现相当简 单,呃,苦了TE们……我在网上找了下,发现至少有两种方法,第一种是直接操作excle的com库,当然python自带的lib里面已经给我们封装好了实现,直接使用就可以 了,win32com.client,这种方法甚至可以直接把excle的进程调用起来。用法很简单,网上的文章也汗牛充栋,就不详细解说了,给个小例子 吧,嘻嘻。这种只能在windows下运行,并且需要安装MS Excel。
viewplaincopy to clipboardprint?
[*]# -*- coding: utf-8 -*-
[*]from win32com.client import constants, Dispatch
[*]
[*]xlsApp = Dispatch("Excel.Application")
[*]# 通过赋值Visible为True或者False可以控制是否 调出excle
[*]xlsApp.Visible = 1
[*]
[*]# xlsBook = xlsApp.Workbooks.Open("c:\\magictong.xls")
[*]# xlsSht = xlsBook.Worksheets("sheet1")
[*]
[*]xlsBook = xlsApp.Workbooks.Add()
[*]xlsSht = xlsBook.Sheets.Add()
[*]xlsSht.Cells(2, 3).Value = "Tecent QQ"
[*]xlsSht.Cells(2, 3).Font.Color = 0xff0000
[*]
[*]xlsSht.Name = "GCD go to bell"
[*]xlsBook.SaveAs("c:\\magictong.xls")
[*]xlsApp.Quit()
[*]
[*]print "__end"
# -*- coding: utf-8 -*- from win32com.client import constants, DispatchxlsApp = Dispatch("Excel.Application") # 通过赋值Visible为True或者False可以控制是否调出excle xlsApp.Visible = 1# xlsBook = xlsApp.Workbooks.Open("c:\\magictong.xls") # xlsSht = xlsBook.Worksheets("sheet1")xlsBook = xlsApp.Workbooks.Add() xlsSht = xlsBook.Sheets.Add() xlsSht.Cells(2, 3).Value = "Tecent QQ" xlsSht.Cells(2, 3).Font.Color = 0xff0000xlsSht.Name = "GCD go to bell" xlsBook.SaveAs("c:\\magictong.xls") xlsApp.Quit()print "__end" 第二种方法是使用pyExcelerator开源库,这个可以到http://sourceforge.net/projects/pyexcelerator/去 下载,不过这个链接不是很好下,另外我传了一份到csdn上面也可以去下,比较稳定,http://d.download.iyunv.com/down/1884158/magictong。 是一个zip包,解压后的文件如下:
examples目录是给出的很多小例子,很全面,把这些例子搞明白,基本上使用pyExcelerator的基本方法你也就全搞明白了,其他目录不用管 了,tools目录里面是一些转换工具,从xls文件转换到txt啦,html啦,有兴趣也可以看看。下面说怎么安装吧,其实readme.txt里面已 经说的很清楚了,仔细看下这个文件吧,嘿嘿,这个目录下还有一个setup.py文件,这个文件是用于库的安装的,安装方法是pythonsetup.pyinstall,在win32下直接写个bat文件把这句写进去,放在和setup.py同一级目录,直接运行就over了,里面有cmd.bat文件是 我加在里面的。
安装好了,咱们就开始使用吧,先看看解析xls文件,简单几句就搞定了:
viewplaincopy to clipboardprint?
[*]from pyExcelerator import *
[*]sheets = parse_xls("c:/a.xls")
[*]print sheets
from pyExcelerator import * sheets = parse_xls("c:/a.xls") print sheets xls的源文件如下所示:
输出可不简单:
viewplaincopy to clipboardprint?
[*][(u'Sheet1', {(0, 1): 1.0, (7, 3): 10.890000000000001, (0, 0): u'tonglei', (1, 1): u'qq', (2, 2): u'\u7acb\u91cc\u4e09\u77f3', (0, 3): 5.0, (0, 2): 2.0}), (u'Sheet2', {}), (u'Sheet3', {})]
[(u'Sheet1', {(0, 1): 1.0, (7, 3): 10.890000000000001, (0, 0): u'tonglei', (1, 1):u'qq', (2, 2): u'\u7acb\u91cc\u4e09\u77f3', (0, 3): 5.0, (0, 2): 2.0}),(u'Sheet2', {}), (u'Sheet3', {})] 仔细研究下这个数据,最外层是一个list,里面有三个元组,对应三个sheet,每个元组有两项数据,第一个是sheet的名字,如"Sheet1", 第二项是一个字典,字典中的每一项的key是一个以xls文件的行列两项为元素的元组,而value就是该行列对应的数值,不过这个里面的类型可能就不会 像xls里面那么丰富了。得到了这些数据,能干什么……我就不说了。
再看看怎么写xls文件:
viewplaincopy to clipboardprint?
[*]# -*- coding: cp936 -*-
[*]from pyExcelerator import *
[*]
[*]# 创建一个工作表
[*]w = Workbook()
[*]# 增加一个sheet并设定名称
[*]ws = w.add_sheet(u"中文的sheet")
[*]
[*]# 设置样式
[*]font0 = Font()
[*]font0.name = 'Times New Roman'
[*]font0.struck_out = True
[*]font0.bold = True
[*]
[*]style0 = XFStyle()
[*]style0.font = font0
[*]
[*]ws.write(1, 1, u"magictong", style0)
[*]ws.write(1, 2, 45)
[*]
[*]# 我想写一个数值
[*]style1 = XFStyle()
[*]style1.num_format_str = "0"
[*]ws.write(2, 4, 12, style1)
[*]ws.write(2, 5, 4, style1)
[*]
[*]# 保存
[*]w.save("c:\\mini.xls")
# -*- coding: cp936 -*- from pyExcelerator import *# 创建一个工作表 w = Workbook() # 增加一个sheet并设定名称 ws = w.add_sheet(u"中文的sheet")# 设置样式 font0 = Font() font0.name = 'Times New Roman' font0.struck_out = True font0.bold = Truestyle0 = XFStyle() style0.font = font0ws.write(1, 1, u"magictong", style0) ws.write(1, 2, 45)# 我想写一个数值 style1 = XFStyle() style1.num_format_str = "0" ws.write(2, 4, 12, style1) ws.write(2, 5, 4, style1)# 保存 w.save("c:\\mini.xls") 输出xls文件如下:
输出格式是很多的,可以看一个自带的例子:
viewplaincopy to clipboardprint?
[*]#!/usr/bin/env python
[*]# -*- coding: windows-1251 -*-
[*]# Copyright (C) 2005 Kiseliov Roman
[*]__rev_id__ = """$Id: num_formats.py,v 1.1 2005/07/20 07:24:11 rvk Exp $"""
[*]
[*]
[*]from pyExcelerator import *
[*]
[*]w = Workbook()
[*]ws = w.add_sheet('Hey, Dude')
[*]
[*]fmts = [
[*] 'general',
[*] '0',
[*] '0.00',
[*] '#,##0',
[*] '#,##0.00',
[*] '"$"#,##0_);("$"#,##',
[*] '"$"#,##0_);("$"#,##',
[*] '"$"#,##0.00_);("$"#,##',
[*] '"$"#,##0.00_);("$"#,##',
[*] '0%',
[*] '0.00%',
[*] '0.00E+00',
[*] '# ?/?',
[*] '# ??/??',
[*] 'M/D/YY',
[*] 'D-MMM-YY',
[*] 'D-MMM',
[*] 'MMM-YY',
[*] 'h:mm AM/PM',
[*] 'h:mm:ss AM/PM',
[*] 'h:mm',
[*] 'h:mm:ss',
[*] 'M/D/YY h:mm',
[*] '_(#,##0_);(#,##0)',
[*] '_(#,##0_);(#,##0)',
[*] '_(#,##0.00_);(#,##0.00)',
[*] '_(#,##0.00_);(#,##0.00)',
[*] '_("$"* #,##0_);_("$"* (#,##0);_("$"* "-"_);_(@_)',
[*] '_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)',
[*] '_("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(@_)',
[*] '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)',
[*] 'mm:ss',
[*] ':mm:ss',
[*] 'mm:ss.0',
[*] '##0.0E+0',
[*] '@'
[*]]
[*]
[*]i = 0
[*]for fmt in fmts:
[*] ws.write(i, 0, fmt)
[*]
[*] style = XFStyle()
[*] style.num_format_str = fmt
[*]
[*] ws.write(i, 4, -1278.9078, style)
[*]
[*] i += 1
[*]
[*]w.save('num_formats.xls')
#!/usr/bin/env python # -*- coding: windows-1251 -*- # Copyright (C) 2005 Kiseliov Roman __rev_id__ = """$Id: num_formats.py,v 1.1 2005/07/20 07:24:11 rvk Exp$""" from pyExcelerator import *w = Workbook() ws = w.add_sheet('Hey, Dude')fmts = [ 'general', '0', '0.00', '#,##0', '#,##0.00', '"$"#,##0_);("$"#,##', '"$"#,##0_);("$"#,##', '"$"#,##0.00_);("$"#,##', '"$"#,##0.00_);("$"#,##', '0%', '0.00%', '0.00E+00', '# ?/?', '# ??/??', 'M/D/YY', 'D-MMM-YY', 'D-MMM', 'MMM-YY', 'h:mm AM/PM', 'h:mm:ss AM/PM', 'h:mm', 'h:mm:ss', 'M/D/YY h:mm', '_(#,##0_);(#,##0)', '_(#,##0_);(#,##0)', '_(#,##0.00_);(#,##0.00)', '_(#,##0.00_);(#,##0.00)', '_("$"* #,##0_);_("$"* (#,##0);_("$"* "-"_);_(@_)', '_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)', '_("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(@_)', '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)', 'mm:ss', ':mm:ss', 'mm:ss.0', '##0.0E+0', '@' ]i = 0 for fmt in fmts: ws.write(i, 0, fmt) style = XFStyle() style.num_format_str = fmt ws.write(i, 4, -1278.9078, style) i += 1w.save('num_formats.xls') 可以运行一下,看看都有些什么格式的可以输出,不过我找了下,不知道怎么输出一个标准的数值。
页:
[1]