在 Word 文档中,当页眉页脚的内容过多时,我们可以调整其高度以便完整的显示其内容;当其他文档需要相同的页眉页脚时,我们可以直接复制页眉页脚到想用的文档,这样可以保证页眉页脚的内容和样式等一致;当打印时需要节省打印纸的空间或共享文档时需要避免隐秘信息被公开,我们可以删除页眉页脚。在本文中,我们将介绍如何使用 Spire.Doc for Python 在 Python 中调整、复制或删除 Word 文档的页眉页脚。
安装 Spire.Doc for Python
本教程需要 Spire.Doc for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.Doc
如果您不确定如何安装,请参考此教程: 如何在 Windows 中安装 Spire.Doc for Python
Python 调整 Word 文档中页眉页脚的高度
Spire.Doc for Python 提供了 Section.PageSetup.HeaderDistance 和 Section.PageSetup.FooterDistance 属性来设置 Word 文档的页眉和页脚的高度,这两个属性的值的单位是磅(pt)。以下是详细步骤:
- 创建一个 Document 类的对象。
- 使用 Document.LoadFromFile() 方法加载一个 Word 文档。
- 使用 Document.Sections[] 属性获取 Word 文档的第一个章节。
- 使用 Section.PageSetup.HeaderDistance 和 Section.PageSetup.FooterDistance 属性分别设置页眉和页脚的高度。
- 使用 Document.SaveToFile() 方法保存结果文档。
- Python
from spire.doc import *
from spire.doc.common import *
# 定义输入和输出文件名
inputFile = "页眉页脚.docx"
outputFile = "调整页眉页脚高度.docx"
# 创建一个文档对象
doc = Document()
# 从文件加载文档
doc.LoadFromFile(inputFile)
# 获取文档的第一个节(section)
section = doc.Sections[0]
# 调整页眉和页脚的高度为100
section.PageSetup.HeaderDistance = 100
section.PageSetup.FooterDistance = 100
# 将修改后的文档保存到新文件中
doc.SaveToFile(outputFile, FileFormat.Docx)
doc.Close()
Python 设置页面边框是否包含页眉页脚
Spire.Doc for Python 提供了 Section.PageSetup.PageBorderIncludeHeader 和 Section.PageSetup.PageBorderIncludeFooter 属性来设置页面边框是否包含页眉页脚。以下是详细步骤:
- 创建一个 Document 类的对象。
- 使用 Document.AddSection() 方法添加一个节。
- 使用 Section.PageSetup.Borders 的属性来设置边框样式。
- 分别设置页眉页脚的内容
- 使用 Section.PageSetup.PageBorderIncludeHeader 和 Section.PageSetup.PageBorderIncludeFooter 属性分别设置页眉页脚是否被包含在页面边框中。
- 使用 Document.SaveToFile() 方法保存结果文档。
- Python
from spire.doc import *
from spire.doc.common import *
# 定义输出文件名
outputFile = "页面边框环绕.docx"
# 创建一个文档对象
doc = Document()
# 添加一个节(section)
section = doc.AddSection()
# 设置页面边框样式为波浪线
section.PageSetup.Borders.BorderType(BorderStyle.Wave)
section.PageSetup.Borders.Color(Color.get_Green())
section.PageSetup.Borders.Left.Space = 20.0
section.PageSetup.Borders.Right.Space = 20.0
# 添加页眉
paragraph1 = section.HeadersFooters.Header.AddParagraph()
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Right
headerText = paragraph1.AppendText("页眉未包含在页面边框中")
headerText.CharacterFormat.FontName = "宋体"
headerText.CharacterFormat.FontSize = 20.0
headerText.CharacterFormat.Bold = True
# 添加页脚
paragraph2 = section.HeadersFooters.Footer.AddParagraph()
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Left
footerText = paragraph2.AppendText("页脚包含在页面边框中")
footerText.CharacterFormat.FontName = "微软雅黑"
footerText.CharacterFormat.FontSize = 20.0
footerText.CharacterFormat.Bold = True
# 设置页面边框不包含页眉,设置页眉距离为40,页面边框包含页脚,设置页脚距离为40
section.PageSetup.PageBorderIncludeHeader = False
section.PageSetup.HeaderDistance = 40.0
section.PageSetup.PageBorderIncludeFooter = True
section.PageSetup.FooterDistance = 40.0
# 将文档保存到文件
doc.SaveToFile(outputFile, FileFormat.Docx)
doc.Close()
Python 复制页眉页脚内容到其他文档
当您创建多个密切相关的 Word 文档时,您可能希望将其中一个文档的页眉或页脚用作其他文档的页眉或页脚。例如,在创建内部文档时,将公司徽标、名称或其他材料放在页眉中,这时只需创建一次页眉,然后将页眉复制到其他地方即可。详细的实现步骤如下:
- 创建两个 Document 类的对象。
- 使用 Document.LoadFromFile() 方法分别加载源 Word 文档和需要添加页眉页脚的示例文档。
- 使用 Document.Sections[0].HeadersFooters.Header 和 Document.Sections[0].HeadersFooters.Footer 分别获取源 Word 文档第一个章节的页眉和页脚。
- 遍历示例文档的章节,分别将源 Word 文档的页眉页脚内容复制到示例文档中。
- 使用 Document.SaveToFile() 方法保存添加了页眉页脚的结果文档。
- Python
from spire.doc import *
from spire.doc.common import *
# 定义输入和输出文件名
inputFile = "页眉页脚.docx"
inputFile_1 = "示例文档.docx"
outputFile = "复制页眉页脚.docx"
# 创建两个文档对象并加载文档
doc1 = Document()
doc1.LoadFromFile(inputFile)
header = doc1.Sections[0].HeadersFooters.Header
footer = doc1.Sections[0].HeadersFooters.Footer
doc2 = Document()
doc2.LoadFromFile(inputFile_1)
# 复制页眉
for i in range(doc2.Sections.Count):
section = doc2.Sections.get_Item(i)
for h in range(header.ChildObjects.Count):
obj = header.ChildObjects.get_Item(h)
section.HeadersFooters.Header.ChildObjects.Add(obj.Clone())
# 复制页脚
for i in range(doc2.Sections.Count):
section = doc2.Sections.get_Item(i)
for f in range(footer.ChildObjects.Count):
obj = footer.ChildObjects.get_Item(f)
section.HeadersFooters.Footer.ChildObjects.Add(obj.Clone())
# 将修改后的文档保存到新文件中
doc2.SaveToFile(outputFile, FileFormat.Docx)
doc2.Close()
Python 删除页眉页脚内容
Word 文档的页眉页脚内容可能会包含作者姓名、文档用途等隐秘信息,当共享文档或打印文档时可能需要删除这些内容以保护隐私或节省打印纸的空间。删除页眉页脚内容的详细步骤如下:
- 创建一个 Document 类的对象。
- 使用 Document.LoadFromFile() 方法加载一个 Word 文档。
- 使用 Document.Sections[] 方法获取第一个章节。
- 分别获取章节的页眉和页脚,包括首页、奇数页、偶数页,然后使用 Header.ChildObjects.Clear() 和 Footer.ChildObjects.Clear() 删除页眉页脚的内容。
- 使用 Document.SaveToFile() 方法保存结果文档。
- Python
from spire.doc import *
from spire.doc.common import *
# 定义输入和输出文件名
inputFile = "页眉页脚.docx"
outputFile = "删除页眉页脚.docx"
# 创建一个文档对象
doc = Document()
# 从文件加载文档
doc.LoadFromFile(inputFile)
# 获取文档的第一个节(section)
section = doc.Sections[0]
# 清空第一页、奇数页和偶数页的页眉
header = None
header = section.HeadersFooters[HeaderFooterType.HeaderFirstPage]
if header is not None:
header.ChildObjects.Clear()
header = section.HeadersFooters[HeaderFooterType.HeaderOdd]
if header is not None:
header.ChildObjects.Clear()
header = section.HeadersFooters[HeaderFooterType.HeaderEven]
if header is not None:
header.ChildObjects.Clear()
# 清空第一页、奇数页和偶数页的页脚
footer = None
footer = section.HeadersFooters[HeaderFooterType.FooterFirstPage]
if footer is not None:
footer.ChildObjects.Clear()
footer = section.HeadersFooters[HeaderFooterType.FooterOdd]
if footer is not None:
footer.ChildObjects.Clear()
footer = section.HeadersFooters[HeaderFooterType.FooterEven]
if footer is not None:
footer.ChildObjects.Clear()
# 将修改后的文档保存到新文件中
doc.SaveToFile(outputFile, FileFormat.Docx)
doc.Close()
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。