openpyxl設(shè)置單元格樣式的方法?這個(gè)問(wèn)題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見(jiàn)到的。希望通過(guò)這個(gè)問(wèn)題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到新城網(wǎng)站設(shè)計(jì)與新城網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋新城地區(qū)。
在對(duì)openpyxl有了簡(jiǎn)單了解之后,今天本文將通過(guò)實(shí)例詳細(xì)講解openpyxl的單元格樣式
openpyxl的單元格樣式由6種屬性決定,每一種都是一個(gè)類,需要注意的是這在對(duì)樣式屬性賦值時(shí),樣式實(shí)例一經(jīng)創(chuàng)建,樣式實(shí)例的屬性就不可更改,只能新建樣式實(shí)例。如下所示:
font(字體類):字號(hào)、字體顏色、下劃線等
fill(填充類):顏色等
border(邊框類):設(shè)置單元格邊框
alignment(位置類):對(duì)齊方式
number_format(格式類):數(shù)據(jù)格式
protection(保護(hù)類):寫保護(hù)
單元格默認(rèn)樣式如下:
>>> from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font >>> font = Font(name='Calibri', ... size=11, ... bold=False, ... italic=False, ... vertAlign=None, ... underline='none', ... strike=False, ... color='FF000000') >>> fill = PatternFill(fill_type=None, ... start_color='FFFFFFFF', ... end_color='FF000000') >>> border = Border(left=Side(border_style=None, ... color='FF000000'), ... right=Side(border_style=None, ... color='FF000000'), ... top=Side(border_style=None, ... color='FF000000'), ... bottom=Side(border_style=None, ... color='FF000000'), ... diagonal=Side(border_style=None, ... color='FF000000'), ... diagonal_direction=0, ... outline=Side(border_style=None, ... color='FF000000'), ... vertical=Side(border_style=None, ... color='FF000000'), ... horizontal=Side(border_style=None, ... color='FF000000') ... ) >>> alignment=Alignment(horizontal='general', ... vertical='bottom', ... text_rotation=0, ... wrap_text=False, ... shrink_to_fit=False, ... indent=0) >>> number_format = 'General' >>> protection = Protection(locked=True, ... hidden=False)
以上幾種樣式(字體、填充、邊框、位置和保護(hù))實(shí)例一旦被創(chuàng)建實(shí)例的屬性就不可更改,只能重新創(chuàng)建實(shí)例。
>>> from openpyxl.styles import colors >>> from openpyxl.styles import Font, Color >>> from openpyxl.styles import colors >>> from openpyxl import Workbook >>> wb = Workbook() >>> ws = wb.active >>> >>> a1 = ws['A1'] >>> d4 = ws['D4'] >>> ft = Font(color=colors.RED) >>> a1.font = ft >>> d4.font = ft >>> >>> a1.font.italic = True # 不會(huì)生效,樣式實(shí)例一旦創(chuàng)建,實(shí)例的屬性就不可改變 >>> >>> # 若要改變樣式,必須新建樣式實(shí)例 >>> >>> a1.font = Font(color=colors.RED, italic=True) # 創(chuàng)建字體實(shí)例(紅色、斜體,其他屬性默認(rèn))
復(fù)制樣式
樣式可以被復(fù)制:
>>> from openpyxl.styles import Font >>> >>> ft1 = Font(name='Arial', size=14) >>> ft2 = ft1.copy(name="Tahoma") # 復(fù)制時(shí)指定字體為“Tahoma”,其他屬性均復(fù)制自ft1 >>> ft1.name 'Arial' >>> ft2.name 'Tahoma' >>> ft2.size 14.0
基本字體顏色
字體顏色有一些顏色常量,可以直接調(diào)用:
from openpyxl.styles import Font from openpyxl.styles.colors import RED font = Font(color=RED) font = Font(color="00FFBB00")
也可以按索引創(chuàng)建實(shí)例:
from openpyxl.styles.colors import Color c = Color(indexed=32) c = Color(theme=6, tint=0.5)
說(shuō)到顏色,V2.3.5版本必須使用“00XXXXXX”的格式,不能使用“#XXXXXX”的格式。如果顏色不生效請(qǐng)查看版本后修改。
應(yīng)用樣式
可以直接應(yīng)用到單元格:
from openpyxl.workbook import Workbook from openpyxl.styles import Font, Fill wb = Workbook() ws = wb.active c = ws['A1'] c.font = Font(size=12)
可以對(duì)整行整列設(shè)置樣式,前提是單元格已創(chuàng)建。
col = ws.column_dimensions['A'] col.font = Font(bold=True) row = ws.row_dimensions[1] row.font = Font(underline="single")
數(shù)據(jù)格式
數(shù)據(jù)格式屬性number_format的值是字符串類型,不為對(duì)象,直接賦值即可。
openpyxl內(nèi)置了一些數(shù)據(jù)格式查看openpyxl.styles.numbers,也支持excel自定義格式,以下兩種方式效果相同:
# 使用openpyxl內(nèi)置的格式 from openpyxl.styles import numbers ws.cell['D2'].number_format = numbers.FORMAT_GENERAL ws.cell(row=2, column=4).number_format = numbers.FORMAT_DATE_XLSX15
# 直接使用字符串 ws.cell['D2].number_format = 'General' ws.cell(row=2, column=4).number_format = 'd-mmm-yy'
編輯頁(yè)面設(shè)置
from openpyxl.workbook import Workbook wb = Workbook() ws = wb.active ws.page_setup.orientation = ws.ORIENTATION_LANDSCAPE ws.page_setup.paperSize = ws.PAPERSIZE_TABLOID ws.page_setup.fitToHeight = 0 ws.page_setup.fitToWidth = 1
編輯打印選項(xiàng)
from openpyxl.workbook import Workbook wb = Workbook() ws = wb.active ws.print_options.horizontalCentered = True ws.print_options.verticalCentered = True
感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)openpyxl設(shè)置單元格樣式的方法大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。