1、安裝 openpyxl:
zds@ubuntu ~ $ sudo pip3 install openpyxl
The directory '/home/zds/.cache/pip/http' or its parent directory ...
The directory '/home/zds/.cache/pip' or its parent directory ...
Collecting openpyxl
Downloading openpyxl-2.5.0.tar.gz (169kB)
100% |████████████████████████████████| 174kB 28kB/s
Collecting jdcal (from openpyxl)
Downloading jdcal-1.3.tar.gz
Collecting et_xmlfile (from openpyxl)
Downloading et_xmlfile-1.0.1.tar.gz
Installing collected packages: jdcal, et-xmlfile, openpyxl
Running setup.py install for jdcal ... done
Running setup.py install for et-xmlfile ... done
Running setup.py install for openpyxl ... done
Successfully installed et-xmlfile-1.0.1 jdcal-1.3 openpyxl-2.5.0
zds@ubuntu ~ $
2泻肯、在 shell 里使用 openpyxl 的 Workbook
方法創(chuàng)建一個工作簿,即一個 .xlsx
文件:
zds@ubuntu ~ $ ipython
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from openpyxl import Workbook
In [2]: wb = Workbook() # wb 就是一個工作簿
# 創(chuàng)建工作簿時自動生成一張工作表并運行此工作表
In [3]: wb.active # 當前運行的工作表
Out[3]: <Worksheet "Sheet">
In [4]: ws = wb.active
In [5]: ws
Out[5]: <Worksheet "Sheet">
3、使用 create_sheet
方法創(chuàng)建工作表:
In [6]: ws1 = wb.create_sheet() # 默認新建工作表插入到工作簿末尾
# 設(shè)置新建工作表插入到工作簿第一個位置疲吸,表名為 Kitty
In [7]: ws2 = wb.create_sheet(index=0, title='Kitty')
# 在創(chuàng)建工作表的時候系統(tǒng)自動命名(Sheet,Sheet1前鹅,Sheet2 ...)
# 用 title 方法可以修改工作表的名字
In [8]: ws2.title = 'Kobe'
In [9]: ws2 is wb['Kobe'] # 通過工作簿的 key 可以獲得工作表
Out[9]: True
In [10]: wb.sheetnames # 打印所有工作表的名字
Out[10]: ['Kobe', 'Sheet', 'Sheet1']
In [11]: ws2 is wb[wb.sheetnames[2]]
Out[11]: True
4摘悴、操作數(shù)據(jù)
# 新建的工作表里是沒有單元格的
In [42]: ws['A1'] # 這一步就創(chuàng)建了一個新的單元格到內(nèi)存
Out[42]: <Cell 'Sheet'.A1>
In [43]: ws.cell(row=1, column=1) # 同上
Out[43]: <Cell 'Sheet'.A1>
In [44]: c = ws['A1']
In [45]: ws['A1'] = 'hello' # 向單元格里填入數(shù)據(jù),賦值即修改
In [46]: c.value # 獲得單元格數(shù)據(jù)
Out[46]: 'hello'
# 注意:當一個工作表被創(chuàng)建時舰绘,其中不包含單元格蹂喻,只有當單元格被獲取時才被創(chuàng)建
# 這種方式我們不會創(chuàng)建我們從不會使用的單元格,從而減少了內(nèi)存消耗
In [47]: for i in range(1, 6): # 這步操作會在內(nèi)存中生成 20 個單元格
...: for j in range(1, 5): # 不管對它們賦值與否都生成了捂寿,占了內(nèi)存
...: ws.cell(row=i, column=j)
...:
In [48]: ws['A1': 'C3'] # 使用切片獲取多個單元格口四,若沒有則自動創(chuàng)建
Out[48]:
((<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>))
In [40]: ws.rows # 迭代器,可獲取全部單元格
Out[49]: <generator object Worksheet._cells_by_row at 0x7fc20b791f68>
In [50]: ws.columns # 同上
Out[50]: <generator object Worksheet._cells_by_col at 0x7fc20b8a4ba0>
In [17]: for i in ws.rows: # 打印全部單元格
...: print(i)
...:
(<Cell 'Sheet'.A1>, <Cell 'Sheet'.B1>, <Cell 'Sheet'.C1>, <Cell 'Sheet'.D1>)
(<Cell 'Sheet'.A2>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.C2>, <Cell 'Sheet'.D2>)
(<Cell 'Sheet'.A3>, <Cell 'Sheet'.B3>, <Cell 'Sheet'.C3>, <Cell 'Sheet'.D3>)
(<Cell 'Sheet'.A4>, <Cell 'Sheet'.B4>, <Cell 'Sheet'.C4>, <Cell 'Sheet'.D4>)
(<Cell 'Sheet'.A5>, <Cell 'Sheet'.B5>, <Cell 'Sheet'.C5>, <Cell 'Sheet'.D5>)
In [18]: l = list('abcd')
In [19]: ws.append(l) # 此方法可以添加一行數(shù)據(jù)秦陋,注意參數(shù)為列表或元組
In [20]: for i in ws.values: # 此屬性可獲得全部單元格的值蔓彩,格式為列表,列表元素為元組
...: print(i)
...:
('hello', None, None, None)
(None, None, None, None)
(None, None, None, None)
(None, None, None, None)
(None, None, None, None)
('a', 'b', 'c', 'd')
5驳概、保存文件
# 將工作簿保存到本地赤嚼,參數(shù)為文件名
In [19]: wb.save('test.xlsx')
6、從文件中導入數(shù)據(jù)
In [29]: from openpyxl import load_workbook
In [30]: wbl = load_workbook('test.xlsx')
In [31]: wbl.sheetnames
Out[31]: ['Sheet', 'Sheet1', 'Kobe']