Python 读写 Excel 表格

openpyxl

Posted by Randle on January 18, 2020

openpyxl-doc

openpyxlpython 中用来读写 excel 的包,操作的是新版 excel 文件,即后缀名为 xlsx的文件。

每个excel文件抽象为一个Workbook,每个工作表抽象为一个sheet对象,每个单元格抽象为一个cell对象,

安装:

1
pip install openpyxl

Workbook

加载Workbook:

1
2
3
4
>>> from openpyxl import load_workbook
>>> wb2 = load_workbook('test.xlsx')
>>> print wb2.sheetnames
['Sheet2', 'New Title', 'Sheet1']

新建Workbook:

1
2
>>> from openpyxl import Workbook
>>> wb = Workbook()

保存Workbook:

1
2
wb = Workbook()
wb.save('balances.xlsx')

关闭文件引用:

1
2
wb = Workbook()
wb.close()

Sheet

获得活动sheet:

1
>>> ws = wb.active

创建sheet:

1
2
3
4
5
6
7
8
# insert at the end (default)
>>> ws1 = wb.create_sheet("Mysheet") 

# insert at first position
>>> ws2 = wb.create_sheet("Mysheet", 0) 

# insert at the penultimate position
>>> ws3 = wb.create_sheet("Mysheet", -1) 

表单名:

1
ws.title = "New Title"

访问sheet:

1
2
3
4
5
6
7
>>> ws3 = wb["New Title"]

>>> print(wb.sheetnames)
['Sheet2', 'New Title', 'Sheet1']

>>> for sheet in wb:
...     print(sheet.title)

复制sheet:

1
2
source = wb.active
target = wb.copy_worksheet(source)

Cell

访问单个单元格

1
2
3
c = ws['A4']
ws['A4'] = 4
d = ws.cell(row=4, column=2, value=10)

cell(row,colum)访问行和列的方式,行和列从1开始。

1
2
3
4
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.cell(row, column, value)

可以按照这种方式为单元格赋值。

访问范围内单元格

1
2
3
4
5
cell_range = ws['A1':'C2']
colC = ws['C']
col_range = ws['C:D']
row10 = ws[10]
row_range = ws[5:10]

也可以通过行或列迭代的方式:

1
2
3
4
5
6
7
8
9
>>> 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>
1
2
3
4
5
6
7
8
9
>>> 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>

或者直接通过rows属性,或者columns属性。

1
2
3
4
5
6
7
8
9
10
11
12
>>> 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>))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
>>> 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>))

只看value

可以使用Worksheet.values方法实现只看sheet里面的value

1
2
3
for row in ws.values:
   for value in row:
     print(value)

Worksheet.iter_rows()Worksheet.iter_cols() 方法可以使用参数 values_only 只返回 value

1
2
3
4
5
6
for row in ws.iter_rows(min_row=1, max_col=3, \
		max_row=2, values_only=True):
    print(row)
---
(None, None, None)
(None, None, None)

数据存储

获得 Cell 对象以后,可以这样为其赋值:

1
2
3
4
5
6
7
>>> c.value = 'hello, world'
>>> print(c.value)
'hello, world'

>>> d.value = 3.14
>>> print(d.value)
3.14