Skip to main content

Command Palette

Search for a command to run...

How to Add or Delete Excel Images with Python

Published
5 min readView as Markdown

In data processing and office automation, Excel files often rely on images to boost visual clarity—whether for reports, dashboards, or official documents. However, manual image management (adding/deleting) is tedious, time-consuming, and prone to human error. If you’re looking to automate this workflow with Python, Free Spire.XLS for Python is the ultimate free solution.

This step-by-step guide walks you through adding images to specific cells/custom positions and deleting single/batch images in Excel using Python. Packed with runnable code, practical scenarios, and expert tips, it’s tailored for data analysts, automation engineers, and anyone aiming to streamline Excel tasks.


Why Python + Free Spire.XLS Stands Out

While popular libraries like pandas or openpyxl excel at Excel data manipulation, they fall short when it comes to image operations:

  • openpyxl: Only handles cell content (text/numbers) — no direct image support.
  • xlwings: Requires a local Excel installation and COM interface, making it unsuitable for cross-platform or server-side automation.

Free Spire.XLS for Python solves these pain points with a lightweight, powerful design:
✅ Full image functionality (add, delete, crop, resize)
✅ No Excel installation or COM dependencies (runs natively in Python)
✅ Cross-platform support (Windows, macOS, Linux)
✅ Supports both .xlsx and .xls formats
✅ Simple API with minimal code complexity

Direct Comparison Table

FeatureFree Spire.XLS for Pythonopenpyxl / xlwings
Image Addition✅ Full support (cell/custom positions)❌ No direct support
Image Deletion✅ Single/batch deletion❌ No built-in methods
Excel Runtime Required❌ No dependency✅ Mandatory (xlwings)
Cross-Platform Compatibility✅ Windows/macOS/Linux❌ Limited (xlwings)
Script Automation✅ Seamless (no GUI required)⚠️ Requires Excel GUI (xlwings)

📌 Note: The free version of Free Spire.XLS for Python has a file size limit (suitable for small-to-medium documents).


Step 1: Install Free Spire.XLS for Python

Start by installing the library via pip (works for Python 3.6+):

pip install spire.xls.free

How to Add Images to Excel with Python

Free Spire.XLS offers two flexible ways to add images—choose based on your use case.

Scenario 1: Add Images to Specific Cells

Ideal for aligning images with tabular data (e.g., product logos next to names, screenshots next to metrics). Use Worksheet.Pictures.Add(row, column, image_path) to anchor images to cells.

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

# 1. Create a new workbook (or load an existing one with LoadFromFile())
workbook = Workbook()
sheet = workbook.Worksheets[0]  # Get the first worksheet

# 2. Add image to cell (row 1, column 3 — note: rows/columns are 1-indexed)
image_path = "logo.png"  # Replace with your image path (JPG/PNG/BMP supported)
picture = sheet.Pictures.Add(1, 3, image_path)

# 3. Adjust cell size to fit the image (optional but recommended)
sheet.Columns[2].ColumnWidth = 25  # Column C (index 2, 0-indexed)
sheet.Rows[0].RowHeight = 135      # Row 1 (index 0, 0-indexed)

# 4. Save the modified file
workbook.SaveToFile("Excel_with_Image.xlsx", ExcelVersion.Version2016)
workbook.Dispose()  # Release resources
print("Image added successfully!")

Scenario 2: Add Images with Custom Position & Size

For precise control (e.g., overlaying images on charts), use LeftColumnOffset, TopRowOffset, Width, and Height properties.

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

workbook = Workbook()
sheet = workbook.Worksheets[0]

# Add image (anchor to cell A1 first, then adjust position)
image_path = "chart_screenshot.png"
picture = sheet.Pictures.Add(1, 1, image_path)

# Customize position (offsets from the top-left of cell A1)
picture.LeftColumnOffset = 90  # 90 points to the right
picture.TopRowOffset = 20      # 20 points down

# Customize size (pixels)
picture.Width = 150
picture.Height = 150

# Save with Excel 2019 compatibility
workbook.SaveToFile("Excel_Custom_Image.xlsx", ExcelVersion.Version2019)
workbook.Dispose()
print("Custom-sized image added successfully!")

💡 Pro Tip: The library supports JPG, PNG, BMP, and GIF formats. For high-resolution images, avoid excessive resizing to prevent distortion.


How to Delete Images from Excel with Python

Free Spire.XLS lets you delete single images by index or batch-delete all images—no manual selection required.

Scenario 1: Delete a Specific Image

Images are stored in Worksheet.Pictures (a list-like collection, 0-indexed). Use the image’s index to target it.

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

# 1. Load the Excel file with images
workbook = Workbook()
workbook.LoadFromFile("Excel_with_Image.xlsx")
sheet = workbook.Worksheets[0]

# 2. Delete the first image (index 0)
if sheet.Pictures.Count > 0:  # Check if images exist to avoid errors
    sheet.Pictures[0].Remove()
    print("Specific image deleted successfully!")
else:
    print("No images found in the worksheet.")

# 3. Save the modified file
workbook.SaveToFile("Excel_After_Delete.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Scenario 2: Batch Delete All Images

To remove all images (e.g., cleaning up old reports), iterate backward through the Pictures collection (avoids index shifting issues).

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

workbook = Workbook()
workbook.LoadFromFile("Excel_with_Image.xlsx")
sheet = workbook.Worksheets[0]

# Delete all images (backward iteration to prevent index errors)
for i in range(sheet.Pictures.Count - 1, -1, -1):
    sheet.Pictures[i].Remove()

print(f"All {sheet.Pictures.Count + 1} images deleted successfully!")  # Count is 0 after deletion

workbook.SaveToFile("Excel_No_Images.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

📌 Key Note: Backward iteration (range(start, end, -1)) is critical here. If you iterate forward, deleting an image shifts the indices of remaining images, leading to missing or duplicate deletions.


Pro Tips for Success:

  1. Batch Processing: Combine with os.walk() to add/delete images in multiple Excel files (e.g., process all files in a folder).
  2. Error Handling: Add try-except blocks to handle missing files or invalid image paths (e.g., FileNotFoundError).
  3. Format Compatibility: Save as .xlsx (not .xls) for better compatibility with modern Excel versions.

Final Thoughts

Free Spire.XLS for Python simplifies Excel image automation with its intuitive API, cross-platform support, and zero dependencies. Whether you’re adding images to reports or cleaning up redundant visuals, this library eliminates manual work and boosts productivity.

Start automating Excel image tasks today—save time, reduce errors, and focus on what matters most! 🚀