📜  在Python .docx 模块中使用页眉和页脚

📅  最后修改于: 2022-05-13 01:55:10.574000             🧑  作者: Mango

在Python .docx 模块中使用页眉和页脚

先决条件:使用 .docx 模块

Word 文档包含包装在三个对象级别内的格式化文本。最低级别运行对象、中间级别段落对象和最高级别文档对象。因此,我们无法使用普通文本编辑器处理这些文档。但是,我们可以使用 python-docx 模块在Python中操作这些 word 文档。安装这个模块的pip命令是:

pip install python-docx

Python docx 模块允许用户通过操作现有文档或创建新的空文档并对其进行操作来操作文档。它是一个强大的工具,因为它可以帮助您在非常大的范围内操作文档。页眉和页脚都是节的一部分,因此每个节都可以有不同的页眉和页脚。页眉是文档的重要部分,因为它包含有关发布者希望在每个页面上显示的文档的重要信息。

简单标题

header 对象总是出现在部分或页面的顶部,可以通过使用section.header来调用。每个新标题都包含一个空段落,并且可以像文档的其余部分一样对其进行编辑。要添加内容,我们使用段落的.text方法。

示例 1:在 Word 文档中添加标题。

Python3
# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Calling the header
header = section.header
 
# Calling the paragraph already present in
# the header section
header_para = header.paragraphs[0]
 
# Adding text in the header
header_para.text = "This is a header..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Selecting the header
header = section.header
 
# Selecting the paragraph already present in
# the header section
header_para = header.paragraphs[0]
 
# Adding the left zoned header
header_para.text = "This is Left Zoned Header..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Selecting the header
header = section.header
 
# Selecting the paragraph already present in
# the header section
header_para = header.paragraphs[0]
 
# Adding the centred zoned header
header_para.text = "\tThis is Centred Zoned Header..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Selecting the header
header = section.header
 
# Selecting the paragraph already present in
# the header section
header_para = header.paragraphs[0]
 
# Adding the right zoned header
header_para.text = "\t\tThis is Right Zoned Header..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Calling the footer
footer = section.footer
 
# Calling the paragraph already present in
# the footer section
footer_para = footer.paragraphs[0]
 
# Adding text in the footer
footer_para.text = "This is a footer..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Calling the footer
footer = section.footer
 
# Calling the paragraph already present in
# the footer section
footer_para = footer.paragraphs[0]
 
# Adding the left zoned footer
footer_para.text = "This is the left zoned footer..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Calling the footer
footer = section.footer
 
# Calling the paragraph already present in
# the footer section
footer_para = footer.paragraphs[0]
 
# Adding the centered zoned footer
footer_para.text = "\tThis is the centered zoned footer..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


Python3
# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Calling the footer
footer = section.footer
 
# Calling the paragraph already present in
# the footer section
footer_para = footer.paragraphs[0]
 
# Adding the right zoned footer
footer_para.text = "\t\tThis is the right zoned footer..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')



输出:

分区标题

通过使用此模块,您还可以在 Word 文档中添加分区标题。要添加分区标题,我们使用制表符,即“ \t ”。左、中、右三个区域。默认情况下,文本位于左侧区域,如果我们在文本上使用单个 ' \t ',那么它将移动到中心区域,并分别在右侧区域再添加一个 ' \t '。

示例 2:向 Word 文档添加左分区标题。

蟒蛇3

# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Selecting the header
header = section.header
 
# Selecting the paragraph already present in
# the header section
header_para = header.paragraphs[0]
 
# Adding the left zoned header
header_para.text = "This is Left Zoned Header..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


输出:

示例 3:向 Word 文档添加居中的分区标题。

蟒蛇3

# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Selecting the header
header = section.header
 
# Selecting the paragraph already present in
# the header section
header_para = header.paragraphs[0]
 
# Adding the centred zoned header
header_para.text = "\tThis is Centred Zoned Header..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


输出:

示例 4:向 Word 文档添加右分区标题。

蟒蛇3

# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Selecting the header
header = section.header
 
# Selecting the paragraph already present in
# the header section
header_para = header.paragraphs[0]
 
# Adding the right zoned header
header_para.text = "\t\tThis is Right Zoned Header..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


输出:

简单的页脚

页脚对象始终位于部分或页面的底部,可以通过使用section.footer调用。每个新页脚都包含一个空段落,并且可以像文档的其余部分一样对其进行编辑。为了添加内容,我们使用段落的.text 方法

示例 1:向 Word 文档添加页脚。

蟒蛇3

# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Calling the footer
footer = section.footer
 
# Calling the paragraph already present in
# the footer section
footer_para = footer.paragraphs[0]
 
# Adding text in the footer
footer_para.text = "This is a footer..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


输出:

分区页脚

要添加分区页脚,我们将使用制表符,即“ \t ”。左、中、右三个区域。默认情况下,文本位于左侧区域,如果我们在文本上使用单个 ' \t ',那么它将筛选到中心区域,并分别在右侧区域再添加一个 ' \t '。

示例 2:向 Word 文档添加左分区页脚。

蟒蛇3

# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Calling the footer
footer = section.footer
 
# Calling the paragraph already present in
# the footer section
footer_para = footer.paragraphs[0]
 
# Adding the left zoned footer
footer_para.text = "This is the left zoned footer..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


输出:

示例 3:向 Word 文档添加中心分区页脚。

蟒蛇3

# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Calling the footer
footer = section.footer
 
# Calling the paragraph already present in
# the footer section
footer_para = footer.paragraphs[0]
 
# Adding the centered zoned footer
footer_para.text = "\tThis is the centered zoned footer..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


输出:

示例 4:向 Word 文档添加右分区页脚。

蟒蛇3

# Import docx NOT python-docx
import docx
 
# Create an instance of a word document
doc = docx.Document()
 
# Choosing the top most section of the page
section = doc.sections[0]
 
# Calling the footer
footer = section.footer
 
# Calling the paragraph already present in
# the footer section
footer_para = footer.paragraphs[0]
 
# Adding the right zoned footer
footer_para.text = "\t\tThis is the right zoned footer..."
 
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
 
# Now save the document to a location
doc.save('gfg.docx')


输出: