Convert Excel to SVG in Python Like a Pro | Guide
At first glance, Excel spreadsheets and SVG files might not seem related. Yet, there are situations where you need to convert an Excel table into SVG format—for instance, when you want to display Excel-generated data on a website or other visual platforms without embedding the entire Excel file.
Unfortunately, tools like MS Excel don’t offer a direct way to do this. Typically, you’d have to export the Excel file to a PDF first and then convert the PDF to SVG, which can be time-consuming and tedious. But don’t worry—this article will show you how to simplify the process. With Python, you can quickly and directly convert Excel to SVG, saving time and effort while achieving professional results.
Python Library to Convert Excel to SVG
Although Python is powerful, some third-party libraries will make the conversion between Excel files and SVG easier. Talking about Excel libraries, you may think of OpenPyXL, XlsxWriter, Spire.XLS, Aspose.Cell and so on. In this guide, we will use Spire.XLS for Python (full name of Spire.XLS) as an example tool to demonstrate converting Excel to SVG because of its intuitive APIs. But don’t worry—the steps of different APIs are similar. You can install this tool using the pip command: pip install Spire.XLS
.
Convert Excel Worksheets to SVG in Python in 3 Steps
Suppose you are fully prepared now, let’s get down to the real problem. Converting Excel worksheets to SVGs is easy and straightforward with the Workbook.SaveToFile() method. Basically, you just need three main steps: first load a source Excel file, then get a worksheet, and finally, save it as SVG. Let’s dive into the detailed instructions.
Steps to convert Excel to SVG:
Create a Workbook instance.
Load a source Excel file from the local storage using the Workbook.LoadFromFile() method.
Get a worksheet through the Workbook.Worksheets[] property.
Create a Stream object.
Convert the Excel worksheet to SVG with the Worksheet.SaveToStream() method.
Below is an example of converting the first worksheet to SVG:
from spire.xls.common import *
from spire.xls import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("/sample.xlsx")
# Get the first worksheet
worksheet = workbook.Worksheets[0]
# Save the worksheet to an SVG
stream = Stream("/WorksheetToSVG.svg")
worksheet.ToSVGStream(stream, 0, 0, 0, 0)
stream.Flush()
stream.Close()
# Release the resource
workbook.Dispose()
Convert a Chart in Excel to SVG Quickly
Unlike standard worksheets, a chart sheet doesn't contain cells; instead, it features a full-page chart. As a result, the steps to save it as an SVG differ slightly from those used for regular worksheets. Let’s explore how this process works.
Steps to convert a chart sheet to SVG:
Create an instance of the Workbook class.
Specify the file path of the source Excel file using the Workbook.LoadFromFile() method.
Get a chart sheet with the Workbook.GetChartSheetByName() method.
Save the chart sheet of an Excel file as SVG through the ChartSheet.ToSVGStream() method.
Here is the code example of converting a chart sheet to SVG:
from spire.xls.common import *
from spire.xls import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Sample2.xlsx")
# Get a specific chart sheet
chartSheet = workbook.GetChartSheetByName("Chart1")
# Save the chart sheet to an SVG
stream = Stream("ChartSheetToSVG.svg")
chartSheet.ToSVGStream(stream)
stream.Flush()
stream.Close()
# Release the resource
workbook.Dispose()
The Conclusion
This guide demonstrates how to convert Excel files to SVG, covering both standard worksheets and chart sheets. With clear steps and practical code examples, you'll see just how quick and easy it is to perform this conversion.