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類型,是只讀的,不能對其進行操作。
xlrd讀取Excel中的日期:
一個值為1984/5/30的單元格
>>> print sheet.cell(2,7) #直接讀取是個日期格式,讀value會返回30832.0的值 xldate:30832.0 >>> xlrd.xldate_as_tuple(sheet.cell(2,7).value, 0) #變為元祖,第二個參數有兩種取值,0或者1,0是以1900-01-01為基准的日期,而1是1904-01-01為基准的日期。 (1984, 5, 30, 0, 0, 0) >>> xlrd.xldate.xldate_as_datetime(sheet.cell(2,7).value, 1) datetime.datetime(1988, 5, 31, 0, 0) >>> xlrd.xldate.xldate_as_datetime(sheet.cell(2,7).value, 0) datetime.datetime(1984, 5, 30, 0, 0)
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')