Posted on 2014-11-21 09:34
pts 阅读(3857)
评论(0) 编辑 收藏 所属分类:
Python
【FROM】
http://blog.csdn.net/suofiya2008/article/details/6284208
有一万年没来这儿写blog了,今天等着发版,抽空来写点东西吧。
python有很多模块都是用来操作excel的,比如xlrd,xlwt,pyExcelerator。用着很方便,但是问题是,只能支持到 excel2003。虽然一般的应用其实足够了,但是如果遇到了导出大量数据(超过65535条)的需求时,excel2003就不够用了。所以我就只好 去找一个能支持excel2007的模块。
google了一下,发现了这个openpyxl,不过网上也没什么中文的文档可以看,于是就自己琢磨琢磨。
I.安装
openpyxl的最新版本是1.5.0 下载地址 http://pypi.python.org/pypi/openpyxl/1.5.0
需求python的版本是2.6+ ,也就是说,如果centOS系统的机器想用,那得升级系统的python。。。
安装就是解压缩,然后cd到目录,然后
python setup.py install
你懂的~
II.读取excel2007文件
-
-
- from openpyxl.reader.excel import load_workbook
- import MySQLdb
- import time
-
-
- startTime = time.time()
-
-
- wb = load_workbook(filename = r'empty_book.xlsx' )
-
-
- print "Worksheet range(s):" , wb.get_named_ranges()
- print "Worksheet name(s):" , wb.get_sheet_names()
-
-
- sheetnames = wb.get_sheet_names()
- ws = wb.get_sheet_by_name(sheetnames[0 ])
-
-
- print "Work Sheet Titile:" ,ws.title
- print "Work Sheet Rows:" ,ws.get_highest_row()
- print "Work Sheet Cols:" ,ws.get_highest_column()
-
-
-
- data_dic = {}
-
-
- for rx in range(ws.get_highest_row()):
-
- temp_list = []
- pid = ws.cell(row = rx,column = 0 ).value
- w1 = ws.cell(row = rx,column = 1 ).value
- w2 = ws.cell(row = rx,column = 2 ).value
- w3 = ws.cell(row = rx,column = 3 ).value
- w4 = ws.cell(row = rx,column = 4 ).value
- temp_list = [w1,w2,w3,w4]
-
- data_dic[pid] = temp_list
-
-
- print 'Total:%d' %len(data_dic)
-
- from openpyxl.reader.excel import load_workbook
- import MySQLdb
- import time
-
- startTime = time.time()
-
- wb = load_workbook(filename = r'empty_book.xlsx')
-
- print "Worksheet range(s):", wb.get_named_ranges()
- print "Worksheet name(s):", wb.get_sheet_names()
-
- sheetnames = wb.get_sheet_names()
- ws = wb.get_sheet_by_name(sheetnames[0])
-
- print "Work Sheet Titile:",ws.title
- print "Work Sheet Rows:",ws.get_highest_row()
- print "Work Sheet Cols:",ws.get_highest_column()
-
- data_dic = {}
-
- for rx in range(ws.get_highest_row()):
-
- temp_list = []
- pid = ws.cell(row = rx,column = 0).value
- w1 = ws.cell(row = rx,column = 1).value
- w2 = ws.cell(row = rx,column = 2).value
- w3 = ws.cell(row = rx,column = 3).value
- w4 = ws.cell(row = rx,column = 4).value
- temp_list = [w1,w2,w3,w4]
-
- data_dic[pid] = temp_list
-
- print 'Total:%d' %len(data_dic)
注意的是ws.cell()方法,支持的参数有两种,cell( coordinate=None , row=None , column=None)
coordinate坐标,eg ws.cell("B1")
row 和 column 是行和列 ,都是从0开始
还有,如果想取得格里的值,得用ws.cell("A1").value 取到,如果用过xlrd,因为写法差不多,可能就会忘记加value了。
III.写入excel2007
- 写excel2007
-
-
-
-
- import MySQLdb
- import time
- import sys
-
- from openpyxl.workbook import Workbook
-
- from openpyxl.writer.excel import ExcelWriter
-
- from openpyxl.cell import get_column_letter
-
-
-
-
- wb = Workbook()
-
- ew = ExcelWriter(workbook = wb)
-
-
- dest_filename = r'empty_book.xlsx'
-
-
- ws = wb.worksheets[0 ]
-
-
- ws.title = "range names"
-
-
-
- i=1
- table = {}
- for record in records:
- for x in range( 1 ,len(record)+ 1 ):
- col = get_column_letter(x)
- ws.cell('%s%s' %(col, i)).value = '%s' % (record[x- 1 ])
-
- i+=1
-
-
- ws = wb.create_sheet()
-
- ws.title = 'Pi'
-
- ws.cell('F5' ).value = 3.14
-
-
-
- ew.save(filename = dest_filename)
- 写excel2007
-
- import MySQLdb
- import time
- import sys
-
- from openpyxl.workbook import Workbook
-
- from openpyxl.writer.excel import ExcelWriter
-
- from openpyxl.cell import get_column_letter
-
- wb = Workbook()
-
- ew = ExcelWriter(workbook = wb)
-
- dest_filename = r'empty_book.xlsx'
-
- ws = wb.worksheets[0]
-
- ws.title = "range names"
-
- i=1
- table = {}
- for record in records:
- for x in range(1,len(record)+1):
- col = get_column_letter(x)
- ws.cell('%s%s'%(col, i)).value = '%s' % (record[x-1])
-
- i+=1
-
- ws = wb.create_sheet()
- ws.title = 'Pi'
- ws.cell('F5').value = 3.14
-
-
- ew.save(filename = dest_filename)
注意的地方:
# col是用列号x为参数,调用了这个模块的get_column_letter方法算出来的字母,这个比较蛋疼。
col = get_column_letter(x)
#在为数据格赋值的时候,注意写的格式:要不会有各种不靠谱的问题出现(这个是用坐标的方式写的,其实用row ,col的方式可能没那么麻烦)
ws.cell( '%s%s'%(col, i) ).value = '%s' % (record[x-1])
关于该模块的API 可以查询官方文档 http://packages.python.org/openpyxl/api.html
总体来说,这个模块还是挺方便的,但是问题就是在对于python的版本有一定要求,如果在centOs上用,可能会有些问题。
本文只是简单的写了下使用的方法,之后格式超链接神马的之后再填上~