Python Xlsxwriter 简明教程

当使用上述方法打印工作表时, headerfooter 将在纸上生成。打印预览还显示页眉和页脚。两者均使用 set_header()set_footer() 方法进行配置。页眉和页脚字符串通过以下控制字符进行配置 −

Control

Category

Description

&L

Justification

Left

&C

Center

&R

Right

&P

Information

Page number

&N

Total number of pages

&D

Date

&T

Time

&F

File name

&A

Worksheet name

&Z

Workbook path

&fontsize

Font

Font size

&"font,style"

Font name and style

&U

Single underline

&E

Double underline

&S

Strikethrough

&X

Superscript

&Y

Subscript

&[Picture]

Images

Image placeholder

&G

Same as &[Picture]

&&

Misc.

Literal ampersand "&"

Example

以下代码使用 set_header()set_footer() 方法 −

import xlsxwriter

wb = xlsxwriter.Workbook('hello.xlsx')
ws = wb.add_worksheet()

data = [
   ['Anil', 45, 55, 50], ['Ravi', 60, 70, 80],
   ['Kiran', 65, 75, 85],['Karishma', 55, 65, 45]
]

for row in range(len(data)):
   ws.write_row(row,0, data[row])

header1 = '&CTutorialspoint'
footer1 = '&LSimply Easy Learning'

ws.set_landscape()
ws.set_paper(9) #A4 paper
ws.set_header(header1)
ws.set_footer(footer1)

ws.set_column('A:A', 50)

wb.close()

Output

运行以上 Python 代码并打开工作表。从文件菜单中,选择打印选项。在右侧窗格中,显示预览。您应该可以看到页眉和页脚。

header and footer