轉:https://www.cnblogs.com/crazymagic/articles/9752287.html
Python操控Excel之讀取
我們在python中引入openpyxl模塊來操控excel文件。一個以.xlsx為擴張名的excel文件打開后叫工作簿workbook,每個工作簿可以包括多張表單worksheet,正在操作的這張表單被認為是活躍的active sheet。每張表單有行和列,行號1、2、3…,列號A、B、C...。在某一個特定行和特定列的小格子叫單元格cell。
python程序從excel文件中讀數據基本遵循以下步驟:
1、import openpyxl
2、調用openpyxl模塊下的load_workbook(‘你的文件名.xlsx’)函數打開excel文件,得到一個工作簿(workbook)對象wb
3、通過wb.active或wb的方法函數get_sheet_by_name(‘你想要訪問的表單名稱’)得到表單對象ws
4、通過索引獲取單元格:ws[‘B2’]
通過表單的方法函數cell()獲取單元格:ws.cell(row=1, column=2)
通過單元格的屬性value,row,column,coordinate對單元格進行多方向立體式訪問
5、行對象ws[10],列對象[‘C’],行切片ws[5:10]、列切片ws[‘C:D’],單元格左上角和右下角左邊共同划定表單指定區域ws['A1':'C3']
6、ws.max_row和ws.max_column給出數據用到的最大行和列
7、from openpyxl.utils import get_column_letter, column_index_from_string引進來的兩個函數實現excel表格列字母和數字的轉換
工作薄中包可以獲取表單對象,表單對象中獲取行和列 ,行和列中獲取單元格對象
excel中內容如下:
從工作薄中獲取創建表單對象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import
openpyxl
# 打開excel文件,獲取工作簿對象
wb
=
openpyxl.load_workbook(
'example.xlsx'
)
# 從工作薄中獲取一個表單(sheet)對象
sheets
=
wb.sheetnames
print
(sheets,
type
(sheets))
# 創建一個表單
mySheet
=
wb.create_sheet(
'mySheet'
)
print
(wb.sheetnames)
# 獲取指定的表單
sheet3
=
wb.get_sheet_by_name(
'Sheet3'
)
sheet4
=
wb[
'mySheet'
]
for
sheet
in
wb:
print
(sheet.title)
|
獲取單元格對象中的信息
import
openpyxl
# 打開excel文件,獲取工作簿對象
wb
=
openpyxl.load_workbook(
'example.xlsx'
)
# 從表單中獲取單元格的內容
ws
=
wb.active
# 當前活躍的表單
print
(ws)
print
(ws[
'A1'
])
# 獲取A列的第一個對象
print
(ws[
'A1'
].value)
c
=
ws[
'B1'
]
print
(
'Row {}, Column {} is {}'
.
format
(c.row, c.column, c.value))
# 打印這個單元格對象所在的行列的數值和內容
print
(
'Cell {} is {}\n'
.
format
(c.coordinate, c.value))
# 獲取單元格對象的所在列的行數和值
|
獲取指定的單元格中 值
1
2
3
4
5
6
7
8
9
10
11
|
import
openpyxl
# 打開excel文件,獲取工作簿對象
wb
=
openpyxl.load_workbook(
'example.xlsx'
)
# 從表單中獲取單元格的內容
ws
=
wb.active
# 當前活躍的表單
print
(ws.cell(row
=
1
, column
=
2
))
# 獲取第一行第二列的單元格
print
(ws.cell(row
=
1
, column
=
2
).value)
for
i
in
range
(
1
,
8
,
2
):
# 獲取1,3,4,7 行第二列的值
print
(i, ws.cell(row
=
i, column
=
2
).value)
|
獲取指定的行和列
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import
openpyxl
# 打開excel文件,獲取工作簿對象
wb
=
openpyxl.load_workbook(
'example.xlsx'
)
# 從表單中獲取單元格的內容
ws
=
wb.active
# 當前活躍的表單
# 從表單中獲取行和列
colC
=
ws[
'C'
]
# 獲取整個C列
print
(colC)
row6
=
ws[
6
]
# 獲取第6行
print
(row6,
type
(row6))
|
遍歷行和列中單元格的值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import
openpyxl
# 打開excel文件,獲取工作簿對象
wb
=
openpyxl.load_workbook(
'example.xlsx'
)
ws
=
wb.active
# 當前活躍的表單
col_range
=
ws[
'B:C'
]
row_range
=
ws[
2
:
6
]
for
col
in
col_range:
# 打印BC兩列單元格中的值內容
for
cell
in
col:
print
(cell.value)
for
row
in
row_range:
# 打印 2-5行中所有單元格中的值
for
cell
in
row:
print
(cell.value)
for
row
in
ws.iter_rows(min_row
=
1
, max_row
=
2
, max_col
=
2
):
# 打印1-2行,1-2列中的內容
for
cell
in
row:
print
(cell.value)
|
查看行和列的總數
import
openpyxl
# 打開excel文件,獲取工作簿對象
wb
=
openpyxl.load_workbook(
'example.xlsx'
)
ws
=
wb.active
# 當前活躍的表單
print
(
'{}行 {}列'
.
format
(ws.max_row, ws.max_column))
|
數字和字母的轉換
1
2
3
4
5
6
7
8
9
10
11
12
|
import
openpyxl
from
openpyxl.utils
import
get_column_letter, column_index_from_string
# 打開excel文件,獲取工作簿對象
wb
=
openpyxl.load_workbook(
'example.xlsx'
)
ws
=
wb.active
# 當前活躍的表單
# 把數字轉換成字母
print
(get_column_letter(
2
), get_column_letter(
47
), get_column_letter(
900
))
# 把字母轉換成數字
print
(column_index_from_string(
'AAH'
))
|