USING IRONXL FOR PYTHON

How to Insert image into Excel Using Python

Published December 15, 2024
Share:

For data analysis and reporting in modern applications, Microsoft Excel remains one of the most widely used tools. Python, with its robust ecosystem of libraries, offers a powerful way to automate and enhance the functionality of Microsoft Excel. One such library is IronXL, which allows users to work with Excel files in an intuitive and efficient manner. This article will guide you through the process of inserting images into Excel using IronXL in Python.

How to Insert image into Excel Using Python

  1. Install the IronXL python package.
  2. Create or Open an Excel Workbook.
  3. Select the Worksheet where the image will be inserted.
  4. Insert the Image at a specified location.
  5. Save the Workbook.

What is IronXL?

IronXL is a .NET library that simplifies the process of reading, writing, and manipulating Excel files. It is particularly advantageous for developers who want to work with Excel documents without requiring Microsoft Office installed on their machine. IronXL provides a straightforward API to handle various Excel functionalities, including creating spreadsheets, formatting cells, and inserting images. In python, we can make use of the IronXL library using the IronXL python package. You can find the complete API documentation here.

Prerequisites

Before we dive into the code, ensure that you have the following prerequisites:

  1. Python Installed: Make sure you have Python installed on your machine. You can download it from the official Python website.
  2. IronXL Installed: You need to install the IronXL package. You can do this using pip.
  3. Image File: Have an image file ready that you want to insert into your Excel sheet. Supported formats typically include JPEG, PNG, and BMP.

Steps for Python Insert Image to Excel file

The process of inserting an image into an Excel spreadsheet involves the following basic steps:

Step 1: Install the IronXL python package.

Make sure you have created a python file with .py extensions. Now install the IronXL package

pip install IronXL

Step 2: Create or Open an Excel Workbook.

Create an Excel file or open an existing Excel file using iron XL

workbook = ironxl.Workbook()  # For a new file
# Or 
workbook = ironxl.Workbook('existing_file.xlsx')  # For an existing workbook
PYTHON

Step 3: Select the Worksheet where the image will be inserted.

worksheet = workbook.add_worksheet('Sheet1')  # Create a new worksheet
# or
worksheet = workbook.get_worksheet('Sheet1')  # To access an existing worksheet
# or
worksheet = workbook.DefaultWorkSheet
PYTHON

Step 4: Insert the Image at a specified location.

image_path = "ironXL.jpg  # Path to your image file / picture
# Specify the cell where you want to insert image
cell_location = 'B2'  # specify excel cell, also can have custom cell border
worksheet.insert_image(cell_location, image_path) # write image
PYTHON

Step 5: Save the Workbook.

workbook.save('AwesomeIronXL.xlsx')
PYTHON

Here are the details of how to insert an image into an Excel file using python IronXL:

Image to insert

How to Insert image into Excel Using Python: Figure 1

Full Code Example

import ironxl
# Step 1: Create a new Excel workbook or open an existing one
workbook = ironxl.Workbook()  # For a new excel file
# workbook = ironxl.Workbook('existing_file.xlsx')  # For an existing workbook
# Step 2: Select the a worksheet from list of worksheets or create new
# worksheet = workbook.add_worksheet('Sheet1')  # Create a new worksheet
# worksheet = workbook.get_worksheet('Sheet1')  # To access an existing worksheet
worksheet = workbook.DefaultWorkSheet
# Step 3: Insert an image
image_path = "ironXL.jpg  # Path to your image file / picture
# Specify the cell where you want to place the image
cell_location = 'B2'  # specify excel cell, also can have custom cell border
worksheet.insert_image(cell_location, image_path) # write image
# Step 4: Save the workbook
workbook.save('OwesomeIronXL.xlsx')
PYTHON

Explanation of the Code

  1. Creating/Opening a Workbook: You can either create a new workbook or open an existing one using ironxl.Workbook() function. If you are working with an existing Excel file, make sure to provide the correct file path to the picture/image.
  2. Selecting a Worksheet: You can add a new worksheet using add_worksheet() or access an existing one with get_worksheet().
  3. Inserting the Image: The insert_image() method is used to place the image. You need to specify the cell where the image will be inserted, along with the path to the image file as an attribute.
  4. Saving the Workbook: Finally, save the workbook using the save() method, providing a filename for the output Excel file.

Output

How to Insert image into Excel Using Python: Figure 2

Command output

How to Insert image into Excel Using Python: Figure 3

Additional Considerations

Image Size and Formatting

When inserting images, it’s essential to consider the size and aspect ratio to ensure they fit well within the cells. You may want to resize your image beforehand or use additional methods to adjust its dimensions after insertion.

Error Handling

Implement error handling to manage scenarios such as file not found, unsupported file formats, or issues with the Excel file itself. Using try-except blocks can help manage exceptions gracefully.

Dependencies and Limitations

While IronXL is powerful, it’s important to understand its dependencies and licensing. Ensure that you comply with the licensing terms and understand the limitations of the free version, if applicable.

License (Trial Available)

Users need to include a trial license to use the IronXL. In the code, just include the license as below

import ironxl
ironxl.License.LicenseKey = "Your License"
PYTHON

You can get complete information on the license here.

Conclusion

Inserting images into Excel using Python and IronXL is a straightforward process that enhances the visual appeal of your spreadsheets. Whether for reporting, presentations, or data visualization, incorporating images can make your Excel documents more engaging. With the steps outlined above, you can seamlessly automate the insertion of images, saving time and improving efficiency in your data handling tasks.

As you become more familiar with IronXL, explore its other functionalities such as formatting cells, creating charts, and managing complex data sets to further enhance your Excel automation capabilities.

NEXT >
How to Create an Excel File in Python

Ready to get started? Version: 2024.11 just released

Free pip Download View Licenses >