Python實現(xiàn)添加和自定義設(shè)置Word頁眉頁腳
頁眉和頁腳是 Word 文檔中常用的元素,用于在每一頁的頂部或底部顯示統(tǒng)一的信息,如文檔標題、公司名稱、頁碼等。在處理大量文檔或需要批量添加頁眉頁腳時,手動操作效率較低。使用 Python 可以自動化完成這些任務(wù),提高文檔處理效率。
本文將介紹如何使用 Python 和 Free Spire.Doc 庫在 Word 文檔中添加和設(shè)置頁眉頁腳,包括添加文本、圖片、頁碼,以及設(shè)置奇偶頁不同的頁眉頁腳等常見操作。
環(huán)境準備
首先需要安裝 Spire.Doc 庫:
pip install spire.doc.free
安裝完成后,即可在 Python 代碼中導(dǎo)入并使用相關(guān)功能。
基本概念
在開始編寫代碼之前,需要了解幾個基本概念:
- Section(節(jié)): Word 文檔由一個或多個節(jié)組成,每個節(jié)可以有自己的頁眉頁腳設(shè)置
- Header(頁眉): 位于頁面頂部的區(qū)域
- Footer(頁腳): 位于頁面底部的區(qū)域
- Paragraph(段落): 頁眉頁腳中的內(nèi)容通過段落來組織和顯示
添加基本頁眉頁腳
下面的示例演示如何為 Word 文檔添加包含文本和圖片的頁眉頁腳:
from spire.doc import *
from spire.doc.common import *
# 創(chuàng)建文檔對象并加載文件
document = Document()
document.LoadFromFile("Sample.docx")
# 獲取第一個節(jié)
section = document.Sections[0]
# 獲取頁眉和頁腳對象
header = section.HeadersFooters.Header
footer = section.HeadersFooters.Footer
# 在頁眉中添加段落
headerParagraph = header.AddParagraph()
# 添加圖片到頁眉
headerPicture = headerParagraph.AppendPicture("Header.png")
# 設(shè)置圖片大小和位置
headerPicture.Width = 40
headerPicture.Height = 40
headerPicture.TextWrappingStyle = TextWrappingStyle.InFrontOfText
headerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left
headerPicture.VerticalAlignment = ShapeVerticalAlignment.Outside
# 添加文本到頁眉
text = headerParagraph.AppendText("公司內(nèi)部文檔")
text.CharacterFormat.FontName = "微軟雅黑"
text.CharacterFormat.FontSize = 10
text.CharacterFormat.Italic = True
# 設(shè)置頁眉段落右對齊
headerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right
# 添加下邊框線
headerParagraph.Format.Borders.Bottom.BorderType = BorderStyle.Single
headerParagraph.Format.Borders.Bottom.Space = 0.05
# 在頁腳中添加段落
footerParagraph = footer.AddParagraph()
# 添加頁碼字段
footerParagraph.AppendField("page number", FieldType.FieldPage)
footerParagraph.AppendText(" / ")
footerParagraph.AppendField("number of pages", FieldType.FieldNumPages)
# 設(shè)置頁腳段落居中對齊
footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center
# 保存文檔
document.SaveToFile("HeaderAndFooter.docx", FileFormat.Docx)
document.Close()
以下是生成文檔的頁眉頁腳:

