Python 的 openpyxl 模塊可讓咱們能讀取和修改 Excel 文件。bash
首先讓咱們先理解一些 Excel 基礎概念。函數
Excel 文件也稱作爲工做簿。每一個工做簿能夠包含多個工做表(Sheet)。用戶當前查看的表或關閉 Excel 前最後查看的表,稱爲活動表。ui
每一張表都是由列和行構成的。列是以 A 開始的字母表示;而行是以 1 開始的數字表示的。由特定行和列所指定的方格稱爲單元格。每一個單元格均可以包含一個數字或文本。這些單元格就構成了這張表。spa
經過 pip 就能夠安裝最新版的 openpyxl。excel
pip install openpyxl
複製代碼
假設有這樣一份世界人口統計 Excel 文檔,內容以下:code
wb = openpyxl.load_workbook('population.xlsx')
print('wb 類型 :')
print(type(wb), '\n')
複製代碼
運行結果:cdn
wb 類型 : <class 'openpyxl.workbook.workbook.Workbook'>對象
導入 openpyxl 模塊以後,就可使用 openpyxl.load_workbook() 函數來加載 Excel 文檔咯。這個 Excel 文件表示爲 Workbook 對象。blog
注意: load_workbook() 函數中的文件,必須在當前工做目錄。能夠導入 os 模塊,利用 os.getcwd() 來獲悉當前工做目錄。os 還提供了 chdir() 方法,能夠改變當前工做目錄。索引
import os
print('當前工做目錄 :')
print(os.getcwd(), '\n')
複製代碼
運行結果:
...
print('取得全部工做表的表名 :')
print(wb.sheetnames, '\n')
print('取得某張工做表 :')
sheet = wb['Sheet3']
print(type(sheet))
print('表名 - ' + sheet.title, '\n')
print('取得活動工做表 :')
active_sheet = wb.active
print('表名 - ' + active_sheet.title, '\n')
複製代碼
運行結果:
取得全部工做表的表名 : ['Sheet1', 'Sheet2', 'Sheet3']
取得某張工做表 : <class 'openpyxl.worksheet.worksheet.Worksheet'> 表名 - Sheet3
取得活動工做表 : 表名 - Sheet1
...
print('取得 A1 單元格 :')
cell = active_sheet['A1']
print(cell)
print(cell.value, '\n')
print('取得 B1 單元格 :')
cell = active_sheet['B1']
print(cell)
print(cell.value, '\n')
print('行號爲 ' + str(cell.row) + ',列號爲 ' + str(cell.column) + ' 的單元格,其值爲 ' + cell.value, '\n')
print('單元格 ' + cell.coordinate + ' 其值爲 ' + cell.value, '\n')
print('取得 C1 單元格的值 :')
print(active_sheet['C1'].value, '\n')
print('經過指定行與列,來獲取單元格:')
print(active_sheet.cell(row=1, column=2))
print(active_sheet.cell(row=1, column=2).value)
print('迭代行與列,來獲取單元格的值:')
for i in range(1, 8, 2):
print(i, active_sheet.cell(row=i, column=2).value)
print('\n')
複製代碼
運行結果:
取得 A1 單元格 : <Cell 'Sheet1'.A1> 排名
取得 B1 單元格 : <Cell 'Sheet1'.B1> 國家
行號爲 1,列號爲 2 的單元格,其值爲 國家
單元格 B1 其值爲 國家
取得 C1 單元格的值 : 人口
經過指定行與列,來獲取單元格: <Cell 'Sheet1'.B1> 國家 迭代行與列,來獲取單元格的值: 1 國家 3 印度 5 印度尼西亞 7 巴基斯坦
print('獲取工做表的大小:')
print('總行數 -> ' + str(active_sheet.max_row))
print('總列數 -> ' + str(active_sheet.max_column))
複製代碼
運行結果:
獲取工做表的大小: 總行數 -> 11 總列數 -> 4
Worksheet 對象的 max_row 與 max_column,能夠獲取工做表的總行數與總列數,即工做表的大小。
openpyxl 提供了兩個函數,用於轉換列號:
import openpyxl
from openpyxl.utils import get_column_letter, column_index_from_string
...
print('列轉換函數:')
print('[數字轉換爲字母]')
print('第 1 列 -> ' + get_column_letter(1))
print('第 2 列 -> ' + get_column_letter(2))
print('第 37 列 -> ' + get_column_letter(37))
print('第 818 列 -> ' + get_column_letter(818))
print('[字母轉換爲數字]')
print('第 A 列 -> ' + str(column_index_from_string('A')))
print('第 CC 列 -> ' + str(column_index_from_string('CC')))
複製代碼
運行結果:
[數字轉換爲字母] 第 1 列 -> A 第 2 列 -> B 第 37 列 -> AK 第 818 列 -> AEL [字母轉換爲數字] 第 A 列 -> 1 第 CC 列 -> 81
咱們能夠對 Worksheet 對象切片,取得表格中的一個矩形區域,迭代遍歷這個區域中的全部 Cell 對象。
print(tuple(active_sheet['A2':'D4']))
for row_objects in active_sheet['A2':'D4']:
for cell_object in row_objects:
print(cell_object.coordinate, cell_object.value)
print('-- 當前行獲取結束 --')
複製代碼
運行結果:
((<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>, <Cell 'Sheet1'.D2>), (<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.C3>, <Cell 'Sheet1'.D3>), (<Cell 'Sheet1'.A4>, <Cell 'Sheet1'.B4>, <Cell 'Sheet1'.C4>, <Cell 'Sheet1'.D4>)) A2 1 B2 中國 C2 13.83億人(2016) D2 9634057 -- 當前行獲取結束 -- A3 2 B3 印度 C3 1339180127 D3 2973190 -- 當前行獲取結束 -- A4 3 B4 美國 C4 324459463 D4 9147420 -- 當前行獲取結束 --
咱們可使用 Worksheet 對象的 rows 和 columns 屬性,來獲取指定行或者列:
print('獲取特定行:')
print(list(active_sheet.rows)[2])
for cell_object in list(active_sheet.rows)[2]:
print(cell_object.value)
print('獲取特定列:')
print(list(active_sheet.columns)[2])
for cell_object in list(active_sheet.columns)[2]:
print(cell_object.value)
複製代碼
運行結果:
獲取特定行: (<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.C3>, <Cell 'Sheet1'.D3>) 2 印度 1339180127 2973190 獲取特定列: (<Cell 'Sheet1'.C1>, <Cell 'Sheet1'.C2>, <Cell 'Sheet1'.C3>, <Cell 'Sheet1'.C4>, <Cell 'Sheet1'.C5>, <Cell 'Sheet1'.C6>, <Cell 'Sheet1'.C7>, <Cell 'Sheet1'.C8>, <Cell 'Sheet1'.C9>, <Cell 'Sheet1'.C10>, <Cell 'Sheet1'.C11>) 人口 13.83億人(2016) 1339180127 324459463 263991379 209288278 197015955 190886311 164669751 143989754 129163276
讀取 Excel 步驟,總結以下: