OpenPyXL庫 --單元格樣式設置html
單元格的控制,依賴openpyxl.style包,其中定義有須要的對象,引入相關:app
from openpyxl.styles import PatternFill, Font, Alignment, Border, SideBorder 邊框 Side 邊線PatternFill 填充Font 字體Aignment 對齊ide
以上基本可知足須要學習
基本用法是,將單元格對象的設置的屬性賦爲新的與默認不一樣的相應對象。字體
導入excelurl
from openpyxl import load_workbookspa
from openpyxl.styles import Border,Side3d
wb = load_workbook("模板.xlsx")#使用openpyxl讀取xlsx文件,建立workbookexcel
ws = wb.activehtm
ws
<Worksheet "sheet1">
一、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, 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")
效果:
二、字體設置
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")
三、填充
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")
四、對齊
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',vertical='center',wrap_text=True)#純色填充
ws['A3']="永恆君的百寶箱"
ws['A3'].alignment = align
wb.save("test.xlsx")
以上就是本文的所有內容,但願對你們的學習有所幫助,也但願你們多多支持。