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
方法创建一个工作簿,即一个 .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 就是一个工作簿
# 创建工作簿时自动生成一张工作表并运行此工作表
In [3]: wb.active # 当前运行的工作表
Out[3]: <Worksheet "Sheet">
In [4]: ws = wb.active
In [5]: ws
Out[5]: <Worksheet "Sheet">
3、使用 create_sheet
方法创建工作表:
In [6]: ws1 = wb.create_sheet() # 默认新建工作表插入到工作簿末尾
# 设置新建工作表插入到工作簿第一个位置,表名为 Kitty
In [7]: ws2 = wb.create_sheet(index=0, title='Kitty')
# 在创建工作表的时候系统自动命名(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、操作数据
# 新建的工作表里是没有单元格的
In [42]: ws['A1'] # 这一步就创建了一个新的单元格到内存
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' # 向单元格里填入数据,赋值即修改
In [46]: c.value # 获得单元格数据
Out[46]: 'hello'
# 注意:当一个工作表被创建时,其中不包含单元格,只有当单元格被获取时才被创建
# 这种方式我们不会创建我们从不会使用的单元格,从而减少了内存消耗
In [47]: for i in range(1, 6): # 这步操作会在内存中生成 20 个单元格
...: for j in range(1, 5): # 不管对它们赋值与否都生成了,占了内存
...: ws.cell(row=i, column=j)
...:
In [48]: ws['A1': 'C3'] # 使用切片获取多个单元格,若没有则自动创建
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) # 此方法可以添加一行数据,注意参数为列表或元组
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、保存文件
# 将工作簿保存到本地,参数为文件名
In [19]: wb.save('test.xlsx')
6、从文件中导入数据
In [29]: from openpyxl import load_workbook
In [30]: wbl = load_workbook('test.xlsx')
In [31]: wbl.sheetnames
Out[31]: ['Sheet', 'Sheet1', 'Kobe']
网友评论