Python: Compress, Resize, or Move Images in Excel

2023-11-27 01:01:26 Written by  support iceblue
Rate this item
(0 votes)

Excel provides various options to compress, resize, or move images, allowing users to effectively manage and optimize their spreadsheets. By utilizing these features, you can significantly reduce file size, adjust image dimensions to fit within cells, and effortlessly reposition images to enhance the visual appeal of your Excel documents. This article introduces how to programmatically compress, resize or move images in an Excel document in Python using Spire.XLS for Python.

Install Spire.XLS for Python

This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.XLS

If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows

Compress Images in an Excel Document in Python

To compress the quality of an image, Spire.XLS for Python offers the ExcelPicture.Compress() method. The following are the steps to compress images in an Excel document using Spire.XLS for Python.

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Iterate through the worksheets in the document, and get the images from a specific sheet through Worksheet.Pictures property.
  • Get a specific image from the image collection and compress it using ExcelPicture.Compress() method.
  • Save the workbook to a different Excel file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load an Excel document
workbook.LoadFromFile("C:/Users/Administrator/Desktop/Images.xlsx")

# Loop through the worksheets in the document
for sheet in workbook.Worksheets:

    # Loop through the images in the worksheet
    for picture in sheet.Pictures:

        # Compress a specific image
        picture.Compress(50)

# Save the file
workbook.SaveToFile("output/CompressImages.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Python: Compress, Resize, or Move Images in Excel

Resize an Image in an Excel Worksheet in Python

The width and height of an image can be set or get through the ExcelPicture.Width property and the ExcelPicture.Height property. To resize an image in Excel, follow the steps below.

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet though Workbook.Worksheets[index] property.
  • Get a specific image from the worksheet through Worksheet.Pictures[index] property.
  • Reset the size of the image through ExcelPicture.Width property and ExcelPicture.Height property.
  • Save the workbook to a different Excel file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load the Excel document
workbook.LoadFromFile("C:/Users/Administrator/Desktop/Image.xlsx")

# Get a specific worksheet
sheet = workbook.Worksheets[0]

# Get a specific picture from the worksheet
picture = sheet.Pictures[0]

# Resize the picture
picture.Width = (int)(picture.Width / 2)
picture.Height = (int)(picture.Height / 2) 

# Save to file
workbook.SaveToFile("output/ResizeImage.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Python: Compress, Resize, or Move Images in Excel

Move an Image within the Same Worksheet in Python

The start position of an image can be set or get through the ExcelPicture.TopRow property and the ExcelPicture.LetColumn property. To move an image within the same worksheet, follow the steps below.

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet though Workbook.Worksheets[index] property.
  • Get a specific image from the worksheet through Worksheet.Pictures[index] property.
  • Reset the position of the image in the worksheet through ExcelPicture.TopRow property and ExcelPicture.LeftColumn property.
  • Save the workbook to a different Excel file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load the Excel document
workbook.LoadFromFile("C:/Users/Administrator/Desktop/Image.xlsx")

# Get a specific worksheet
sheet = workbook.Worksheets[0]

# Get a specific picture from the worksheet
picture = sheet.Pictures[0]

# Reset the position of the picture
picture.TopRow = 5
picture.LeftColumn = 6

# Save to file
workbook.SaveToFile("output/MoveImage.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Python: Compress, Resize, or Move Images in Excel

Move an Image from a Worksheet to Another in Python

Besides moving images in the same worksheet, you can also move images in different worksheets of the workbook. First, you need to get the desired image from a worksheet and add it to a different worksheet using the Worksheet.Pictures.Add() method, and then delete the original image using the ExcelPicture.Remove() method. The detailed steps are as follows.

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet though Workbook.Worksheets[index] property.
  • Get a specific image from the worksheet through Worksheet.Pictures[index] property.
  • Get another worksheet though Workbook.Worksheets[index] property.
  • Add the image to the target worksheet using Worksheet.Pictures.Add() method.
  • Remove the image from the source worksheet using ExcelPicture.Remove() method.
  • Save the workbook to a different Excel file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load the Excel document
workbook.LoadFromFile("C:/Users/Administrator/Desktop/Image.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Get the first picture from the worksheet
picture = sheet.Pictures[0]

# Get the second worksheet
sheet_two = workbook.Worksheets[1]

# Add the picture to the second worksheet
sheet_two.Pictures.Add(1, 1, picture.Picture)

# Remove the picture in the first worksheet
picture.Remove()

# Save to file
workbook.SaveToFile("output/MoveImage.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Python: Compress, Resize, or Move Images in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 25 April 2024 02:15