1. 程式人生 > >Python excel 功能擴展庫 ——> openpyxl 的基本使用

Python excel 功能擴展庫 ——> openpyxl 的基本使用

.sh 背景色 max 操作文件 wing 實例化 t對象 html 路徑

說明:本文檔內容參考自 https://www.cnblogs.com/zeke-python-road/p/8986318.html (作者:關關雎鳩`)的文檔

from openpyxl import Workbook

from openpyxl import load_workbook
# 實例化一個操作對象
wb = Workbook()

# 獲取當前活躍狀態的sheet
ws = wb.active

‘‘‘
# 基本操作,插入方式按順序逐行插入
# 單元格內容控制
ws[‘A1‘] = ‘姓名‘
ws[‘B1‘] = ‘性別‘
# 在上文基礎上添加整行(上文存在三行,則從第四行開始整行插入)
ws.append([‘項目‘,‘姓名‘,‘時間‘,‘報價‘,‘備註‘])
ws.append([1,2,3])
ws.append([23,34,45,56])
# 單元格內容控制
ws[‘A2‘] = ‘jony‘
ws[‘A3‘] = ‘male‘
# 文件保存(必須用絕對路徑)
wb.save(‘/home/ht/fir.xlsx‘)
‘‘‘

‘‘‘
# 創建新的工作區,並輸入該區的標簽名(標簽名,位置)* 位置:該區在標簽中的排序
w1 = wb.create_sheet(‘sheet1‘)
w2 = wb.create_sheet(‘sheet2‘)
w3 = wb.create_sheet(‘sheet3‘,0)

# 這裏是修改該區標簽名
w1.title = ‘sheet-1‘
w2.title = ‘sheet-2‘
w3.title = ‘sheet-3‘

w2.append([‘ds‘,‘hp‘,‘wq‘])

# 設置標簽的背景色(不是表格的單元格)
w2.sheet_properties.tabColor = "1072BA"

# 可以通過工作區的title名來獲取該sheet對象
wanna = wb.get_sheet_by_name(‘sheet-2‘)

print(wanna)
print(wb.sheetnames)

# <Worksheet "sheet-2">
# [‘sheet-3‘, ‘Sheet‘, ‘sheet-1‘, ‘sheet-2‘]

wb[‘sheet-3‘].append([1,2,3,4,5])

# 復制工作區,新的工作區的默認命名為sheet-3 Copy
new_3 = wb.copy_worksheet(w3)
# 復制品重命名
new_3.title = ‘new‘
wb.save(‘/home/ht/mul_sheet.xlsx‘)
‘‘‘

‘‘‘
sh1 = wb.active
sh2 = wb.create_sheet(‘sheet-cell‘)

# 單表格坐標數值輸入
sh2[‘A1‘] = ‘aaa插入內容‘
# 單元格坐標接受小寫
sh2[‘d4‘] = ‘表格小寫‘
# 單元格行列值坐標輸入
cell1 = sh2.cell(row=3,column=2,value=‘三行二列‘)
cell2 = sh2.cell(3,4,‘三行四列‘)
print(cell1.value)
# 三行二列
wb.save(‘/home/ht/sheet-cell.xlsx‘)
‘‘‘

‘‘‘
# 批量獲取單元格
mul_cell = wb.active
mul_cell.append([‘a1‘,‘b1‘,‘c1‘,‘d1‘])
mul_cell.append([‘a2‘,‘b2‘,‘c2‘,‘d2‘])
mul_cell.append([‘a3‘,‘b3‘,‘c3‘,‘d3‘])

# 獲取A列所有單元格
print(mul_cell[‘a‘])
# (<Cell ‘Sheet‘.A1>,
# <Cell ‘Sheet‘.A2>,
# <Cell ‘Sheet‘.A3>)

# 獲取 BCD 三列所有單元格
print(mul_cell[‘b:d‘])
# ((<Cell ‘Sheet‘.B1>, <Cell ‘Sheet‘.B2>, <Cell ‘Sheet‘.B3>),
# (<Cell ‘Sheet‘.C1>, <Cell ‘Sheet‘.C2>, <Cell ‘Sheet‘.C3>),
# (<Cell ‘Sheet‘.D1>, <Cell ‘Sheet‘.D2>, <Cell ‘Sheet‘.D3>))

