Python: Replace or Extract Images in Excel

Images in Excel can enhance data visualization and help convey information effectively. Apart from inserting/deleting images in Excel with Spire.XLS for Python, you can also use the library to replace existing images with new ones, or extract images for reuse or backup. This article will demonstrate how to replace or extract images in Excel in 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

Replace Images in Excel with Python

To replace a picture in Excel, you can load a new picture and then set it as the value of the ExcelPicture.Picture property. The following are the detailed steps to replace an Excel image with another one.

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Get a specified picture from the worksheet using Worksheet.Pictures[] property.
  • Load an image and then replace the original picture with it using ExcelPicture.Picture property.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook instance 
workbook = Workbook()

# Load an Excel file
workbook.LoadFromFile ("ExcelImg.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Get the first picture from the worksheet
excelPicture = sheet.Pictures[0]
            
# Replace the picture with another one 
excelPicture.Picture = Image.FromFile("logo.png")

# Save the result file
workbook.SaveToFile("ReplaceImage.xlsx", ExcelVersion.Version2016)

Python: Replace or Extract Images in Excel

Extract Images from Excel with Python

Spire.XLS for Python provides the ExcelPicture.Picture.Save() method to save the images in Excel to a specified file path. The following are the detailed steps to extract all images in an Excel worksheet at once.

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Loop through to get all pictures in the worksheet using Worksheet.Pictures property.
  • Extract pictures and save them to a specified file path using ExcelPicture.Picture.Save() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook instance
workbook = Workbook()

# Load an Excel file
workbook.LoadFromFile("Test.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Get all images in the worksheet
for i in range(sheet.Pictures.Count - 1, -1, -1):
    pic = sheet.Pictures[i]

    # Save each image as a PNG file
    pic.Picture.Save("ExtractImages\\Image-{0:d}.png".format(i), ImageFormat.get_Png())

workbook.Dispose()

Python: Replace or Extract Images in 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.