Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editable plots in PowerPoint from python: equivalent of officer and rvg

I am using the officer and rvg packages to get plots from R into MS PowerPoint as editable vector graphics. Reproducible example below.

I am looking for a way to implement an equivalent solution with python, preferably using matplotlib. The critical part is not the creation of slides from the IDE but rather the editable vector graphics part, i.e. plots should end up in PowerPoint as grouped objects comprised of a range of simple powerpoint geometries such as lines, squares, and text fields.

R example:

library(tidyverse)
library(officer)
library(rvg)

# Get some data and make a plot
ggp <- diamonds %>% 
  group_by(clarity) %>%
  summarise(price = mean(price)) %>%
  ggplot(aes(x = clarity, y = price, fill = clarity)) +
  geom_bar(stat = 'identity', colour = 'black')

# Create a new powerpoint document
doc <- read_pptx()
doc <- add_slide(doc, 'Title and Content', 'Office Theme')
# Add the plot 
doc <- ph_with_vg(doc, ggobj = ggp, type = 'body')  

# Write the document to a file
print(doc, target = 'plots.pptx')

The resulting chart is completely editable:

enter image description here

like image 813
JanLauGe Avatar asked Feb 23 '18 08:02

JanLauGe


1 Answers

Since the 2019 version of MS office you can add svg files as images and then ungroup them to make them editable. See 'Convert an SVG image to an Office shape' on this MS page. In my personal experience, this is however neither very stable nor complete.

Another way, which is more stable and complete although still not perfect, is importing it in powerpoint as emf file, which is also a vector-format. Old version of matplotlib could export to this format. For newer version, I export as svg and then use inkscape --file "input.svg" --export-emf "output.emf" to convert to emf, which I then load in powerpoint. Ungrouping the object again allows you to edit, if everything works well.

like image 194
Koen G. Avatar answered Nov 14 '22 03:11

Koen G.