# How to Replace and Extract Images from Excel in Python [Simple Tips]

Managing images in Excel spreadsheets is a common task when working with product catalogs, reports, or financial statements. Whether you need to replace outdated images or extract them for other uses, Python provides an efficient way to automate these processes. By leveraging Python, you can save time and streamline your workflow. In this article, we’ll explore how to **replace and extract images from Excel tables using Python**, helping you keep your workbooks updated and well-organized.

## Python Libraries to Replace and Extract Images in Excel Files

To efficiently manage images in Excel using Python, leveraging specialized libraries is essential. Open-source options like openpyxl can handle both replacing and extracting images. However, when replacing images, it removes the existing image before inserting a new one rather than performing a direct replacement. XlsxWriter supports inserting and replacing images but does not provide functionality for extracting them from Excel sheets.

For a more comprehensive solution, enterprise-level Excel components like [Spire.XLS](https://www.e-iceblue.com/Introduce/xls-for-python.html) and Aspose.Cells offer greater flexibility. Beyond image management, they support advanced tasks such as [Excel-to-PDF](https://casie.hashnode.dev/how-to-save-excel-as-pdf-and-vice-versa-in-python) and CSV conversion, automatic row and column adjustments, and more.

Considering ease of use and functionality, this article will demonstrate how to replace and extract images in Excel using Spire.XLS. Whether you're a beginner or an experienced user, you’ll find the process straightforward. You can install it using the following pip command: `pip install spire.XLS`.

## How to Replace Images in Excel Spreadsheets

When an image in an Excel sheet is incorrect or outdated, replacing it is essential to maintain a professional and up-to-date document—especially in time-sensitive files like product catalogs. Fortunately, Spire.XLS provides the *ExcelPicture.Picture* property, allowing you to directly and accurately replace an existing image without the need for manual adjustments.

Below, we’ll walk through the step-by-step process to efficiently replace images in an Excel sheet using Python.

Steps to replace an image in Excel worksheets:

* Create an instance of the **Workbook** class.
    
* Read a source Excel file using the **Workbook.LoadFromFile()** method.
    
* Get a specified worksheet through the **Workbook.Worksheets\[\]** rpoperty.
    
* Access a certain image with the **Worksheet.Pictures\[\]** property.
    
* Load a new image and replace the old one with it using the **ExcelPicture.Picture** property.
    
* Save the modified Excel workbook through the **Workbook.SaveToFile()** method.
    

The code example here shows how to replace the first image with a new one in an Excel file:

```python
from spire.xls import *
from spire.xls.common import *
 
# Create a Workbook instance
workbook = Workbook()
 
# Load an Excel file
workbook.LoadFromFile ("/sample.xlsx")


# Get the first worksheet
sheet = workbook.Worksheets[1]
 
# Get the first picture from the worksheet
excelPicture = sheet.Pictures[0]
             
# Replace the picture with another one
excelPicture.Picture = Image.FromFile("/excel.png")
 
# Save the result file
workbook.SaveToFile("/ReplaceImage.xlsx", ExcelVersion.Version2016)
```

![Python Replace Images in Excel](https://cdn.hashnode.com/res/hashnode/image/upload/v1741253696137/9442370b-ca95-429f-9c12-6049c02cf2f3.png align="center")

## How to Extract Images from Excel Workbooks

Whether you need to reuse images from an Excel sheet or archive visual data for future reference, extracting them manually can be time-consuming. Since Excel doesn’t offer a built-in feature for this, automating the process with Python is a more efficient solution. In this section, we’ll walk you through the steps to quickly and accurately extract images from an Excel workbook using Python.

Steps to extract images from Excel:

* Create a Workbook object.
    
* Load an Excel file with the **Workbook.LoadFromFile()** method.
    
* Get a worksheet through the **Workbook.Worksheets\[\]** rpoperty.
    
* Loop through all images in the worksheet.
    
* Get all pictures with the [**Worksheet.Pictures**](http://Worksheet.Pictures) property.
    
* Extract each picture and save them through the [**ExcelPicture.Picture.Save**](http://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 ("/sample.xlsx")


# Get the first worksheet
sheet = workbook.Worksheets[1]
 
# 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("/Image-{0:d}.png".format(i), ImageFormat.get_Png())
 
workbook.Dispose()
```

![Python Extract Images from Excel ](https://cdn.hashnode.com/res/hashnode/image/upload/v1741253760174/ca846261-0f8d-4954-bc08-03ce08f3448d.png align="center")

## The Bottom Line

This article covers how to replace and extract images in Excel files using Python. With step-by-step instructions and practical code examples, you'll be able to manage images in Excel with ease. By the end of this guide, handling image replacement and extraction will be a breeze!
