上節對python如何定製word的頁眉頁腳做了詳細介紹,當然,要作出一篇精彩的word文檔,樣式公布可沒,本章繼續介紹python如何玩轉word的樣式。
使用樣式
python如何玩轉word的樣式
此頁面使用前一頁中開發的概念而不作介紹。如果術語不熟悉,請參閱word的樣式參考。
訪問樣式
使用以下屬性訪問樣式:
>>> document = Document()
>>> styles = document.styles
>>> styles
<docx.styles.styles.Styles object at 0x10a7c4f50>
該對象按名稱提供對定義樣式的字典式訪問:
>>> styles['Normal']
<docx.styles.style._ParagraphStyle object at <0x10a7c4f6b>
注意
內置樣式使用其英文名稱存儲在WordprocessingML文件中,例如"標題1",即使在本地化版本的Word上工作的用戶將在UI中看到本地語言名稱,例如"Kop 1"。因為python-docx在WordprocessingML文件上運行,所以樣式查找必須使用英文名稱。此外部站點上提供的文檔允許您創建本地語言名稱和英語樣式名稱之間的映射::show =
用戶定義的樣式(也稱為自定義樣式)未進行本地化,並且使用與Word UI中顯示的名稱完全相同的名稱進行訪問。
該對象也是可迭代的。通過使用標識屬性,可以生成定義樣式的各種子集。例如,此代碼將生成已定義段落樣式的列表:
>>> fromdocx.enum.styleimportWD_STYLE_TYPE
>>> styles = document.styles
>>> paragraph_styles = [
... s for s in styles if s.type == WD_STYLE_TYPE.PARAGRAPH
... ]
>>> forstyle in paragraph_styles:
... print(style.name)
...
Normal
Body Text
List Bullet
應用樣式
應用樣式
word的paragraph對象,每個有style屬性。將樣式對象分配給此屬性將應用該樣式:
>>> document = Document()
>>> paragraph = document.add_paragraph()
>>> paragraph.style
<docx.styles.style._ParagraphStyle object at <0x11a7c4c50>
>>> paragraph.style.name
'Normal'
>>> paragraph.style = document.styles['Heading 1']
>>> paragraph.style.name
'Heading 1'
樣式名稱也可以直接分配,在這種情況下python-docx將為您執行查找:
>>> paragraph.style = 'List Bullet'
>>> paragraph.style
<docx.styles.style._ParagraphStyle object at <0x10a7c4f84>
>>> paragraph.style.name
'List Bullet'
也可以使用樣式對象或其名稱在創建時應用樣式:
>>> paragraph = document.add_paragraph(style='Body Text')
>>> paragraph.style.name
'Body Text'
>>> body_text_style = document.styles['Body Text']
>>> paragraph = document.add_paragraph(style=body_text_style)
>>> paragraph.style.name
'Body Text'
添加或刪除樣式
通過指定唯一名稱和樣式類型,可以向文檔添加新樣式:
>>> fromdocx.enum.styleimportWD_STYLE_TYPE
>>> styles = document.styles
>>> style = styles.add_style('Citation', WD_STYLE_TYPE.PARAGRAPH)
>>> style.name
'Citation'
>>> style.type
PARAGRAPH (1)
使用該base_style屬性指定新樣式應從以下格式繼承格式設置的樣式:
>>> style.base_style
None
>>> style.base_style = styles['Normal']
>>> style.base_style
<docx.styles.style._ParagraphStyle object at 0x10a7a9550>
>>> style.base_style.name
'Normal'
只需調用其方法即可從文檔中刪除樣式 :
>>> styles = document.styles
>>> len(styles)
10
>>> styles['Citation'].delete()
>>> len(styles)
9
注意
該Style.delete()方法從文檔中刪除樣式的定義。它不會影響應用該樣式的文檔中的內容。具有未在文檔中定義的樣式的內容使用該內容對象的默認樣式來呈現,例如在段落的情況下為"正常"。
定義字符格式
字符,段落和表格樣式都可以指定要應用於具有該樣式的內容的字符格式。可以在樣式中指定可以直接應用於文本的所有字符格式。示例包括字體字體和大小,粗體,斜體和下劃線。
這三種樣式類型中的每一種都具有 提供對象訪問的屬性。樣式的對象提供了獲取和設置該樣式的字符格式的屬性。
這裡提供了幾個例子。有關完整的可用屬性集,請參閱API文檔。
可以像這樣訪問樣式的字體:
>>> fromdocximportDocument
>>> document = Document()
>>> style = document.styles['Normal']
>>> font = style.font
字體和大小設置如下:
>>> fromdocx.sharedimportPt
>>> font.name = 'Calibri'
>>> font.size = Pt(12)
許多字體屬性是三值,這意味著他們可以採取的價值觀 True,False和None。True表示該物業處於"開啟"狀態,表示該物業處於False"關閉"狀態。從概念上講,該None值意味著"繼承"。由於樣式存在於繼承層次結構中,因此能夠在層次結構中的正確位置指定屬性非常重要,通常儘可能遠離層次結構。例如,如果所有標題都應該是Arial字體,那麼在標題1 樣式上設置該屬性並使標題2從標題1繼承更有意義。
大膽和斜體都是三態屬性,如全帽,刪除線,上標等等。有關完整列表,請參閱API文檔:
>>> font.bold, font.italic
(None, None)
>>> font.italic = True
>>> font.italic
True
>>> font.italic = False
>>> font.italic
False
>>> font.italic = None
>>> font.italic
None
下劃線是一種特殊情況。它是三態屬性和枚舉值屬性的混合體。True意味著單一下劃線,是迄今為止最常見的。False意味著沒有下劃線,但None如果不需要下劃線,則更經常是正確的選擇,因為很少從基礎樣式繼承下劃線。其他形式的下劃線(如double或dashed)是使用枚舉的成員指定的:
>>> font.underline
None
>>> font.underline = True
>>> # or perhaps
>>> font.underline = WD_UNDERLINE.DOT_DASH
定義段落格式
段落樣式和表格樣式都允許指定段落格式。這些樣式通過其屬性提供對對象的訪問。
段落格式包括布局行為,例如對齊,縮進,前後空格,之前的分頁控制。有關可用屬性的完整列表,請參閱該對象的API文檔頁面。
下面是一個如何創建一個段落樣式的示例,該段落樣式具有1/4英寸的懸掛縮進,上方12個點的間距:
>>> fromdocx.enum.styleimportWD_STYLE_TYPE
>>> fromdocx.sharedimportInches, Pt
>>> document = Document()
>>> style = document.styles.add_style('Indent', WD_STYLE_TYPE.PARAGRAPH)
>>> paragraph_format = style.paragraph_format
>>> paragraph_format.left_indent = Inches(0.25)
>>> paragraph_format.first_line_indent = Inches(-0.25)
>>> paragraph_format.space_before = Pt(12)
>>> paragraph_format.widow_control = True
使用段落特定的樣式屬性
段落樣式具有一個 屬性,該屬性指定要應用於在該樣式的段落之後插入的新段落的樣式。當樣式通常只在序列中出現一次(例如標題)時,這非常有用。在這種情況下,完成標題後,段落樣式可以自動設置回主體樣式。
在最常見的情況(正文段落)中,後續段落應該與當前段落具有相同的樣式。如果未指定下一個段落樣式,則默認情況下通過應用相同樣式來處理此情況。
以下是如何將標題1樣式的下一個段落樣式更改為正文文本的示例 :
>>> fromdocximportDocument
>>> document = Document()
>>> styles = document.styles
>>> styles['Heading 1'].next_paragraph_style = styles['Body Text']
可以通過分配None或樣式本身來恢復默認行為:
>>> heading_1_style = styles['Heading 1']
>>> heading_1_style.next_paragraph_style.name
'Body Text'
>>> heading_1_style.next_paragraph_style = heading_1_style
>>> heading_1_style.next_paragraph_style.name
'Heading 1'
>>> heading_1_style.next_paragraph_style = None
>>> heading_1_style.next_paragraph_style.name
'Heading 1'
控制樣式在Word UI中的顯示方式
樣式的屬性分為兩類,行為屬性 和格式屬性。其行為屬性控制樣式在Word UI中出現的時間和位置。其格式設置屬性確定應用樣式的內容的格式,例如字體的大小及其段落縮進。
一種風格有五種行為屬性:
·
·
·
·
·
有關這些行為屬性如何交互的說明,請參閱" 的" 部分,以確定樣式在Word UI中的顯示時間和位置。
該priority屬性採用整數值。其他四種樣式行為屬性是三態,這意味著它們可以取值True(on),False(off)或None(inherit)。
在樣式庫中顯示樣式
以下代碼將使"正文文本"段落樣式首先顯示在樣式庫中:
>>> fromdocximportDocument
>>> document = Document()
>>> style = document.styles['Body Text']
>>> style.hidden = False
>>> style.quick_style = True
>>> style.priorty = 1
從樣式庫中刪除樣式
此代碼將從樣式庫中刪除"正常"段落樣式,但允許它保留在推薦列表中:
>>> style = document.styles['Normal']
>>> style.hidden = False
>>> style.quick_style = False
使用潛在樣式
有關如何定義尚未在.docx文件的styles.xml部分中定義的內置樣式的行為屬性的說明, 請參閱的和部分 。
訪問文檔中的潛在樣式
可以從styles對象訪問文檔中的潛在樣式:
>>> document = Document()
>>> latent_styles = document.styles.latent_styles
一個對象支持len(),迭代和樣式名字典式的訪問:
>>> len(latent_styles)
161
>>> latent_style_names = [ls.name forls in latent_styles]
>>> latent_style_names
['Normal', 'Heading 1', 'Heading 2', ... 'TOC Heading']
>>> latent_quote = latent_styles['Quote']
>>> latent_quote
<docx.styles.latent.LatentStyle object at 0x10a7c4f50>
>>> latent_quote.priority
29
更改潛在樣式默認值
該對象還提供對當前文檔中內置樣式的默認行為屬性的訪問。這些默認值為 定義的任何未定義屬性以及沒有明確潛在樣式定義的內置樣式的所有行為屬性提供值。有關完整可用屬性集的對象,請參閱API文檔 :
>>> latent_styles.default_to_locked
False
>>> latent_styles.default_to_locked = True
>>> latent_styles.default_to_locked
True
添加潛在的樣式定義
可以使用方法添加新的潛在樣式 。此代碼為內置樣式"List Bullet"添加了一種新的潛在樣式,將其設置為顯示在樣式庫中:
>>> latent_style = latent_styles['List Bullet']
KeyError: no latent style with name 'List Bullet'
>>> latent_style = latent_styles.add_latent_style('List Bullet')
>>> latent_style.hidden = False
>>> latent_style.priority = 2
>>> latent_style.quick_style = True
刪除潛在的樣式定義
可以通過調用其delete()方法刪除潛在樣式定義 :
>>> latent_styles['Light Grid']
<docx.styles.latent.LatentStyle object at 0x10a7c4f50>
>>> latent_styles['Light Grid'].delete()
>>> latent_styles['Light Grid']
KeyError: no latent style with name 'Light Grid'
有關樣式的介紹,就暫時到這裡,老鐵們有什麼意見和需求請儘管提,覺得不錯的點個大讚。