python處理excel之openpyxl
引用:https://www.cnblogs.com/programmer-tlh/p/10461353.html
創建exce
# 創建工作簿
wb = openpyxl.Workbook()
# 保存excel
wb.save('d:/test.xls')
打開已有excel
# 打開excel
wb = openpyxl.load_workbook("d:/test.xlsx")
# 打開sheet
ws = wb['test1']
儲存數據
# 方式一:數據可以直接分配到單元格中(可以輸入公式)
ws['A1'] = 'test'
# 方式二:可以附加行,從第一列開始附加(從最下方空白處,最左開始)(可以輸入多行)
ws.append(['id', 'name', 'age'])
創建表(sheet)
# 方式一:插入到最後(default)
ws = wb.create_sheet('test2')
# 方式二:插入到指定的位置
ws = wb.create_sheet('test3', 0)
# 如果sheet名已存在,會在sheet名後面追加數字(如已有test、test1、test3,新創建的爲test4)
ws = wb.create_sheet('test')
選擇表(sheet)
# sheet 名稱可以作爲 key 進行索引
ws = wb['test']
ws1 = wb.get_sheet_by_name('test1')
查看錶名(sheet)
# 顯示所有表名
wb.sheetnames
['test3', 'test', 'test4', 'test1', 'Sheet', 'Sheet1', 'test2', 'test5']
# 判斷表格是否存在
'test' in wb.sheetnames
True
刪除工作表
# 方式一
wb.remove(sheet)
# 方式二
del wb[sheet]
訪問單元格(cell)
單一單元格訪問
# 方法一:沒有值返回None
>>> c = ws['A1']
# 方法二:row 行,column 列,默認值
>>> d = ws.cell(4, 2, value=10)
多單元格訪問
切片多單元格應當符合:從左到右,從上到下
# 獲取某行
>>> ws['1']
(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>)
# 獲取某列
>>> ws['A']
(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>, <Cell 'Sheet1'.A3>, <Cell 'Sheet1'.A4>)
# A列到B列
>>> ws['A':'B']
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>, <Cell 'Sheet1'.A3>, <Cell 'Sheet1'.A4>),
(<Cell 'Sheet1'.B1>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.B4>))
# 第行到第4行
>>> ws['1':'4']
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>),
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>),
(<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.C3>),
(<Cell 'Sheet1'.A4>, <Cell 'Sheet1'.B4>, <Cell 'Sheet1'.C4>))
# 單元格到單元格:一行中的某段
>>> ws['A1':'B1']
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>),)
# 單元格到單元格:從左到右,從上到下
>>> ws['A1':'B2']
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>),
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>))
# 不合法的姿勢
>>> ws['A2':'B1']
()
>>> ws['B2':'A1']
()
>>> ws['B2':'A1':-1]
()
行列條件迭代
ws.iter_rows
、ws.iter_cols
四個可填參數:
- min_row:最小行(默認爲1)
- max_row:最大行(默認爲sheet最大值)
- min_col:最小列(默認爲1)
- max_col:最大列(默認爲sheet最大值)
# 指定範圍(行 → 行) 與ws['A1':'C2']效果相同
>>> ws.iter_rows(min_row=1, max_col=3, max_row=2)
<generator object Worksheet._cells_by_row at 0x00000181E5939648> # 返回生成器
>>> tuple(ws.iter_rows(min_row=1, max_col=3, max_row=2))
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>),
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>))
# 指定範圍(列 → 列)
>>> tuple(ws.iter_cols(max_col=3, max_row=2))
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>),
(<Cell 'Sheet1'.B1>, <Cell 'Sheet1'.B2>),
(<Cell 'Sheet1'.C1>, <Cell 'Sheet1'.C2>))
所有行
返回生成器,行–>行
>>> ws.rows
<generator object Worksheet._cells_by_row at 0x00000181E597F448>
>>> tuple(ws.rows)
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>),
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>),
(<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.C3>),
(<Cell 'Sheet1'.A4>, <Cell 'Sheet1'.B4>, <Cell 'Sheet1'.C4>),
(<Cell 'Sheet1'.A5>, <Cell 'Sheet1'.B5>, <Cell 'Sheet1'.C5>),
(<Cell 'Sheet1'.A6>, <Cell 'Sheet1'.B6>, <Cell 'Sheet1'.C6>))
所有列
返回生成器,列–>列
>>> tuple(ws.columns)
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>, <Cell 'Sheet1'.A3>, <Cell 'Sheet1'.A4>, <Cell 'Sheet1'.A5>, <Cell 'Sheet1'.A6>),
(<Cell 'Sheet1'.B1>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.B4>, <Cell 'Sheet1'.B5>, <Cell 'Sheet1'.B6>),
(<Cell 'Sheet1'.C1>, <Cell 'Sheet1'.C2>, <Cell 'Sheet1'.C3>, <Cell 'Sheet1'.C4>, <Cell 'Sheet1'.C5>, <Cell 'Sheet1'.C6>))
保存數據
wb.save('d:/test.xlsx')
其他
改變 sheet 標籤按鈕顏色
ws.sheet_properties.tabColor = "1072BA"
設置行高和列寬
# 第2行行高
sheet.row_dimensions[2].height = 40
# C列列寬
sheet.column_dimensions['C'].width = 30
獲取最大行,最大列
# 獲得最大列和最大行
print(sheet.max_row)
print(sheet.max_column)
根據數字得到字母,根據字母得到數字
from openpyxl.utils import get_column_letter, column_index_from_string
# 根據列的數字返回字母
print(get_column_letter(2)) # B
# 根據字母返回列的數字
print(column_index_from_string('D')) # 4
合併和拆分單元格
- 所謂合併單元格,即以合併區域的左上角的那個單元格爲基準,覆蓋其他單元格使之稱爲一個大的單元格。
- 相反,拆分單元格後將這個大單元格的值返回到原來的左上角位置。
# 合併單元格, 往左上角寫入數據即可
sheet.merge_cells('B1:G1') # 合併一行中的幾個單元格
sheet.merge_cells('A1:C3') # 合併一個矩形區域中的單元格
- 合併後只可以往左上角寫入數據,也就是區間中:左邊的座標。
- 如果這些要合併的單元格都有數據,只會保留左上角的數據,其他則丟棄。換句話說若合併前不是在左上角寫入數據,合併後單元格中不會有數據。
- 以下是拆分單元格的代碼。拆分後,值回到A1位置
sheet.unmerge_cells('A1:C3')