Marc
Marc

Reputation: 103

Python to Powerpoint: Export svg to a pptx file

I was experimenting with the python-pptx library to automate the process of adding images to my presentations. I used code such as:

from pptx import Presentation
import os
 
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[8])
placeholder = slide.placeholders[1]
picture = placeholder.insert_picture('pie.png')
prs.save("ESEMPIO.pptx")

This works just fine, but it was noted that when zooming in the pie chart contained in pie.png becomes quite pixelated. Someone suggested to use SVG format, so I saved the image as an SVG (unfortunately, I can't seem to add the SVG file here). Now, if I try to do:

from pptx import Presentation
import os
 
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[8])
placeholder = slide.placeholders[1]
picture = placeholder.insert_picture('pie.svg')
prs.save("ESEMPIO.pptx")

I get the following error: UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f38da87c350>, which I guess is because it does not like the SVG format. Is there any workaround to get an SVG into a PowerPoint presentation?

PNG pie chart

Upvotes: 3

Views: 3287

Answers (3)

anne_717
anne_717

Reputation: 11

A long-winded way to do this is to save your image as an emf (this involves calling Inkscape to do so but the process is explained quite well in this thread: Convert SVG/PDF to EMF) and then use this emf when you call insert_picture. This image should render as a vectorized image in your powerpoint- meaning that it scales and remains editable (you'll need to right-click the image and select Group --> Ungroup if you want to edit).

Upvotes: 1

Martin Packer
Martin Packer

Reputation: 763

In md2pptx we automate the conversion of SVG to .PNG under the covers. It uses CairoSVG to do it.

md2pptx relies heavily on python-pptx to convert Markdown to .pptx.

You can get md2pptx here:

https://github.com/MartinPacker/md2pptx

Sorry if this seems like an advert. I just wanted you to know there is automation available.

Upvotes: 0

Adid
Adid

Reputation: 1584

According to this issue on the repository, svg support doesn't seem to be a priority for the devs, and they suggest manually converting to png and inserting that into your presentation. My best suggestion is converting to a relatively high resolution png so the pixelation is less noticeable.

Upvotes: 3

Related Questions