In data processing and office automation scenarios, Excel files often require embedding images to enhance visual presentation. However, manually adding or deleting images is not only inefficient but also error-prone. How to implement automated management of images in Excel using Python? The answer is: use the Free Spire.XLS for Python library.
This article will systematically introduce how to add images and delete images in Excel files through Python code, providing complete examples and operation workflows. Based on practical and usable technical solutions, the content is suitable for data analysts, automation engineers, and office automation enthusiasts.
Why Choose Python + Free Spire.XLS?
Among numerous Python libraries such as pandas and openpyxl, although they can handle Excel table structures, their support for image operations is limited. For example:
-
openpyxlcan only read and write cell content, but cannot directly manipulate images. -
xlwingscan call the Excel interface, but it depends on the runtime environment and is not suitable for script automation.
In contrast, Free Spire.XLS for Python is a cross-platform library specifically designed for processing Excel files. It supports complete image operations, including adding, deleting, cropping, and format adjustment. Moreover, it does not require installing Excel or relying on COM interfaces, running entirely in the Python environment.
✅ Advantage Summary:
| Comparison Criteria | Free Spire.XLS for Python | openpyxl / xlwings |
|---|---|---|
| Image Addition Support | ✅ Supported | ❌ Cell content only |
| Image Deletion Support | ✅ Supported | ❌ No direct interface |
| No Excel Runtime Required | ✅ Supported | ❌ Excel environment needed |
| Script Automation | ✅ Efficient | ⚠️ Depends on external environment |
📌 Note: Free Spire.XLS for Python supports .xlsx and .xls formats but has a file size limit, making it suitable for small-scale documents.
Complete Workflow to Add Images to Excel with Python
Step 1: Install the Library
pip install spire.xls.free
Step 2: Code Example — Add Images to Specific Cell Positions
This scenario is suitable when images need to be aligned with cell content (e.g., images corresponding to row data in reports). The core is to specify the image path and target cell through Worksheet.Pictures.Add().
from spire.xls import *
from spire.xls.common import *
# Create a workbook object
workbook = Workbook()
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Add an image to the specified cell of the worksheet
imgPath = "logo.png"
picture = sheet.Pictures.Add(1, 3, imgPath)
# Adjust cell size to fit the image
sheet.Columns[2].ColumnWidth = 25
sheet.Rows[0].RowHeight = 135
# Save the file
workbook.SaveToFile("InsertImage.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
Advanced Skill: Add Images with Custom Position and Size
To precisely control the coordinates, width, and height of images, you can use the LeftColumnOffset / TopRowOffset / Width / Height properties provided by ExcelPicture:
from spire.xls import *
from spire.xls.common import *
# Create a workbook object
workbook = Workbook()
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Add an image to the specified cell of the worksheet
imgPath = "logo.png"
picture = sheet.Pictures.Add(1, 3, imgPath)
# Customize image position
picture.LeftColumnOffset = 90 # Left offset
picture.TopRowOffset = 20 # Top offset
# Customize image size
picture.Width = 150
picture.Height = 150
# Save the file
workbook.SaveToFile("add_custom_size_image.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
Delete Images in Excel with Python
Free Spire.XLS for Python supports deleting specific images by index or batch deleting all images. The specific implementations are as follows.
Scenario 1: Delete an Image at a Specified Position
Images in a worksheet are stored as a list in the Pictures property, which can be located and deleted by index (starting from 0):
from spire.xls import *
from spire.xls.common import *
# Load the Excel file
workbook = Workbook()
workbook.LoadFromFile("InsertImage.xlsx")
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Delete the first image in the worksheet
sheet.Pictures[0].Remove()
# Save the file
workbook.SaveToFile("DeleteImage.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
Delete All Images in a Worksheet
Batch delete all images by iterating through the Pictures list:
from spire.xls import *
from spire.xls.common import *
# Load the Excel file
workbook = Workbook()
workbook.LoadFromFile("InsertImage.xlsx")
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Delete all images in the worksheet
for i in range(sheet.Pictures.Count - 1, -1, -1):
sheet.Pictures[i].Remove()
# Save the file
workbook.SaveToFile("DeleteAllImage.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
📌 Tip: After deletion, the cells will revert to plain text or blank.
Practical Application Scenarios and Recommendations
| Scenario | Recommended Operation |
|---|---|
| Data report generation | Automatically add charts or screenshots |
| Project progress visualization | Embed progress charts in task sheets |
| Purchase order/contract processing | Add company logo or approval page images |
| Automated audit processes | Delete redundant images in old versions |
Free Spire.XLS for Python provides Python developers with a lightweight and free solution for Excel image operations. It enables adding images (to specified cells/with custom sizes) and deleting images (specific images/batch deletion) without relying on Microsoft Excel. Its advantages lie in cross-platform compatibility and concise API design. In practical applications, it can be combined with logic, such as batch file traversal to further expand functions and improve the automation efficiency of Excel processing.
Top comments (0)