Convert Excel to Images (PNG, JPEG, BMP) with Python

Alice Yang
3 min readJul 11, 2023
Convert Excel to Images in Python
Convert Excel to Images in Python

Images provide a convenient and universal way to share data. By converting Excel files into images, you can ensure that your data can be easily accessed and viewed on various devices, operating systems, and software applications without the need for specific spreadsheet software. In this article, we will explore how to convert Excel to various image formats using Python.

We’ll discuss the following topics:

Installation

To convert Excel files to images in Python, we will use Spire.XLS for Python, which is a python Excel library for creating, reading, editing, and converting Excel files within Python applications.

You can install Spire.XLS for Python from pypi by running the following commands in your terminal:

pip install Spire.Xls

For more detailed information about the installation, you can check this official documentation: How to Install Spire.XLS for Python in VS Code.

Convert an Excel File to PNG Images in Python

To convert an entire Excel file to images, you can loop through the worksheets in the file and save each worksheet as an image using the Worksheet.ToImage() method provided by Spire.XLS for Python.

The following code snippet shows how to convert an Excel file to PNG images in Python:

from spire.xls import *
from spire.xls.common import *

#Create a Workbook instance
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

#Iterate through all the worksheets in the file
for sheet in workbook.Worksheets:
fileName = sheet.Name + ".png"
#Save each worksheet to a PNG image
image = sheet.ToImage(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn)
image.Save(fileName)

workbook.Dispose()

Convert a Specific Excel Worksheet to a JPEG Image in Python

If you only want to convert a specific worksheet to an image, you can directly access that worksheet and then save it as an image using the Worksheet.ToImage() method.

The following code snippet shows how to convert a specific Excel worksheet to a JPEG image in Python:

from spire.xls import *
from spire.xls.common import *

#Create a workbook
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

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

#Save the worksheet to a JPEG image
image = sheet.ToImage(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn)
image.Save("SheetToImage.jpeg")

workbook.Dispose()

Convert a Specific Cell Range in an Excel Worksheet to a BMP Image in Python

You can convert a specific cell range within an Excel worksheet to an image by passing the start row index, start column index, end row index and end column index to the Worksheet.ToImage() method as parameters.

The following code snippet shows how to convert a specific cell range in an Excel worksheet to a BMP image in Python:

from spire.xls import *
from spire.xls.common import *

#Create a workbook
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

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

#Save a specific cell range of the worksheet to a BMP image
image = sheet.ToImage(1, 1, 2, 3)
image.Save("CellRangeToImage.bmp")

workbook.Dispose()

Convert Excel to Images without White Margins in Python

When converting Excel files to images, it is common to encounter white margins or empty spaces around the exported image. To convert Excel to images without white margins, you can remove the margins from the Excel worksheets before the conversion process.

The following code snippet shows how to convert an Excel worksheet to an image without white margins:

from spire.xls import *
from spire.xls.common import *

#Create a workbook
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

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

#Remove left, right, top and bottom margins from the worksheet
sheet.PageSetup.LeftMargin = 0
sheet.PageSetup.BottomMargin = 0
sheet.PageSetup.TopMargin = 0
sheet.PageSetup.RightMargin = 0

#Save the worksheet to a PNG image
image = sheet.ToImage(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn)
image.Save("SheetToImageWithoutMargins.png")

workbook.Dispose()

Conclusion

In this article, we delved into the process of converting Excel files to popular image formats such as PNG, JPEG, and BMP using Python and the Spire.XLS for Python library. In addition to converting Excel to image formats, Spire.XLS provides the ability to convert Excel files to other file formats such as PDF, CSV, HTML and XML. This versatility enables you to transform Excel data into various output formats suitable for different use cases and requirements.

--

--

Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.