来自 python 的 PowerPoint 中的可编辑图:相当于 officer 和 rvg

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

我正在使用 officerrvg 包将绘图从 R 导入 MS PowerPoint 作为可编辑矢量图形。下面是可重现的例子。

我正在寻找一种方法来实现与 python 等效的解决方案,最好使用 matplotlib。关键部分不是从 IDE 创建幻灯片,而是可编辑的矢量图形部分,即绘图最终应该作为由一系列简单的 PowerPoint 几何图形(例如线条、正方形和文本)组成的分组对象出现在 PowerPoint 中字段。

R 示例:

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')

生成的图表是完全可编辑的:

自 2019 版 MS office 起,您可以将 svg 文件添加为图像,然后取消组合它们以使其可编辑。请参阅此 MS page 上的 'Convert an SVG image to an Office shape'。然而,根据我个人的经验,这 既不稳定也不完整

另一种方法更稳定和完整,但仍不完美,将其作为emf文件导入到powerpoint中,这也是一种矢量格式。旧版本的 matplotlib 可以导出为这种格式。对于较新的版本,我导出为 svg,然后使用 inkscape --file "input.svg" --export-emf "output.emf" 转换为 emf,然后我将其加载到 powerpoint 中。如果一切正常,再次取消组合对象允许您进行编辑。