Fundamentals 5 min read

Batch Insert Images into Excel Cells with Python and openpyxl

This article demonstrates how to use Python's openpyxl library to automatically insert named images into specific Excel cells in bulk, explaining the problem, providing a complete script, and offering practical tips for handling large files and sharing the solution with the community.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Batch Insert Images into Excel Cells with Python and openpyxl

Introduction

The author received a question in a Python automation group about inserting images into Excel based on the image name stored in a cell. The goal is to match each image file with its corresponding cell in column B and insert it automatically.

Solution Code

The following Python script uses openpyxl to load the workbook, collect image files from a folder, resize them, and insert each image into the appropriate row in column B.

from openpyxl.drawing.image import Image
import os
from openpyxl import load_workbook

pic_path = r'C:\Users\Administrator\Desktop\图片练习'
wb = load_workbook(r'C:\Users\Administrator\Desktop\练习2.xlsx')
ws = wb.active
pic_list = [os.path.join(pic_path, i) for i in os.listdir(pic_path) if i.endswith('.jpg')]

new_size = (90, 90)
num = 2
for i, pic in enumerate(pic_list, start=1):
    image = Image(pic)
    image.width, image.height = new_size
    ws.row_dimensions[num].height = 80
    ws.column_dimensions['b'].width = 12
    ws.add_image(image, 'b' + str(num))
    num += 2
wb.save(r'C:\Users\Administrator\Desktop\练习2.xlsx')

The script resizes each image to 90x90 pixels, adjusts row height and column width, and inserts the image into column B of the corresponding row.

Implementation Details

The idea was inspired by a community member who suggested that a simple name‑matching approach could be done directly in Excel or VBA. The Python solution fixes the image size to the cell dimensions, making batch insertion straightforward.

Additional screenshots illustrate the result of the batch insertion and the code layout.

Conclusion

The article provides a complete, ready‑to‑use script for anyone needing to batch‑insert images into Excel based on cell names. It also shares practical advice for asking technical questions, such as providing small demo data, error screenshots, and keeping code snippets concise.

Readers are encouraged to adapt the script for their own automation tasks and to share feedback with the community.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonBatch ProcessingopenpyxlExcel Automationimage insertion
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.