In various cases, Excel spreadsheets are required to be embedded in the web or desktop applications. One of the solutions in such cases is the conversion of Excel worksheets to image formats. In this article, you will learn how to convert Excel XLSX or XLS to PNG, JPEG, SVG, or other images in Python.
Python Excel to Image Converter API
In order to convert Excel’s XLSX or XLS files to image formats, we will use Aspose.Cells for Python via Java. It is a spreadsheet manipulation API that lets you create, modify or convert Excel files. You can install the API using the following command.
pip install aspose-cells
Aspose.Cells for Python via Java supports conversion of Excel files to the following image formats:
Python Excel to Image Conversion
The following are the steps to convert Excel files to an image format, i.e. PNG, JPEG, etc. in Python.
- Load the Excel file using the Workbook class.
- Create an instance of ImageOrPrintOptions class and specify the output image format.
- Access the worksheet you want to convert using Workbook.getWorksheets().get(index) method.
- Create a SheetRender object and initialize it with Worksheet and ImageOrPrintOptions objects.
- Save each page of Excel worksheet as an image using SheetRender.toImage(pageIndex, fileName) method.
The following code sample shows how to convert an Excel worksheet to PNG image.
Convert Excel to SVG Image in Python
The following are the steps to convert an Excel file to SVG in Python.
- Load the Excel file using the Workbook class.
- Create an instance of ImageOrPrintOptions class and specify the output image format.
- Loop through the worksheets in the Excel file using Workbook.getWorksheets().getCount() method.
- In each iteration, perform the following operations:
- Create a SheetRender object and initialize it with Worksheet and ImageOrPrintOptions objects.
- Save each page of the Excel worksheet as SVG using SheetRender.toImage(pageIndex, fileName) method.
The following code sample shows how to convert Excel to SVG in Python.
Get a Free API License
You can use the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to convert Excel files to image formats in Python. The step-by-step guide and code samples demonstrated how to convert sheets in an Excel workbook to PNG and SVG formats. You can explore more about the Python spreadsheet API using the documentation. In case you would have any questions or queries, feel free to let us know via our forum.