Python中用OpenPyXL處理Excel表格 - 單元格格式設置


官方文檔: http://openpyxl.readthedocs.io/en/default/

OpenPyXL庫 --單元格樣式設置

單元格樣式的控制,依賴openpyxl.style包,其中定義有樣式需要的對象,引入樣式相關:

from openpyxl.styles import PatternFill, Font, Alignment, Border, Side
  • Border 邊框 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, 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、字體設置

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)、下划線、刪除線,字體顏色可以用RGBaRGB

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',vertical='center',wrap_text=True)#純色填充
ws['A3']="永恆君的百寶箱"
ws['A3'].alignment = align
wb.save("test.xlsx")

在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM