Converting Excel spreadsheets to image formats can be extremely valuable and versatile in a wide range of situations. Whether you need to share data with others who don’t have Excel installed on their devices, present information in a document or presentation, or publish content online, converting Excel to image format offers a convenient solution. In this article, we will introduce how to programmatically convert Excel to images in Python using Spire.XLS for Python.
- Convert an Excel Worksheet to an Image in Python
- Convert an Excel Worksheet to an Image without White Margins in Python
- Convert a Specific Cell Range to an Image in 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
Convert an Excel Worksheet to an Image in Python
You can easily convert a whole Excel worksheet to an image by using the Worksheet.SaveToImage() method provided by Spire.XLS for Python. The detailed steps are as follows:
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet by its index using Workbook.Worksheets[int index] property.
- Convert the worksheet to an image using Worksheet.ToImage() method.
- Save the image to a PNG file (you can also save the image as other image formats such as JPG and BMP).
- Python
from spire.xls import * from spire.xls.common import * # Create a Workbook object workbook = Workbook() # Load an Excel file workbook.LoadFromFile("Sample.xlsx") # Get the first worksheet sheet = workbook.Worksheets[0] # Save the worksheet to an image image = sheet.ToImage(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn) # Save the image to a PNG file image.Save("SheetToImage.png") workbook.Dispose()
Convert an Excel Worksheet to an Image without White Margins in Python
When converting an Excel worksheet to an image, you may find the resulting image has unwanted white margins surrounding the cells. If you want to convert the worksheet to an image without any extraneous margins, you can remove the page margins set in the original worksheet. The detailed steps are as follows:
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet by its index using Workbook.Worksheets[int index] property.
- Remove all margins from the worksheet by setting its left, right, top, and bottom margin values to zero.
- Convert the worksheet to an image using Worksheet.ToImage() method.
- Save the image to a PNG file.
- Python
from spire.xls import * from spire.xls.common import * # Create a Workbook object workbook = Workbook() # Load an Excel file workbook.LoadFromFile("Sample.xlsx") # Get the first worksheet sheet = workbook.Worksheets[0] # Set all margins of the worksheet to zero sheet.PageSetup.LeftMargin = 0 sheet.PageSetup.BottomMargin = 0 sheet.PageSetup.TopMargin = 0 sheet.PageSetup.RightMargin = 0 # Convert the worksheet to an image image = sheet.ToImage(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn) # Save the image to a PNG file image.Save("SheetToImageWithoutMargins.png") workbook.Dispose()
Convert a Specific Cell Range to an Image in Python
In addition to converting a whole worksheet to an image, Spire.XLS for Python also supports converting a specific cell range of a worksheet to an image. The detailed steps are as follows:
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet by its index using Workbook.Worksheets[int index] property.
- Convert a specific cell range of the worksheet to an image using Worksheet.ToImage() method and pass the index of the start row, start column, end row, and end column of the cell range to the method as parameters.
- Save the image to a PNG file.
- Python
from spire.xls import * from spire.xls.common import * # Create a Workbook object workbook = Workbook() # Load an Excel file workbook.LoadFromFile("Sample.xlsx") # Get the first worksheet sheet = workbook.Worksheets[0] # Convert a specific cell range of the worksheet to an image image = sheet.ToImage(5, 2, 17, 5) # Save the image to a PNG file image.Save("CellRangeToImage.png") workbook.Dispose()
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.