前言
繼續搞Python對Excel文件的處理,此次主要解決如何使用Openpyxl模塊的Styles來生成一個具備個性化樣式的Excel文件。本篇基本算是翻譯了部分實用的官方文檔內容,推薦小夥伴們最好的學習方法,莫過於閱讀官方文檔。: )
官當文檔傳送門:http://openpyxl.readthedocs.org/en/default/styles.html#worksheet-additional-propertieshtml
使用Excel樣式函數
Introduction(簡介)
Styles are used to change the look of your data while displayed on screen. They are also used to determine the number format being used for a given cell or range of cells.
Styles是用於改變你的但願顯示的數據的樣式。也能夠用於設置指定的單元格或單元區域的數字格式。python
The following are the default values(下面是函數的參數缺省值)app
>>> 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) >>>
注意:
Styles are shared between objects and once they have been assigned they cannot be changed. This stops unwanted side-effects such as changing the style for lots of cells when instead of only one.
不一樣的對象之間是能夠共享同一個Styles的,而且一旦爲對象指定了Styles以後就不能夠再次更改。這是爲了在更改不少的單元格的Styles而不只只是更改一個單元格時可以避免沒必要要的反作用。ide
>>> 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) #定義一個能夠共享的Styles >>> a1.font = ft >>> d4.font = ft >>> >>> a1.font.italic = True # is not allowed 對象在指定Styles後是不容許被更改的 >>> >>> # If you want to change the color of a Font, you need to reassign it:: >>> # 若是你想更改個別對象的Styles須要從新定義,且只會影響到個別對象 >>> a1.font = Font(color=colors.RED, italic=True) # the change only affects A1
Styles can also be copied(Styles能夠被複制)函數
>>> from openpyxl.styles import Font >>> >>> ft1 = Font(name='Arial', size=14) >>> ft2 = ft1.copy(name="Tahoma") #複製並修改特定屬性 >>> ft1.name 'Arial' >>> ft2.name 'Tahoma' >>> ft2.size # copied from the 14.0
Colors are usually RGB or aRGB hexvalues. The colors module contains some constants
Colors一般是RGB或者是RGB的十六進制表示。Colors模塊包含了一些常量學習
>>> from openpyxl.styles import Font >>> from openpyxl.styles.colors import RED >>> font = Font(color=RED) #RGB >>> font = Font(color="FFBB00") #RGB hexvalues
There is also support for legacy indexed colors as well as themes and tints
Colors也支持索引顏色、主題和色彩字體
>>> from openpyxl.styles.colors import Color >>> c = Color(indexed=32) #legacy indexed colors 定製好的Color經過索引調用 >>> c = Color(theme=6, tint=0.5)
Styles are applied directly to cells
Styles直接應用於單元格this
>>> from openpyxl.workbook import Workbook >>> from openpyxl.styles import Font, Fill >>> wb = Workbook() >>> ws = wb.active >>> c = ws['A1'] #獲取單元格對象 >>> c.font = Font(size=12) #直接修改單元格對象的字體樣式
Styles can also applied to columns and rows but note that this applies only to cells created (in Excel) after the file is closed. If you want to apply styles to entire rows and columns then you must apply the style to each cell yourself. This is a restriction of the file format
Styles也能夠應用於列和行,可是須要注意的是這種應用只能適用於在關閉文件以後建立的單元格。若是你想應用Style於所有的行和列,你必須爲每個單元格都應用Style。這是因爲文件格式的制約翻譯
>>> col = ws.column_dimensions['A'] #獲取A列的樣式 >>> col.font = Font(bold=True) >>> row = ws.row_dimensions[1] #獲取1行的樣式 >>> row.font = Font(underline="single")
>>> 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
>>> from openpyxl.workbook import Workbook >>> >>> wb = Workbook() >>> ws = wb.active >>> >>> ws.print_options.horizontalCentered = True #水平居中 >>> ws.print_options.verticalCentered = True #垂直居中
Headers and footers use their own formatting language. This is fully supported when writing them.but, due to the complexity and the possibility of nesting, only partially when reading them.rest
頭部和尾部使用它們自身的格式化語言。當你寫的時候是徹底支持的。可是因爲複製性和嵌套的可能性,讓你讀取的時候可能只能讀取到一個部分。
>>> from openpyxl.workbook import Workbook >>> >>> wb = Workbook() >>> ws = wb.worksheets[0] >>> #設置文件頭部和頁尾的樣式 >>> ws.header_footer.center_header.text = 'My Excel Page' >>> ws.header_footer.center_header.font_size = 14 >>> ws.header_footer.center_header.font_name = "Tahoma,Bold" >>> ws.header_footer.center_header.font_color = "CC3366"