Python中一般使用xlrd庫(kù)來讀取Excel文件,使用xlwt庫(kù)來生成Excel文件,使用xlutils庫(kù)復(fù)制和修改Excel文件。這三個(gè)庫(kù)只支持到Excel2003。
python-excel主頁(yè)地址:http://www.python-excel.org/
地址:https://pypi.python.org/pypi/xlrd
github地址:https://github.com/python-excel/xlrd
打開excel文件,獲取一個(gè)Book()對(duì)象:
import xlrdbook = xlrd.open_workbook("myfile.xls")獲取sheets數(shù)目:
>>> book.nsheets3
獲取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返回一個(gè)sheet列表sheet = book.sheet_by_index(0) #通過索引順序獲取sheet = book.sheet_by_name(u'Sheet1')#通過名稱獲取
獲取行數(shù),列數(shù),名字:
>>> sheet.nrows1002>>> sheet.ncols11>>> sheet.nameu'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是不能對(duì)其進(jìn)行操作的:xlrd.open_workbook()方法返回xlrd.Book類型,是只讀的,不能對(duì)其進(jìn)行操作。
地址: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
創(chuàng)建一個(gè)Excel文件并創(chuàng)建一個(gè)Sheet:
from xlwt import *book = Workbook()sheet = book.add_sheet('Sheet1')book.save('myExcel.xls')Workbook類可以有encoding和style_comPRession參數(shù)。
encoding,設(shè)置字符編碼,style_compression,表示是否壓縮。這樣設(shè)置:w = Workbook(encoding='utf-8'),就可以在excel中輸出中文了。默認(rèn)是ascii。
向sheet寫入內(nèi)容:
sheet.write(r, c, label="", style=Style.default_style)
簡(jiǎn)單寫入:
sheet.write(0, 0, label = 'Row 0, Column 0 Value')
設(shè)置格式寫入:
font = xlwt.Font() # 字體font.name = 'Times New Roman'font.bold = Truefont.underline = Truefont.italic = Truestyle = xlwt.XFStyle() # 創(chuàng)建一個(gè)格式style.font = font # 設(shè)置格式字體sheet.write(1, 0, label = 'Formatted value', style) # Apply the Style to the Cellbook.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.0sheet.write(0, 0, datetime.datetime.now(), style)
寫入公式:
sheet.write(0, 0, 5) # Outputs 5sheet.write(0, 1, 2) # Outputs 2sheet.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地址:http://pythonhosted.org/xlutils/
github地址:https://github.com/python-excel/xlutils
xlutils.copy.copy(wb)
復(fù)制一個(gè)xlrd.Book對(duì)象,生成一個(gè)xlwt.Workbook對(duì)象,可以對(duì)xlwt.Workbook進(jìn)行修改。
from xlrd import open_workbookfrom xlutils.copy import copybook = open_workbook('myExcel.xls')wbook = copy(book) #wbook即為xlwt.WorkBook對(duì)象wsheet = wbook.get_sheet(0) #通過get_sheet()獲取的sheet有write()方法wsheet.write(0, 0, 'value')wb.save('myExcel.xls')
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注