How to Add Header and Footer in Excel with Python (Text & Image)

Casie LiuCasie Liu
4 min read

Headers and footers are commonly used in Excel to display important information such as document titles, page numbers, or dates. If you're looking for a fast and efficient way to add headers and footers in Excel, this guide will walk you through an automated solution using Python. With just a few lines of code, you can insert both text and image headers or footers—no need to manually open and edit each Excel file.

You can add various types of text to the header and footer in Excel files, such as your company name, website URL, contact information, date, or page numbers. These details not only enrich the content of your spreadsheet but also help emphasize its originality and ownership. With free Spire.XLS for Python, a simple and easy-to-use Excel library, you can automatically insert any text into the header or footer—saving both time and effort.

Steps to add a header and a footer in Excel:

  • Create a Workbook instance.

  • Load an Excel file using the Workbook.LoadFromFile() method.

  • Get a worksheet through the Workbook.Worksheets[] property.

  • Add Text to the left header with the PageSetup.LeftHeader property.

  • Add a page number to the center footer by setting PageSetup.CenterFooter property to &P.

  • Add the current date to the right footer by setting PageSetup.RightFooter property to &D.

  • Save the updated Excel file using the Workbook.SaveToFile() method.

This code example demonstrates how to insert text headers and footers in the first Excel worksheet:

from spire.xls import *
from spire.xls.common import *

# Create an object of Workbook class
workbook = Workbook()

# Load a sample file from disk
workbook.LoadFromFile("/Population.xlsx")

# Get the first worksheet of this file
Worksheet = workbook.Worksheets[0]

# Add text to the left header
Worksheet.PageSetup.LeftHeader = "&\"Calibri\"&14 Top 10 Countries by Population"

# Add page number to the center footer
Worksheet.PageSetup.CenterFooter = "&P"

# Add the current date to the right footer
Worksheet.PageSetup.RightFooter = "&D"

# Set the view mode of the sheet
Worksheet.ViewMode = ViewMode.Layout

# Save the result file
workbook.SaveToFile("/AddTextHeaderFooter.xlsx", ExcelVersion.Version2010)
workbook.Dispose()

 Add Header and Footer in Excel Using Python

Sometimes, you may want to insert an image—such as a company logo—into the header or footer of an Excel spreadsheet to make the file more visually appealing and professional. With free Spire.XLS for Python, adding images is quite similar to inserting text, but it involves using the PageSetup.LeftHeaderImage property, which is specifically designed for handling images. Below are the detailed steps and sample code to help you get started.

Steps to add an image header and footer in Excel files:

  • Create an object of the Workbook class.

  • Read an Excel file and get a certain worksheet through the Workbook.Worksheets[] property.

  • Load an image from files and set it as the header and footer using the PageSetup.LeftHeaderImage property.

  • Place the image in the left header section by setting the PageSetup.LeftHeader property to “&G”.

  • Set it as the image source of the center footer by using the PageSetup.CenterFooterImage property.

  • Display the image in the center footer section by setting the PageSetup.CenterFooter property to “&G”.

  • Save the modified Excel file.

The code below demonstrates how to add image-based headers and footers to the first worksheet in an Excel file:

from spire.xls import *

# Create an object of workbook class
workbook = Workbook()

# Load a sample file from disk
workbook.LoadFromFile("/Population.xlsx")

# Get the first sheet of this file
sheet = workbook.Worksheets[0]

# Load an image from disk
image = Stream("/Logo1.png")

# Add the image to the left header
sheet.PageSetup.LeftHeaderImage = image
sheet.PageSetup.LeftHeader = "&G"

# Add the image to the center footer
sheet.PageSetup.CenterFooterImage = image
sheet.PageSetup.CenterFooter = "&G"

# Set the view mode of the sheet
sheet.ViewMode = ViewMode.Layout

# Save the result file
workbook.SaveToFile("/AddImageHeaderFooter.xlsx", ExcelVersion.Version2010)
workbook.Dispose()

To Wrap Up

By the end of the blog, you can easily add headers and footers in Excel files with both text and images. Whether you're adding page numbers, contact details, or a company logo, these enhancements help make your spreadsheets more informative and professional.

Q1: How do I insert a header and Footer?

Go to the “Insert” tab and click “Header & Footer”. Excel will switch to “Page Layout View”, where you can enter custom content. For batch processing, consider using free Spire.XLS for Python to automate the task.

Q2: Why can't I see the header and Footer in Excel?

They’re only visible in “Page Layout View” or “Print Preview”. Switch from “Normal View” to see them, and make sure content is actually added.

Q3: How do I add a header to an entire workbook in Excel?

Right-click a sheet tab, select “Select All Sheets”, then add the header. It will apply to all sheets. You can also automate this with Spire.XLS.

Q4:How do I put headers at the top of each page in Excel?

To repeat row headers on each printed page, go to “Page Layout” > “Print Titles”, and set the row(s) under Rows to repeat at top.

0
Subscribe to my newsletter

Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Casie Liu
Casie Liu