In various cases, you need to embed Excel spreadsheets into Web or desktop applications. One solution in this case is to convert Excel worksheets to image formats. In this article, you’ll learn how to convert Excel XLSX or XLS to PNG, JPEG, SVG, or other images in Python.

  • Convert Excel to images in Python
  • Convert Excel to SVG in Python

To convert Excel XLSX or XLS files to image format, we’ll use Aspose.Cells for Python via Java, a spreadsheet manipulation API that lets you create, modify, or convert Excel files. You can download Aspose.Cells for Python

Aspose.Cells for Python via Java supports converting Excel files to the following image formats:

  • EMF
  • WMF
  • JPEG
  • PNG
  • BMP
  • GIF
  • TIFF
  • SVG
  • GLTF
  • PICT
  • SVM
  • Office Compatible EMF

Convert Excel to images in Python

Here are the steps to convert Excel files to image formats, i.e. PNG, JPEG, and so on.

  1. Use the Workbook class to load Excel files
  2. Create an instance of the ImageOrPrintOptions class and specify the output image format.
  3. Use the workbook.getWorksheets ().get(index) method to access the worksheet to be transformed.
  4. Create a SheetRender object and initialize it with the Worksheet and ImageOrPrintOptions objects.
  5. Save each page of an Excel worksheet as an image using sheetRender.toImage (pageIndex, fileName) method.

The following code example shows how to convert an Excel worksheet into a PNG image in Python.

# load the Excel workbook
workbook = Workbook("Book1.xlsx")

# create image options
imgOptions = ImageOrPrintOptions()
imgOptions.setSaveFormat(SaveFormat.SVG)

# load the worksheet to be rendered
sheet = workbook.getWorksheets().get(0)

# create sheet render object
sr = SheetRender(sheet, imgOptions)

# convert sheet to PNG image
for j in range(0, sr.getPageCount()):
	sr.toImage(j, "WorksheetToImage-out%s" %(j) + ".png")
Copy the code

Convert Excel to SVG in Python

Here are the steps to convert Excel files to SVG in Python.

  1. Use the Workbook class to load Excel files.
  2. Create an instance of the ImageOrPrintOptions class and specify the output image format.
  3. Use the workbook.getWorksheets ().getCount() method to traverse a worksheet in an Excel file.
  4. In each iteration, do the following:
    1. Create a SheetRender object and initialize it with the Worksheet and ImageOrPrintOptions objects.
    2. Save each page of an Excel worksheet as SVG using sheetRender.toimage (pageIndex, fileName) method.

The following code example shows how to convert Excel to SVG in Python.

# load the Excel workbook
workbook = Workbook("Book1.xlsx")

# create image options
imgOptions = ImageOrPrintOptions()
imgOptions.setSaveFormat(SaveFormat.SVG)

# get sheet count
sheetCount = workbook.getWorksheets().getCount()

# loop through the sheets
for i in range(0, sheetCount):
	sheet = workbook.getWorksheets().get(i)
	
	# convert each sheet to SVG
	sr = SheetRender(sheet, imgOptions)
	for j in range(0, sr.getPageCount()):
	  sr.toImage(j, sheet.getName() + "%s" % j + "_out.svg")
Copy the code

If you have any questions or requirements, please feel free to join the Aspose Technology Exchange Group (761297826), we are happy to provide you with inquiries and consultation.