http://www.jianshu.com/p/642456aa93e2
最近在网上爬取奥运项目资料,并写入Excel中。在写到Excel中是用到了OpenPyXL,翻译了一部分自己用到的知识点。
>>> from openpyxl import Workbook
>>> wb = Workbook()
>>> ws = wb.active
注意:该函数调用工作表的索引(_active_sheet_index),默认是0。除非你修改了这个值,否则你使用该函数一直是在对第一张工作表进行操作。>>> ws1 = wb.create_sheet() #默认插在工作簿末尾
# or
>>> ws2 = wb.create_sheet(0) # 插入在工作簿的第一个位置
ws.title = "New Title"
ws.sheet_properties.tabColor = "1072BA"
>>> ws3 = wb["New Title"]
>>> ws4 = wb.get_sheet_by_name("New Title")
>>> ws is ws3 is ws4
True
>>> print(wb.get_sheet_names())
['Sheet2', 'New Title', 'Sheet1']
你也可以循环得到所有的工作表 >>> for sheet in wb:
print(sheet.title)
现在我们已经知道如何使用一张工作表了,下面我我们开始修改工作表中单元格的内容
>>> c = ws['A4']
通过上述的语句,将返回在A4处的单元格,如果不存在将在A4新建一个。 单元格的值也可以直接赋值 >>> ws['A4'] = 4
>>> c = ws.cell('A4')
>>> d = ws.cell(row = 4, column = 2)
注意:当一个工作表被创建是,其中不包含单元格。只有当单元格被获取是才被创建。这种方式我们不会创建我们从不会使用的单元格,从而减少了内存消耗。警告:由于上述特性,你如果遍历了单元格而非想要使用它们也将会在内存当中创建。比如下面:
>>> for i in range(1,101):
for j in range(1,101):
ws.cell(row = i, column = j)
上述代码将会在内存中创建100*100个单元格。
当然,这里也有方法来清理这些不想要的单元格,在后续我们将会介绍。
>>> cell_range = ws['A1':'C2']
>>> tuple(ws.iter_rows('A1:C2'))
(( | , | , | ),
( | , | , | ))
>>> for row in ws.iter_rows('A1:C2'):
for cell in row:
print cell
|
|
|
|
|
|
>>> ws = wb.active
>>> ws['C9'] = 'hello world'
>>> 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()方法 >>> 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>))
>>> 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.value
datetime.datetime(2010, 9, 10, 22, 25, 18)
>>> c.value = '31.50'
>>> print(c.value)
31.5
>>> wb = Workbook()
>>> wb.save('balances.xlsx')
!特别警告:这个操作将会在没有认识提示的情况下用现在写的内容,覆盖掉原文件中的所有内容>>> wb = load_workbook('document.xlsx')
>>> wb.save('document_template.xltx', as_template=True)
>>> wb = load_workbook('document_template.xltx')
>>> wb.save('document.xlsx', as_template=False)
>>> wb = load_workbook('document.xlsx')
>>> wb.save('new_document.xlsx', as_template=False)
警告:在保存文件到文件模板中的时候你应该监控数据的属性和文件扩展名,反之亦然;否则,你得到的工作簿可能无法打开。>>> wb = load_workbook('document.xlsx')
>>> # Need to save with the extension *.xlsx
>>> wb.save('new_document.xlsm')
>>> # MS Excel can't open the document
>>>
>>> # or
>>>
>>> # Need specify attribute keep_vba=True
>>> wb = load_workbook('document.xlsm')
>>> wb.save('new_document.xlsm')
>>> # MS Excel can't open the document
>>>
>>> # or
>>>
>>> wb = load_workbook('document.xltm', keep_vba=True)
>>> # If us need template document, then we need specify extension as *.xltm.
>>> # If us need document, then we need specify attribute as_template=False.
>>> wb.save('new_document.xlsm', as_template=True)
>>> # MS Excel can't open the document
>>> from openpyxl import load_workbook
>>> wb2 = load_workbook('test.xlsx')
>>> print wb2.get_sheet_names()
['Sheet2', 'New Title', 'Sheet1']
Manipulating a workbook in memory