Python: Add a Watermark to Excel

2024-07-23 01:10:21 Written by  support iceblue
Rate this item
(0 votes)

While watermarks are a common design element used in many types of documents to convey ownership, confidentiality, or branding, Microsoft Excel does not provide a built-in watermark feature. However, there are workaround methods to achieve a watermark effect in Excel spreadsheets.

One approach is to add an image to the header or footer of the worksheet, and another approach is to add an image to a worksheet as the background. In this article, you will learn how to add a header or background image watermark to Excel in Python using Spire.XLS for Python.

Install Spire.XLS for Python

This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.XLS

If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows

Header vs Background Image Watermarks

Header Image Watermark:

Advantages:

  • The watermark is preserved on the printed sheet, ensuring it appears in the final output.

Disadvantages:

  • The watermark is invisible under the "Normal" view mode in Excel, only becoming visible in "Page Layout" or "Page Break Preview" views.
  • To centrally position the watermark graphic on the Excel page, you need to carefully adjust the white margins, especially on the top and left sides of the image.

Background Image Watermark:

Advantages:

  • The watermark image covers the entire worksheet area, providing a consistent background appearance.

Disadvantages:

  • The watermark is not preserved on the printed sheet, meaning it will not appear in the final printed output.

Add a Watermark to Excel Using a Header Image in Python

Spire.XLS for Python offers the PageSetup class to control various settings related to the appearance and layout of the printed worksheet. Under this class, you can find the CenterHeader and CenterHeaderImage properties, allowing you set an image for the center section of the header.

Here are the steps to add a header image watermark to Excel using Python.

  • Create a Workbook object.
  • Load an Excel document from a give file path.
  • Load an image while initialing the Stream class.
  • Get a specific worksheet from the workbook.
  • Add an image field to the header center by setting Worksheet.PageSetup.CenterHeader property to "&G".
  • Apply the image to the header center through Worksheet.PageSetup.CenterHeaderImage property.
  • Save the workbook to a different Excel file.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load an Excel document
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx")

# Load an image file
stream = Stream("C:\\Users\\Administrator\\Desktop\\confidential.png")

# Loop through all worksheets in the file
for i in range(workbook.Worksheets.Count):

    # Get a specific worksheet
    worksheet = workbook.Worksheets[i]

    # Add an image field to the header center
    worksheet.PageSetup.CenterHeader = "&G"

    # Add the image to the header center
    worksheet.PageSetup.CenterHeaderImage = stream

# Save the result file
workbook.SaveToFile("output/AddWatermark.xlsx", ExcelVersion.Version2016)

# Dispose resources
workbook.Dispose()

Python: Add a Watermark to Excel

Add a Watermark to Excel Using a Background Image in Python

The PageSetup class provides the BackgroundImage property to get or set the image for the background. Below are the steps to add a background image watermark to Excel using Python.

  • Create a Workbook object.
  • Load an Excel document from a give file path.
  • Load an image while initialing the Stream class.
  • Get a specific worksheet from the workbook.
  • Apply the image to the worksheet as the background through Worksheet.PageSetup.BackgroundImage property.
  • Save the workbook to a different Excel file.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load an Excel document
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx")

# Load an image file
stream = Stream("C:\\Users\\Administrator\\Desktop\\sample-background.png")

# Loop through all worksheets in the file
for i in range(workbook.Worksheets.Count):

    # Get a specific worksheet
    worksheet = workbook.Worksheets[i]

    # Set the image as the background of the worksheet
    worksheet.PageSetup.BackgoundImage = stream

# Save the result file
workbook.SaveToFile("output/AddWatermark.xlsx", ExcelVersion.Version2016)

# Dispose resources
workbook.Dispose()

Python: Add a Watermark to Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Tuesday, 23 July 2024 01:41