Python使用OpenPyXL處理Excel表格
官方文件: http://openpyxl.readthedocs.io/en/default/
OpenPyXL庫 --單元格樣式設定
單元格樣式的控制,依賴openpyxl.style包,其中定義有樣式需要的物件,引入樣式相關:
from openpyxl.styles import PatternFill,Font,Alignment,Border,SideBorder 邊框 Side 邊線PatternFill 填充Font 字型Aignment 對齊
以上基本可滿足需要
基本用法是,將單元格物件的設定的屬性賦為新的與預設不同的相應物件。
匯入excel
from openpyxl import load_workbook from openpyxl.styles import Border,Side wb = load_workbook("模板.xlsx")#使用openpyxl讀取xlsx檔案,建立workbook ws = wb.active ws
<Worksheet "sheet1">
1、Border 邊框 Side 邊線
from openpyxl.styles import Border,Side border_type=Side(border_style=None,color='FF000000') border = Border(left=border_type,right=border_type,top=border_type,bottom=border_type,diagonal=border_type,diagonal_direction=0,outline=border_type,vertical=border_type,horizontal=border_type )
border_style的樣式有:
‘dashDot',‘dashDotDot',‘dashed',‘dotted',‘double',
‘hair',‘medium',‘mediumDashDot',‘mediumDashDotDot',
‘mediumDashed',‘slantDashDot',‘thick',‘thin'
舉例,原excel
# 樣式1 - 窄邊框,黑色 thin = Side(border_style="thin",color="000000")#邊框樣式,顏色 border = Border(left=thin,right=thin,top=thin,bottom=thin)#邊框的位置 ws['A3'].border = border #A3單元格設定邊框 for row in ws['A5:D6']: for cell in row: cell.border = border#A5:D6區域單元格設定邊框 wb.save("test.xlsx")
效果:
# 樣式2- 寬邊框,藍色 thin = Side(border_style="thick",color="0000FF")#邊框樣式,顏色 border = Border(left=thin,bottom=thin)#邊框的位置 ws['A3'].border = border #A3單元格設定邊框 for row in ws['A5:D6']: for cell in row: cell.border = border#A5:D6區域單元格設定邊框 wb.save("test.xlsx")
效果:
2、字型設定
from openpyxl.styles import Font font = Font(name='Calibri',size=11,color='FF000000',bold=False,italic=False,vertAlign=None,underline='none',strike=False)
字型名稱、字型大小、字型顏色、加粗、斜體、縱向對齊方式(有三種:baseline,superscript, subscript)、下劃線、刪除線,字型顏色可以用RGB 或 aRGB ,
font = Font(size=14,bold=True,name='微軟雅黑',color="FF0000")#字型大小,加粗,字型名稱,字型名字 ws['A3']="歡迎關注:永恆君的百寶箱" ws['A3'].font = font wb.save("test.xlsx")
3、填充
from openpyxl.styles import PatternFill # fill_type 的樣式為 None 或 solid fill = PatternFill(fill_type = None,start_color='FFFFFF',end_color='000000')
fill_type型別
有:'none'、'solid'、'darkDown'、'darkGray'、'darkGrid'、'darkHorizontal'、'darkTrellis'、'darkUp'、'darkVertical'、'gray0625'、
'gray125'、'lightDown'、'lightGray'、'lightGrid'、'lightHorizontal'、
'lightTrellis'、'lightUp'、'lightVertical'、'mediumGray'
官方文件中寫明,fill_type若沒有特別指定型別,則後續的引數都無效
所以上述程式碼就會出問題,start_color代表前景色,end_color是背景色,之所以設定兩個引數是為了方便樣式顏色的填充和漸變色的顯示(個人認為)
如果想要純色填充的話可以用'solid',然後令前景色為你需要的顏色即可,即:
fill = PatternFill(fill_type = None,start_color='FF0000') fill = PatternFill(patternType="solid",start_color="33CCFF")#純色填充 ws['A3']="歡迎關注:永恆君的百寶箱" ws['A3'].fill = fill wb.save("test.xlsx")
4、對齊
from openpyxl.styles import Alignment align = Alignment(horizontal='left',vertical='center',wrap_text=True)
horizontal代表水平方向,可以左對齊left,還有居中center和右對齊right,分散對齊distributed,跨列居中centerContinuous,兩端對齊justify,填充fill,常規general
vertical代表垂直方向,可以居中center,還可以靠上top,靠下bottom,兩端對齊justify,分散對齊distributed
自動換行:wrap_text,這是個布林型別的引數,這個引數還可以寫作wrapText
align = Alignment(horizontal='right',wrap_text=True)#純色填充 ws['A3']="永恆君的百寶箱" ws['A3'].alignment = align wb.save("test.xlsx")
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。