# 獲取第 2到3 行所有單元格
print(mul_cell[2:3])
# ((<Cell ‘Sheet‘.A2>, <Cell ‘Sheet‘.B2>, <Cell ‘Sheet‘.C2>, <Cell ‘Sheet‘.D2>),
# (<Cell ‘Sheet‘.A3>, <Cell ‘Sheet‘.B3>, <Cell ‘Sheet‘.C3>, <Cell ‘Sheet‘.D3>))

# iter_rows 方法,設定參數,獲取二行三列區塊內的所有單元格(獲取基本單位為行,再從行內獲取單元格)
for r in mul_cell.iter_rows(min_row=1,max_row=2,min_col=1,max_col=3):
print(r)
for c in r:
print(c,c.value)

# (<Cell ‘Sheet‘.A1>, <Cell ‘Sheet‘.B1>, <Cell ‘Sheet‘.C1>)
# <Cell ‘Sheet‘.A1> a1
# <Cell ‘Sheet‘.B1> b1
# <Cell ‘Sheet‘.C1> c1
# (<Cell ‘Sheet‘.A2>, <Cell ‘Sheet‘.B2>, <Cell ‘Sheet‘.C2>)
# <Cell ‘Sheet‘.A2> a2
# <Cell ‘Sheet‘.B2> b2
# <Cell ‘Sheet‘.C2> c2

# iter_rows 方法,設定參數,獲取二行三列區塊內的所有單元格(獲取基本單位為列,再從列內獲取單元格)
for r in mul_cell.iter_cols(min_row=1,max_row=2,min_col=1,max_col=3):
print(r)

# (<Cell ‘Sheet‘.A1>, <Cell ‘Sheet‘.A2>)
# (<Cell ‘Sheet‘.B1>, <Cell ‘Sheet‘.B2>)
# (<Cell ‘Sheet‘.C1>, <Cell ‘Sheet‘.C2>)

# 獲取所有行
for r in mul_cell.rows:
print(r)
# (<Cell ‘Sheet‘.A1>, <Cell ‘Sheet‘.B1>, <Cell ‘Sheet‘.C1>, <Cell ‘Sheet‘.D1>)
# (<Cell ‘Sheet‘.A2>, <Cell ‘Sheet‘.B2>, <Cell ‘Sheet‘.C2>, <Cell ‘Sheet‘.D2>)
# (<Cell ‘Sheet‘.A3>, <Cell ‘Sheet‘.B3>, <Cell ‘Sheet‘.C3>, <Cell ‘Sheet‘.D3>)

print(mul_cell.rows)
# <generator object Worksheet._cells_by_row at 0x7f4c615312b0>

# 獲取所有列
print(mul_cell.columns)
# <generator object Worksheet._cells_by_col at 0x7f4c615312b0>
wb.save(‘/home/ht/mul_cell.xlsx‘)
‘‘‘

‘‘‘
from openpyxl import load_workbook

# 操作文件
read_sheet = load_workbook(‘/home/ht/mul_sheet.xlsx‘)

read_sheet.guess_types = True

current = read_sheet.active
current[‘e2‘] = ‘65%‘

read_sheet.save(‘/home/ht/mul_sheet.xlsx‘)
‘‘‘

‘‘‘
from openpyxl import load_workbook
wsheet = load_workbook(‘/home/ht/mul_cell.xlsx‘)

# sheet 對象不存在腳標,只能通過坐標獲取,wsheet.active.rows[1]是不能獲取第二行的!
print(wsheet.active[‘a‘][1].value)
print(wsheet.active[‘2‘][1].value)
# a2
# b2
‘‘‘

‘‘‘ 單元格分割合並

deal_cell = load_workbook(‘/home/ht/sum_sheet.xlsx‘)
sheet = deal_cell.active

# 合並第一行1到4單元格(a1,b1,c1,d1)
sheet.merge_cells(‘a1:d1‘)

# 將 第一行 a1 到 d1 位置的單元格分離出來
sheet.unmerge_cells(‘a1:d1‘)

# 采用區塊的方式合並或分割操作
sheet.merge_cells(start_row=1,end_row=3,start_column=4,end_column=6)

deal_cell.save(‘/home/ht/sum_sheet.xlsx‘)
‘‘‘

‘‘‘ 單元格插入圖片

# 需要安裝PIL圖片包
from openpyxl.drawing.image import Image

img = Image(‘/home/ht/qcode.png‘)
ws.add_image(img,‘F3‘) # 這裏圖片填充的位置坐標必須使用大寫字母
wb.save(‘/home/ht/addimg.xlsx‘)
‘‘‘

Python excel 功能擴展庫 ——> openpyxl 的基本使用