Render Presentation Slides as SVG Images in Python via Java
Overview
SVG is a scalable XML-based image format that works well for web publishing, slide viewers, accessibility workflows, and automated post-processing. Aspose.Slides exports each slide to a separate SVG file and lets you control how text, fonts, pictures, and SVG elements are written.
Use SVGOptions when the exported SVG must be compact, predictable across browsers, or ready for interactive use.
Export a Slide as SVG
Create a Presentation, select a slide, and write it to a stream with Slide.writeAsSvg. The examples require an existing presentation.pptx file. Each example starts the JVM if needed and closes its output streams. The following example exports every slide in a presentation as a separate SVG file.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation
from java.io import FileOutputStream
presentation = Presentation("presentation.pptx")
try:
for slide in presentation.getSlides():
output_file_name = f"slide-{slide.getSlideNumber()}.svg"
svg_stream = FileOutputStream(output_file_name)
try:
slide.writeAsSvg(svg_stream)
finally:
svg_stream.close()
finally:
presentation.dispose()
The filename uses Slide.getSlideNumber rather than the loop index. You can also export an individual shape with Shape.writeAsSvg when a slide viewer or web page needs only that shape.
Configure SVG Output
SVGOptions controls SVG rendering. For text frames, SVGOptions.setUseFrameSize includes the text frame in the rendering area, and SVGOptions.setUseFrameRotation determines whether the frame rotation is applied. Set SVGOptions.setDisableFontLigatures to True when text must be rendered without ligatures.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SVGOptions
from java.io import FileOutputStream
presentation = Presentation("presentation.pptx")
try:
svg_options = SVGOptions()
svg_options.setDisableFontLigatures(True)
svg_options.setUseFrameSize(True)
svg_options.setUseFrameRotation(False)
slide = presentation.getSlides().get_Item(0)
svg_stream = FileOutputStream("slide-with-custom-options.svg")
try:
slide.writeAsSvg(svg_stream, svg_options)
finally:
svg_stream.close()
finally:
presentation.dispose()
Control Text and Fonts
Vectorize All Text
Set SVGOptions.setVectorizeText to True to write all slide text as vector graphics. This eliminates font dependencies and makes the visual result more consistent across browsers, but the text is no longer selectable or searchable as SVG text.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SVGOptions
from java.io import FileOutputStream
presentation = Presentation("presentation.pptx")
try:
svg_options = SVGOptions()
svg_options.setVectorizeText(True)
slide = presentation.getSlides().get_Item(0)
svg_stream = FileOutputStream("slide-with-vectorized-text.svg")
try:
slide.writeAsSvg(svg_stream, svg_options)
finally:
svg_stream.close()
finally:
presentation.dispose()
Choose How External Fonts Are Handled
SVGOptions.setExternalFontsHandling uses a SvgExternalFontsHandling value for fonts that are loaded externally. Choose AddLinksToFontFiles to reference separate font files, Embed to include font data in the SVG, or Vectorize to render only text that uses external fonts as graphics. Verify font licensing before embedding fonts.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SVGOptions, SvgExternalFontsHandling
from java.io import FileOutputStream
presentation = Presentation("presentation.pptx")
try:
slide = presentation.getSlides().get_Item(0)
font_modes = [
("slide-with-font-links.svg", SvgExternalFontsHandling.AddLinksToFontFiles),
("slide-with-embedded-fonts.svg", SvgExternalFontsHandling.Embed),
("slide-with-vectorized-external-fonts.svg", SvgExternalFontsHandling.Vectorize),
]
for output_file_name, font_mode in font_modes:
svg_options = SVGOptions()
svg_options.setExternalFontsHandling(font_mode)
svg_stream = FileOutputStream(output_file_name)
try:
slide.writeAsSvg(svg_stream, svg_options)
finally:
svg_stream.close()
finally:
presentation.dispose()
Reduce Embedded Image Size
Use SVGOptions.setPicturesCompression to reduce the resolution of embedded pictures, SVGOptions.setDeletePicturesCroppedAreas to omit cropped source areas, and SVGOptions.setJpegQuality to control JPEG encoding quality. These settings reduce file size at the cost of image fidelity or retained image data.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import PicturesCompression, Presentation, SVGOptions
from java.io import FileOutputStream
presentation = Presentation("presentation.pptx")
try:
svg_options = SVGOptions()
svg_options.setPicturesCompression(PicturesCompression.Dpi150)
svg_options.setDeletePicturesCroppedAreas(True)
svg_options.setJpegQuality(80)
slide = presentation.getSlides().get_Item(0)
svg_stream = FileOutputStream("compressed-slide.svg")
try:
slide.writeAsSvg(svg_stream, svg_options)
finally:
svg_stream.close()
finally:
presentation.dispose()
Assign Stable IDs to Shapes and Text
Use a Python formatting controller registered through jpype.JProxy to assign SvgShape.setId values to shapes and SvgTSpan.setId values to text tspan elements. Assign the proxy with SVGOptions.setShapeFormattingController.
The following controller uses Shape.getOfficeInteropShapeId, which is stable for the lifetime of the shape, and a repeatable counter for its text spans. This makes the generated IDs suitable for post-processing an unchanged presentation.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SVGOptions
from java.io import FileOutputStream
class StableSvgIdController:
def __init__(self):
self.current_shape_id = ""
self.text_span_index = 0
def formatShape(self, svg_shape, shape):
self.current_shape_id = f"shape-{shape.getOfficeInteropShapeId()}"
self.text_span_index = 0
svg_shape.setId(self.current_shape_id)
def formatText(self, svg_tspan, portion, text_frame):
svg_tspan.setId(f"{self.current_shape_id}-text-{self.text_span_index}")
self.text_span_index += 1
presentation = Presentation("presentation.pptx")
try:
controller = StableSvgIdController()
proxy = jpype.JProxy("com.aspose.slides.ISvgShapeAndTextFormattingController", inst=controller)
svg_options = SVGOptions()
svg_options.setShapeFormattingController(proxy)
slide = presentation.getSlides().get_Item(0)
svg_stream = FileOutputStream("slide-with-stable-ids.svg")
try:
slide.writeAsSvg(svg_stream, svg_options)
finally:
svg_stream.close()
finally:
presentation.dispose()
Add SVG Event Handlers
In a Python formatting controller, call SvgShape.setEventHandler with a SvgEvent value to add a JavaScript event handler to an exported shape. Register the controller through jpype.JProxy and assign it with SVGOptions.setShapeFormattingController. Define the JavaScript function in the page or SVG document that hosts the result.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SVGOptions, SvgEvent
from java.io import FileOutputStream
class SvgEventController:
def formatShape(self, svg_shape, shape):
if shape.getName() == "ActionButton":
svg_shape.setId("action-button")
svg_shape.setEventHandler(SvgEvent.OnClick, "handleShapeClick(event)")
presentation = Presentation("presentation.pptx")
try:
controller = SvgEventController()
proxy = jpype.JProxy("com.aspose.slides.ISvgShapeFormattingController", inst=controller)
svg_options = SVGOptions()
svg_options.setShapeFormattingController(proxy)
slide = presentation.getSlides().get_Item(0)
svg_stream = FileOutputStream("interactive-slide.svg")
try:
slide.writeAsSvg(svg_stream, svg_options)
finally:
svg_stream.close()
finally:
presentation.dispose()
The host page can define the JavaScript function referenced by the handler. Assigning IDs and event handlers enables slide viewers, accessibility enhancements, and other interactive SVG workflows.
FAQ
When should I use SVGOptions.setVectorizeText instead of SvgExternalFontsHandling.Vectorize?
Use SVGOptions.setVectorizeText when all text must be independent of fonts. Use SvgExternalFontsHandling.Vectorize when only text that uses external fonts should be converted to graphics.
What is the best way to make an SVG smaller?
Start by compressing embedded pictures, deleting cropped image areas, and choosing linked font files when the target environment can serve them. Test the result because lower image resolution, lower JPEG quality, and vectorized text each have different quality and size tradeoffs.
Can I modify exported SVG elements after export?
Yes. Assign IDs through a formatting controller, then select the matching SVG elements in your post-processing tool or browser script.