Python使用OpenPyXL處理Excel表格
官方文檔: http://openpyxl.readthedocs.io/en/default/
OpenPyXL庫(kù) --單元格樣式設(shè)置
單元格樣式的控制,依賴(lài)openpyxl.style包,其中定義有樣式需要的對(duì)象,引入樣式相關(guān):
from openpyxl.styles import PatternFill, Font, Alignment, Border, SideBorder 邊框 Side 邊線(xiàn)PatternFill 填充Font 字體Aignment 對(duì)齊
以上基本可滿(mǎn)足需要
基本用法是,將單元格對(duì)象的設(shè)置的屬性賦為新的與默認(rèn)不同的相應(yīng)對(duì)象。
導(dǎo)入excel
from openpyxl import load_workbook from openpyxl.styles import Border,Side wb = load_workbook("模板.xlsx")#使用openpyxl讀取xlsx文件,創(chuàng)建workbook ws = wb.active ws
<Worksheet "sheet1">
1、Border 邊框 Side 邊線(xiàn)
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單元格設(shè)置邊框 for row in ws['A5:D6']: for cell in row: cell.border = border#A5:D6區(qū)域單元格設(shè)置邊框 wb.save("test.xlsx")
效果:
# 樣式2- 寬邊框,藍(lán)色 thin = Side(border_style="thick", color="0000FF")#邊框樣式,顏色 border = Border(left=thin, right=thin, top=thin, bottom=thin)#邊框的位置 ws['A3'].border = border #A3單元格設(shè)置邊框 for row in ws['A5:D6']: for cell in row: cell.border = border#A5:D6區(qū)域單元格設(shè)置邊框 wb.save("test.xlsx")
效果:
2、字體設(shè)置
from openpyxl.styles import Font font = Font(name='Calibri', size=11, color='FF000000', bold=False, italic=False, vertAlign=None, underline='none', strike=False)
字體名稱(chēng)、字體大小、字體顏色、加粗、斜體、縱向?qū)R方式(有三種:baseline,superscript, subscript)、下劃線(xiàn)、刪除線(xiàn),字體顏色可以用RGB 或 aRGB ,
font = Font(size=14, bold=True, name='微軟雅黑', color="FF0000")#字體大小,加粗,字體名稱(chēng),字體名字 ws['A3']="歡迎關(guān)注:永恒君的百寶箱" 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類(lèi)型
有:'none'、'solid'、'darkDown'、'darkGray'、'darkGrid'、'darkHorizontal'、'darkTrellis'、'darkUp'、'darkVertical'、'gray0625'、
'gray125'、'lightDown'、'lightGray'、'lightGrid'、'lightHorizontal'、
'lightTrellis'、'lightUp'、'lightVertical'、'mediumGray'
官方文檔中寫(xiě)明,fill_type若沒(méi)有特別指定類(lèi)型,則后續(xù)的參數(shù)都無(wú)效
所以上述代碼就會(huì)出問(wèn)題,start_color代表前景色,end_color是背景色,之所以設(shè)置兩個(gè)參數(shù)是為了方便樣式顏色的填充和漸變色的顯示(個(gè)人認(rèn)為)
如果想要純色填充的話(huà)可以用'solid',然后令前景色為你需要的顏色即可,即:
fill = PatternFill(fill_type = None,start_color='FF0000') fill = PatternFill(patternType="solid", start_color="33CCFF")#純色填充 ws['A3']="歡迎關(guān)注:永恒君的百寶箱" ws['A3'].fill = fill wb.save("test.xlsx")
4、對(duì)齊
from openpyxl.styles import Alignment align = Alignment(horizontal='left',vertical='center',wrap_text=True)
horizontal代表水平方向,可以左對(duì)齊left,還有居中center和右對(duì)齊right,分散對(duì)齊distributed,跨列居中centerContinuous,兩端對(duì)齊justify,填充fill,常規(guī)general
vertical代表垂直方向,可以居中center,還可以靠上top,靠下bottom,兩端對(duì)齊justify,分散對(duì)齊distributed
自動(dòng)換行:wrap_text,這是個(gè)布爾類(lèi)型的參數(shù),這個(gè)參數(shù)還可以寫(xiě)作wrapText
align = Alignment(horizontal='right',vertical='center',wrap_text=True)#純色填充 ws['A3']="永恒君的百寶箱" ws['A3'].alignment = align wb.save("test.xlsx")
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
nlp自然語(yǔ)言處理基于SVD的降維優(yōu)化學(xué)習(xí)
這篇文章主要為大家介紹了nlp自然語(yǔ)言處理基于SVD的降維優(yōu)化學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04python?opencv背景減去法摳圖實(shí)現(xiàn)示例
這篇文章主要為大家介紹了python?opencv背景減去法摳圖實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05python工具模塊介紹之time?時(shí)間訪(fǎng)問(wèn)和轉(zhuǎn)換的示例代碼
這篇文章主要介紹了python工具模塊介紹-time?時(shí)間訪(fǎng)問(wèn)和轉(zhuǎn)換,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家啊的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04