這段代碼首先加載一個現(xiàn)有的 Word 文檔,然后獲取文檔的第一個節(jié)。通過 HeadersFooters 屬性訪問頁眉和頁腳對象,使用 AddParagraph() 方法添加段落,再通過 AppendPicture() 和 AppendText() 方法添加圖片和文本內(nèi)容。
設(shè)置奇偶頁不同的頁眉頁腳
在正式文檔中,經(jīng)常需要為奇數(shù)頁和偶數(shù)頁設(shè)置不同的頁眉頁腳,例如奇數(shù)頁顯示章節(jié)標題,偶數(shù)頁顯示文檔名稱。下面的代碼演示如何實現(xiàn)這一功能:
from spire.doc import *
from spire.doc.common import *
# 加載文檔
doc = Document()
doc.LoadFromFile("MultiplePages.docx")
# 獲取第一個節(jié)
section = doc.Sections[0]
# 啟用奇偶頁不同的頁眉頁腳
section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = True
# 添加奇數(shù)頁頁眉
oddHeaderPara = section.HeadersFooters.OddHeader.AddParagraph()
oddHeaderText = oddHeaderPara.AppendText("奇數(shù)頁頁眉 - 章節(jié)標題")
oddHeaderPara.Format.HorizontalAlignment = HorizontalAlignment.Center
oddHeaderText.CharacterFormat.FontName = "微軟雅黑"
oddHeaderText.CharacterFormat.FontSize = 10
# 添加偶數(shù)頁頁眉
evenHeaderPara = section.HeadersFooters.EvenHeader.AddParagraph()
evenHeaderText = evenHeaderPara.AppendText("偶數(shù)頁頁眉 - 文檔名稱")
evenHeaderPara.Format.HorizontalAlignment = HorizontalAlignment.Center
evenHeaderText.CharacterFormat.FontName = "微軟雅黑"
evenHeaderText.CharacterFormat.FontSize = 10
# 添加奇數(shù)頁頁腳
oddFooterPara = section.HeadersFooters.OddFooter.AddParagraph()
oddFooterText = oddFooterPara.AppendText("奇數(shù)頁頁腳")
oddFooterPara.Format.HorizontalAlignment = HorizontalAlignment.Center
# 添加偶數(shù)頁頁腳
evenFooterPara = section.HeadersFooters.EvenFooter.AddParagraph()
evenFooterText = evenFooterPara.AppendText("偶數(shù)頁頁腳")
evenFooterPara.Format.HorizontalAlignment = HorizontalAlignment.Center
# 保存文檔
doc.SaveToFile("OddAndEvenHeaderFooter.docx", FileFormat.Docx)
doc.Close()
生成的文檔頁眉頁腳如下:

關(guān)鍵步驟是將 DifferentOddAndEvenPagesHeaderFooter 屬性設(shè)置為 True,然后分別通過 OddHeader、EvenHeader、OddFooter、EvenFooter 屬性訪問和設(shè)置奇偶頁的頁眉頁腳。
設(shè)置首頁不同的頁眉頁腳
某些文檔需要首頁使用特殊的頁眉頁腳,或者首頁不顯示頁眉頁腳。可以通過以下方式實現(xiàn):
from spire.doc import *
from spire.doc.common import *
# 加載文檔
doc = Document()
doc.LoadFromFile("Sample.docx")
# 獲取第一個節(jié)
section = doc.Sections[0]
# 啟用首頁不同的頁眉頁腳
section.PageSetup.DifferentFirstPageHeaderFooter = True
# 設(shè)置首頁頁眉(可以為空以實現(xiàn)首頁不顯示頁眉)
firstHeaderPara = section.HeadersFooters.FirstPageHeader.AddParagraph()
firstHeaderText = firstHeaderPara.AppendText("首頁專用頁眉")
firstHeaderPara.Format.HorizontalAlignment = HorizontalAlignment.Center
# 設(shè)置常規(guī)頁眉(用于除首頁外的其他頁面)
headerPara = section.HeadersFooters.Header.AddParagraph()
headerText = headerPara.AppendText("常規(guī)頁眉")
headerPara.Format.HorizontalAlignment = HorizontalAlignment.Right
# 保存文檔
doc.SaveToFile("FirstPageHeader.docx", FileFormat.Docx)
doc.Close()
生成的文檔頁眉頁腳如下:

通過設(shè)置 DifferentFirstPageHeaderFooter 屬性為 True,然后使用 FirstPageHeader 和 FirstPageFooter 屬性設(shè)置首頁的頁眉頁腳。
調(diào)整頁眉頁腳高度
頁眉頁腳的高度可以通過 HeaderDistance 和 FooterDistance 屬性進行調(diào)整:
from spire.doc import *
from spire.doc.common import *
# 加載文檔
doc = Document()
doc.LoadFromFile("Sample.docx")
# 獲取第一個節(jié)
section = doc.Sections[0]
# 設(shè)置頁眉距離頁面頂部的距離(單位:磅)
section.PageSetup.HeaderDistance = 50
# 設(shè)置頁腳距離頁面底部的距離(單位:磅)
section.PageSetup.FooterDistance = 50
# 添加頁眉內(nèi)容
header = section.HeadersFooters.Header
headerPara = header.AddParagraph()
headerPara.AppendText("調(diào)整高度后的頁眉")
# 保存文檔
doc.SaveToFile("AdjustedHeight.docx", FileFormat.Docx)
doc.Close()
生成的文檔如下:

