原文鏈接
開始在內(nèi)存中使用
創(chuàng)建一個workbook
在剛開始使用openpyxl的時候,不需要直接在文件系統(tǒng)中創(chuàng)建一個文件蒲列,僅僅需要導(dǎo)入Workbook類并開始使用它:
>>> from openpyxl import Workbook
>>> wb = Workbook()
一個workbook總是會創(chuàng)建至少一個worksheet(工作表)膜钓,可以通過openpyxl.workbook.Workbook.active()這個屬性去獲裙低弧:
>>> ws = wb.active
這個函數(shù)使用_active_sheet_index這個屬性揩悄,默認(rèn)設(shè)置的值是0糜工,除非你指定一個值弊添,否則總是獲取到第一個worksheet。
你可以使用openpyxl.workbook.Workbook.create_sheet()來創(chuàng)建一個新的worksheet:
>>> ws1 = wb.create_sheet("Mysheet") # insert at the end (default)# or
>>> ws2 = wb.create_sheet("Mysheet", 0) # insert at first position
當(dāng)創(chuàng)建腳標(biāo)的時候會自動創(chuàng)建一個名字捌木,按照(Sheet, Sheet1, Sheet2, ...)這個列表名創(chuàng)建油坝,你可以使用tiitle屬性來修改這個名字:
>>> ws.title = "New Title"
一旦給了一個worksheet名字,就可以通過一個key去獲取這個worksheet:
>>> ws3 = wb["New Title"]
你可以使用openpyxl.workbook.Workbook.sheetnames()這個屬性獲取所有的腳標(biāo)的名字:
>>> print(wb.sheetnames)['Sheet2', 'New Title', 'Sheet1']
可以迭代所有的腳標(biāo):
>>> for sheet in wb:
... print(sheet.title)
可以使用openpyxl.workbook.Workbook.copy_worksheet()這個屬性復(fù)制一個worksheet:
>>> source = wb.active
>>> target = wb.copy_worksheet(source)
注意:只有cells 和 styles能夠被復(fù)制刨裆,不能在workbooks之間復(fù)制worksheets澈圈,你可以在一個workbook中復(fù)制worksheets
玩數(shù)據(jù)
獲取一個cell
現(xiàn)在我們已經(jīng)知道怎么訪問一個worksheet,我們可以開始修改cell的內(nèi)容了帆啃。(一個cell就是一個單元格)
cell可以直接通過key來獲人才:
>>> c = ws['A4']
這將會返回一個cell或創(chuàng)建一個不存在的cell。cell 的值可以直接被賦值:
>>> ws['A4'] = 4
也可以使用另外一個方法openpyxl.worksheet.Worksheet.cell():
>>> d = ws.cell(row=4, column=2, value=10)
Note:當(dāng)在內(nèi)存當(dāng)中創(chuàng)建一個worksheet的時候努潘,它沒有包含任何cell诽偷,當(dāng)它們第一次被訪問的時候被創(chuàng)建
Warning:因為excel表的滾動特性,滾動出來的cell也會被創(chuàng)建出來疯坤,即使沒有訪問那些cell报慕,例如:
>>> for i in range(1,101):
... for j in range(1,101):
... ws.cell(row=i, column=j)
這將會創(chuàng)建100*100個空的cell
訪問多個cell
使用切片可以訪問多個cell
>>> cell_range = ws['A1':'C2']
行和列能夠被輕松的獲取到:
>>> colC = ws['C']
>>> col_range = ws['C:D']
>>> row10 = ws[10]
>>> row_range = ws[5:10]
也可以使用openpyxl.worksheet.Worksheet.iter_rows()這個方法:
>>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2):
... for cell in row:
... print(cell)
<Cell Sheet1.A1>
<Cell Sheet1.B1>
<Cell Sheet1.C1>
<Cell Sheet1.A2>
<Cell Sheet1.B2>
<Cell Sheet1.C2>
相似的方法openpyxl.worksheet.Worksheet.iter_cols()也可以:
>>> for col in ws.iter_cols(min_row=1, max_col=3, max_row=2):
... for cell in col:
... print(cell)
<Cell Sheet1.A1>
<Cell Sheet1.A2>
<Cell Sheet1.B1>
<Cell Sheet1.B2>
<Cell Sheet1.C1>
<Cell Sheet1.C2>
如果你想迭代一個文件的所有行或列,可以使用openpyxl.worksheet.Worksheet.rows()這個屬性:
>>> ws = wb.active
>>> ws['C9'] = 'hello world'
>>> tuple(ws.rows)
((<Cell Sheet.A1>, <Cell Sheet.B1>, <Cell Sheet.C1>),
(<Cell Sheet.A2>, <Cell Sheet.B2>, <Cell Sheet.C2>),
(<Cell Sheet.A3>, <Cell Sheet.B3>, <Cell Sheet.C3>),
(<Cell Sheet.A4>, <Cell Sheet.B4>, <Cell Sheet.C4>),
(<Cell Sheet.A5>, <Cell Sheet.B5>, <Cell Sheet.C5>),
(<Cell Sheet.A6>, <Cell Sheet.B6>, <Cell Sheet.C6>),
(<Cell Sheet.A7>, <Cell Sheet.B7>, <Cell Sheet.C7>),
(<Cell Sheet.A8>, <Cell Sheet.B8>, <Cell Sheet.C8>),
(<Cell Sheet.A9>, <Cell Sheet.B9>, <Cell Sheet.C9>))
或者openpyxl.worksheet.Worksheet.columns()這個屬性:
>>> tuple(ws.columns)
((<Cell Sheet.A1>,
<Cell Sheet.A2>,
<Cell Sheet.A3>,
<Cell Sheet.A4>,
<Cell Sheet.A5>,
<Cell Sheet.A6>,
...
<Cell Sheet.B7>,
<Cell Sheet.B8>,
<Cell Sheet.B9>),
(<Cell Sheet.C1>,
<Cell Sheet.C2>,
<Cell Sheet.C3>,
<Cell Sheet.C4>,
<Cell Sheet.C5>,
<Cell Sheet.C6>,
<Cell Sheet.C7>,
<Cell Sheet.C8>,
<Cell Sheet.C9>))
數(shù)據(jù)存儲
一旦我們有了一個openpyxl.cell.Cell压怠,我們就可以給它賦值:
>>> c.value = 'hello, world'
>>> print(c.value)'hello, world'
>>> d.value = 3.14
>>> print(d.value)3.14
也能使用類型和格式推斷:
>>> wb = Workbook(guess_types=True)
>>> c.value = '12%'
>>> print(c.value)
0.12
>>> import datetime
>>> d.value = datetime.datetime.now()
>>> print d.valuedatetime.datetime(2010, 9, 10, 22, 25, 18)
>>> c.value = '31.50'
>>> print(c.value)
31.5
保存到文件
最簡單和快速的保存一個workbook方法是使用openpyxl.workbook.Workbook模塊的openpyxl.workbook.Workbook.save()這個方法:
>>> wb = Workbook()
>>> wb.save('balances.xlsx')
Warning:這個方法將會在沒有警告提示下覆蓋已經(jīng)有的內(nèi)容
可以使用template=True將一個workbook保存成一個模版:
>>> wb = load_workbook('document.xlsx')
>>> wb.template = True
>>> wb.save('document_template.xltx')
或者設(shè)置這個屬性為false(默認(rèn))來保存為一個文件:
>>> wb = load_workbook('document_template.xltx')
>>> wb.template = False
>>> wb.save('document.xlsx', as_template=False)
*Warning:當(dāng)保存文檔的時候在模版文檔中你應(yīng)該注意文檔的擴(kuò)展名(后綴名)和數(shù)據(jù)描述眠冈,否則可能會導(dǎo)致文檔不能被再次打開,如下錯誤式例:
>>> wb = load_workbook('document.xlsx')
>>> # 應(yīng)該保存成擴(kuò)展名為*.xlsx
>>> wb.save('new_document.xlsm')
>>> # Excel軟件不能再次打開此文件
>>>
>>> # 或者
>>>
>>> # 應(yīng)該指定屬性keep_vba=True
>>> wb = load_workbook('document.xlsm')
>>> wb.save('new_document.xlsm')
>>> # Excel軟件不能再次打開此文件
>>>
>>> # 或者
>>>
>>> wb = load_workbook('document.xltm', keep_vba=True)
>>> # 如果我們需要一個模版文件菌瘫,就必須指定擴(kuò)展名為 *.xltm.
>>> wb.save('new_document.xlsm')
>>> # Excel軟件不能再次打開此文件
加載一個文件
類似于寫文件蜗顽,可以導(dǎo)入openpyxl.load_workbook()來打開一個已經(jīng)存在的workbook:
>>> from openpyxl import load_workbook
>>> wb2 = load_workbook('test.xlsx')
>>> print wb2.get_sheet_names()
['Sheet2', 'New Title', 'Sheet1']
基本教程已經(jīng)完了。接下來是一些使用例子:
寫一個workbook
>>> from openpyxl import Workbook
>>> from openpyxl.compat import range
>>> from openpyxl.utils import get_column_letter
>>>
>>> wb = Workbook()
>>>
>>> dest_filename = 'empty_book.xlsx'
>>>
>>> ws1 = wb.active
>>> ws1.title = "range names"
>>>
>>> for row in range(1, 40):
... ws1.append(range(600))
>>>
>>> ws2 = wb.create_sheet(title="Pi")
>>>
>>> ws2['F5'] = 3.14
>>>
>>> ws3 = wb.create_sheet(title="Data")
>>> for row in range(10, 20):
... for col in range(27, 54):
... _ = ws3.cell(column=col, row=row, value="{0}".format(get_column_letter(col)))
>>> print(ws3['AA10'].value)
AA
>>> wb.save(filename = dest_filename)
讀取一個已經(jīng)存在的文件
>>> from openpyxl import load_workbook
>>> wb = load_workbook(filename = 'empty_book.xlsx')
>>> sheet_ranges = wb['range names']
>>> print(sheet_ranges['D18'].value)
警告:openpyxl不能讀取Excle中所有的對象雨让,當(dāng)打開和保存相同名字的文件的時候雇盖,圖片和圖表將會丟失
使用數(shù)字格式:
>>> import datetime
>>> from openpyxl import Workbook
>>> wb = Workbook()
>>> ws = wb.active
>>> # set date using a Python datetime
>>> ws['A1'] = datetime.datetime(2010, 7, 21)
>>>
>>> ws['A1'].number_format
'yyyy-mm-dd h:mm:ss'
>>> # You can enable type inference on a case-by-case basis
>>> wb.guess_types = True
>>> # set percentage using a string followed by the percent sign
>>> ws['B1'] = '3.14%'
>>> wb.guess_types = False
>>> ws['B1'].value
0.031400000000000004
>>>
>>> ws['B1'].number_format
'0%'
使用公式:
>>> from openpyxl import Workbook
>>> wb = Workbook()
>>> ws = wb.active
>>> # add a simple formula
>>> ws["A1"] = "=SUM(1, 1)"
>>> wb.save("formula.xlsx")
警告:公式必須使用英文名,并且公式的參數(shù)必須使用逗號分隔宫患,不能使用其他的符號如分號
openpyxl從不評估公式刊懈,但是可以檢查公式的名字:
>>> from openpyxl.utils import FORMULAE
>>> "HEX2DEC" in FORMULAE
True
如果你想使用一個不知道的公式,這可能是因為你使用的公式娃闲,沒有包括在初始規(guī)范虚汛。 這樣的公式必須以xlfn作為前綴。
合并/取消合并單元格:
>>> from openpyxl.workbook import Workbook
>>>
>>> wb = Workbook()
>>> ws = wb.active
>>>
>>> ws.merge_cells('A1:B1')
>>> ws.unmerge_cells('A1:B1')
>>>
>>> # or
>>> ws.merge_cells(start_row=2,start_column=1,end_row=2,end_column=4)
>>> ws.unmerge_cells(start_row=2,start_column=1,end_row=2,end_column=4)
插入圖片:
>>> from openpyxl import Workbook
>>> from openpyxl.drawing.image import Image
>>>
>>> wb = Workbook()
>>> ws = wb.active
>>> ws['A1'] = 'You should see three logos below'
>>> # create an image
>>> img = Image('logo.png')
>>> # add to worksheet and anchor next to cells
>>> ws.add_image(img, 'A1')
>>> wb.save('logo.xlsx')
折疊列:
>>> import openpyxl
>>> wb = openpyxl.Workbook()
>>> ws = wb.create_sheet()
>>> ws.column_dimensions.group('A','D', hidden=True)
>>> wb.save('group.xlsx')
使用Pandas 和 NumPy
openpyxl可以配合使用Pandas 和 NumPy這兩個很受歡迎的庫
NumPy Support
openpyxl已內(nèi)置支持NumPy類型float皇帮,integer和boolean卷哩。 DateTimes支持使用Pandas的時間戳類型。
openpyxl.utils.dataframe.dataframe_to_rows()方法提供簡單的方式使用Pandas 的Dataframes:
from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
wb = Workbook()
ws = wb.active
for r in dataframe_to_rows(df, index=True, header=True):
ws.append(r)
要將數(shù)據(jù)框轉(zhuǎn)換為突出顯示的標(biāo)題和索引:
wb = Workbook()
ws = wb.active
for r in dataframe_to_rows(df, index=True, header=True):
ws.append(r)
for cell in ws['A'] + ws[1]:
cell.style = 'Pandas'
wb.save("pandas_openpyxl.xlsx")
如果你只是想轉(zhuǎn)換數(shù)據(jù)属拾,可以使用只寫模式:
from openpyxl.cell.cell import WriteOnlyCell
wb = Workbook(write_only=True)
ws = wb.create_sheet()
cell = WriteOnlyCell(ws)
cell.style = 'Pandas'
def format_first_row(row, cell):
for c in row:
cell.value = c
yield cell
rows = dataframe_to_rows(df)
first_row = format_first_row(next(rows), cell)
ws.append(first_row)
for row in rows:
row = list(row)
cell.value = row[0]
row[0] = cell
ws.append(row)
wb.save("openpyxl_stream.xlsx")
將工作表轉(zhuǎn)換為Dataframe
要將工作表轉(zhuǎn)換為Dataframe将谊,您可以使用values屬性。 如果工作表沒有標(biāo)題或索引渐白,這很容易:
df = DataFrame(ws.values)
如果工作表有標(biāo)題或索引尊浓,例如Pandas創(chuàng)建的那個,那么需要做更多的工作:
data = ws.values
cols = next(data)[1:]
data = list(data)
idx = [r[0] for r in data]
data = (islice(r, 1, None) for r in data)
df = DataFrame(data, index=idx, columns=cols)
使用過濾和排序
要添加過濾器纯衍,請定義范圍栋齿,然后添加列和排序條件:
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
data = [
["Fruit", "Quantity"],
["Kiwi", 3],
["Grape", 15],
["Apple", 3],
["Peach", 3],
["Pomegranate", 3],
["Pear", 3],
["Tangerine", 3],
["Blueberry", 3],
["Mango", 3],
["Watermelon", 3],
["Blackberry", 3],
["Orange", 3],
["Raspberry", 3],
["Banana", 3]
]
for r in data:
ws.append(r)
ws.auto_filter.ref = "A1:B15"
ws.auto_filter.add_filter_column(0, ["Kiwi", "Apple", "Mango"])
ws.auto_filter.add_sort_condition("B2:B15")
wb.save("filtered.xlsx")
這將添加相關(guān)的指令到文件,但不會實際過濾或排序襟诸。
注意:文章翻譯了一部分openpyxl的官方文檔瓦堵,還有部分內(nèi)容暫時沒有翻譯。