Last modified: Nov 13, 2025 By Alexander Williams

Insert Images into Excel with Python openpyxl

Adding images to Excel files is common. Python openpyxl makes it easy. This guide shows you how.

You will learn to insert various image formats. We cover positioning and sizing too.

Install openpyxl

First, ensure openpyxl is installed. Use pip for installation.


pip install openpyxl

If you need help with installation, check our Install Openpyxl in Python Step by Step guide.

Basic Image Insertion

Start by importing necessary modules. You need openpyxl and Image from openpyxl.drawing.image.


from openpyxl import Workbook
from openpyxl.drawing.image import Image

# Create a new workbook
wb = Workbook()
ws = wb.active

# Load an image
img = Image('example.jpg')

# Add image to worksheet at cell A1
ws.add_image(img, 'A1')

# Save the workbook
wb.save('workbook_with_image.xlsx')

This code creates a new Excel file. It inserts an image at cell A1.

Supported Image Formats

openpyxl supports common image formats. These include JPG, PNG, GIF, and BMP.

Ensure your image files are accessible. Use correct file paths in your code.


# Different image formats
jpg_img = Image('photo.jpg')
png_img = Image('diagram.png')
gif_img = Image('animation.gif')
bmp_img = Image('graphic.bmp')

# Add to different cells
ws.add_image(jpg_img, 'C5')
ws.add_image(png_img, 'E10')

You can mix different formats in one workbook. Each retains its original quality.

Positioning Images Precisely

Cell references like 'A1' work well. For precise placement, use coordinates.


from openpyxl.utils import units

# Position image at specific coordinates
img.anchor = 'D4'  # Anchor to cell D4
# Or use pixel coordinates
img.left = 200
img.top = 150

ws.add_image(img)

Coordinates are in pixels. This gives you fine control over image placement.

Resizing Images

You can change image dimensions. Use width and height properties.


# Original image
original_img = Image('large_photo.jpg')

# Create resized version
resized_img = Image('large_photo.jpg')
resized_img.width = 200  # pixels
resized_img.height = 150

ws.add_image(original_img, 'A1')
ws.add_image(resized_img, 'D1')

Both original and resized images appear. This helps fit images to your layout.

Multiple Images in One Worksheet

You can add many images to a sheet. Just use different anchor points.


# Create multiple image objects
img1 = Image('chart1.png')
img2 = Image('chart2.png')
img3 = Image('logo.jpg')

# Position them in different locations
img1.anchor = 'B2'
img2.anchor = 'F2'
img3.anchor = 'J2'

# Add all to worksheet
ws.add_image(img1)
ws.add_image(img2)
ws.add_image(img3)

Space them properly. Avoid overlapping for clean presentation.

Working with Existing Workbooks

Often you need to add images to existing files. Load the workbook first.


from openpyxl import load_workbook

# Load existing workbook
wb = load_workbook('existing_file.xlsx')
ws = wb.active

# Add image to existing sheet
img = Image('new_image.png')
ws.add_image(img, 'C10')

# Save with changes
wb.save('updated_file.xlsx')

Learn more about handling existing files in our Openpyxl Load Workbook Iterate Sheets Guide.

Image Best Practices

Follow these tips for better results. They improve performance and appearance.

Optimize image sizes before insertion. Large images increase file size significantly.

Use appropriate formats. PNG for graphics, JPG for photos.

Consider cell alignment. Images may cover cell content if not positioned carefully.

Common Issues and Solutions

Sometimes images don't appear. Here are common problems and fixes.

File not found errors occur with wrong paths. Use absolute paths for certainty.

Large images can cause memory issues. Resize them before adding to Excel.

Format compatibility problems are rare. Stick to supported formats mentioned earlier.

Combining with Other Features

Images work well with other openpyxl features. Combine them for powerful reports.

Add images alongside Excel charts using Python openpyxl for comprehensive data visualization.

Use with formatted cells from our Style Cells and Fonts in Excel with Python openpyxl guide.

Complete Example

Here is a full working example. It demonstrates multiple concepts together.


from openpyxl import Workbook
from openpyxl.drawing.image import Image

# Create workbook and select sheet
wb = Workbook()
ws = wb.active
ws.title = "Product Catalog"

# Add company logo
logo = Image('company_logo.png')
logo.width = 100
logo.height = 50
logo.anchor = 'A1'
ws.add_image(logo)

# Add product images
product1 = Image('product1.jpg')
product1.anchor = 'A10'
ws.add_image(product1)

product2 = Image('product2.jpg')
product2.width = 150
product2.anchor = 'D10'
ws.add_image(product2)

# Save the complete workbook
wb.save('product_catalog.xlsx')

This creates a professional product catalog. It includes logo and product images.

Conclusion

Inserting images into Excel with Python openpyxl is straightforward. You learned basic and advanced techniques.

Remember to position images carefully. Optimize sizes for best performance.

Combine images with other Excel features. Create dynamic, visual reports automatically.

Start enhancing your Excel files with images today. The possibilities are endless.