實用技巧
添加頁碼格式
頁碼是頁腳中最常見的元素,可以添加各種格式的頁碼:
footerParagraph = footer.AddParagraph()
# 添加"第 X 頁"格式
footerParagraph.AppendText("第 ")
footerParagraph.AppendField("page number", FieldType.FieldPage)
footerParagraph.AppendText(" 頁")
# 添加"第 X 頁,共 Y 頁"格式
footerParagraph.AppendText("第 ")
footerParagraph.AppendField("page number", FieldType.FieldPage)
footerParagraph.AppendText(" 頁,共 ")
footerParagraph.AppendField("number of pages", FieldType.FieldNumPages)
footerParagraph.AppendText(" 頁")
添加分隔線
為頁眉或頁腳添加分隔線可以增強視覺效果:
# 頁眉底部添加分隔線 headerParagraph.Format.Borders.Bottom.BorderType = BorderStyle.Single headerParagraph.Format.Borders.Bottom.Color = Color.get_Gray() headerParagraph.Format.Borders.Bottom.LineWidth = 0.5 # 頁腳頂部添加分隔線 footerParagraph.Format.Borders.Top.BorderType = BorderStyle.Single footerParagraph.Format.Borders.Top.Color = Color.get_Gray() footerParagraph.Format.Borders.Top.LineWidth = 0.5
設(shè)置圖片大小和位置
當(dāng)在頁眉頁腳中添加圖片時,可以精確控制圖片的位置:
picture = headerParagraph.AppendPicture("Logo.png")
# 設(shè)置圖片大小
headerPicture.Width = 40
headerPicture.Height = 40
# 設(shè)置文字環(huán)繞方式
picture.TextWrappingStyle = TextWrappingStyle.Behind
# 設(shè)置水平位置
picture.HorizontalOrigin = HorizontalOrigin.Page
picture.HorizontalAlignment = ShapeHorizontalAlignment.Left
# 設(shè)置垂直位置
picture.VerticalOrigin = VerticalOrigin.Page
picture.VerticalAlignment = ShapeVerticalAlignment.Top
總結(jié)
本文介紹了使用 Python 在 Word 文檔中添加和設(shè)置頁眉頁腳的多種方法,包括添加文本和圖片、設(shè)置奇偶頁不同的頁眉頁腳、首頁特殊處理、調(diào)整高度等常見操作。通過這些技術(shù),可以高效地批量處理文檔,實現(xiàn)頁眉頁腳的自動化設(shè)置。
在實際應(yīng)用中,可以根據(jù)具體需求組合使用這些方法,例如為正式報告創(chuàng)建包含公司 Logo、文檔標題和頁碼的專業(yè)頁眉頁腳,或者為書籍排版設(shè)置奇偶頁不同的頁眉頁腳樣式。掌握這些技巧能夠顯著提升文檔處理的效率和規(guī)范性。
以上就是Python實現(xiàn)添加和自定義設(shè)置Word頁眉頁腳的詳細內(nèi)容,更多關(guān)于Python Word頁眉頁腳的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python+FFmpeg實現(xiàn)視頻自動化處理的完整指南
本文總結(jié)了一套在Python中使用subprocess.run調(diào)用FFmpeg進行視頻自動化處理的解決方案,涵蓋了跨平臺硬件加速、中間素材處理和終極合并的邏輯,包括硬件編碼參數(shù)映射、精確變速剪輯、多流合并等,需要的朋友可以參考下2025-12-12
python攝氏度和華氏度溫度的轉(zhuǎn)換實現(xiàn)方式
文章講述了作者通過觀察和思考,對生活中的某一現(xiàn)象進行了深入的分析和探討,最終得出了自己的見解和感悟2025-11-11
python3.8中關(guān)于sklearn問題(win10)
這篇文章主要介紹了python3.8中關(guān)于sklearn問題(win10),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

