浅谈Python
# 通过切片
>>> cell_range
=
ws[
'A1'
:
'C2'
]
# 通过行(列)
>>> colC
=
ws[
'C'
]
>>> col_range
=
ws[
'C:D'
]
>>> row10
=
ws[
10
]
>>> row_range
=
ws[
5
:
10
]
# 通过指定范围(行 → 行)
>>>
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>
# 通过指定范围(列 → 列)
>>>
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>
# 遍历所有 方法一
>>> 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.A8>, <Cell Sheet.B8>, <Cell Sheet.C8>),
(<Cell Sheet.A9>, <Cell Sheet.B9>, <Cell Sheet.C9>))
# 遍历所有 方法二
>>>
tuple
(ws.columns)
((<Cell Sheet.A1>,
<Cell Sheet.A2>,
<Cell Sheet.A3>,
...
<Cell Sheet.B7>,
<Cell Sheet.B8>,
<Cell Sheet.B9>),
(<Cell Sheet.C1>,
...
<Cell Sheet.C8>,
<Cell Sheet.C9>))
赞 (0)