有一萬年沒來這兒寫blog了,今天等著發(fā)版,抽空來寫點東西吧。
python有很多模塊都是用來操作excel的,比如xlrd,xlwt,pyExcelerator。用著很方便,但是問題是,只能支持到
excel2003。雖然一般的應(yīng)用其實足夠了,但是如果遇到了導(dǎo)出大量數(shù)據(jù)(超過65535條)的需求時,excel2003就不夠用了。所以我就只好
去找一個能支持excel2007的模塊。
google了一下,發(fā)現(xiàn)了這個openpyxl,不過網(wǎng)上也沒什么中文的文檔可以看,于是就自己琢磨琢磨。
I.安裝
openpyxl的最新版本是1.5.0 下載地址 http://pypi./pypi/openpyxl/1.5.0
需求python的版本是2.6+ ,也就是說,如果centOS系統(tǒng)的機器想用,那得升級系統(tǒng)的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()方法,支持的參數(shù)有兩種,cell(
coordinate=None
, row=None
, column=None)
coordinate坐標(biāo),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為參數(shù),調(diào)用了這個模塊的get_column_letter方法算出來的字母,這個比較蛋疼。
col = get_column_letter(x)
#在為數(shù)據(jù)格賦值的時候,注意寫的格式:要不會有各種不靠譜的問題出現(xiàn)(這個是用坐標(biāo)的方式寫的,其實用row ,col的方式可能沒那么麻煩)
ws.cell(
'%s%s'%(col, i)
).value =
'%s' % (record[x-1])
關(guān)于該模塊的API 可以查詢官方文檔
http://packages./openpyxl/api.html
總體來說,這個模塊還是挺方便的,但是問題就是在對于python的版本有一定要求,如果在centOs上用,可能會有些問題。
本文只是簡單的寫了下使用的方法,之后格式超鏈接神馬的之后再填上~
來自:http://blog.csdn.net/suofiya2008/article/details/6284208