rpptx is an R package for creating, reading, and updating PowerPoint (.pptx) presentations. It is a close R port of the Python python-pptx library, providing an R6-based object model that mirrors the Python API.
Installation
# Development version from GitHub:
# install.packages("pak")
pak::pak("markheckmann/rpptx")Quick start
library(rpptx)
# ---- Open / create -----------------------------------------------------------
prs <- pptx_presentation() # blank presentation
prs <- pptx_presentation("my_file.pptx") # open existing
# ---- Add a slide -------------------------------------------------------------
layout <- prs$slide_layouts[[1]] # "Title Slide" layout
slide <- prs$slides$add_slide(layout)
# ---- Add shapes --------------------------------------------------------------
# Text box
box <- slide$shapes$add_textbox(Inches(1), Inches(1), Inches(4), Inches(1))
tf <- box$text_frame
tf$text <- "Hello, rpptx!"
# Autoshape (filled rectangle)
shp <- slide$shapes$add_shape(
MSO_AUTO_SHAPE_TYPE$ROUNDED_RECTANGLE,
Inches(1), Inches(2.5), Inches(3), Inches(1.5)
)
shp$fill$solid()
shp$fill$fore_color$rgb <- RGBColor(0x4F, 0x81, 0xBD)
# ---- Save --------------------------------------------------------------------
prs$save("output.pptx")Features
Presentations
prs$slide_width # Emu (use .emu_in() or as.numeric() / 914400)
prs$slide_height
prs$slides # collection: prs$slides[[1]], length(prs$slides)
prs$slide_layouts # 11 default layouts
prs$slide_masters
prs$core_properties$title <- "My Presentation"Text and fonts
tf <- shape$text_frame
tf$text <- "Replace all text"
tf$word_wrap <- TRUE
tf$auto_size <- MSO_AUTO_SIZE$SHAPE_TO_FIT_TEXT # expand shape to fit text
tf$vertical_anchor <- MSO_VERTICAL_ANCHOR$MIDDLE
para <- tf$paragraphs[[1]]
para$alignment <- PP_ALIGN$CENTER
run <- para$add_run()
run$text <- "Bold blue text"
run$font$bold <- TRUE
run$font$size <- Pt(24)
run$font$color$rgb <- RGBColor(0xFF, 0x00, 0x00)Charts
# Column chart
chart_data <- CategoryChartData$new()
chart_data$categories <- c("East", "West", "North")
chart_data$add_series("Q1", c(19.2, 21.4, 16.7))
chart_data$add_series("Q2", c(22.3, 28.6, 15.2))
gf <- slide$shapes$add_chart(
XL_CHART_TYPE$COLUMN_CLUSTERED,
Inches(1), Inches(1), Inches(8), Inches(5),
chart_data
)
# Scatter (XY) chart
xy_data <- XyChartData$new()
s <- xy_data$add_series("Series 1")
s$add_data_point(1.0, 2.3)
s$add_data_point(1.5, 1.8)
slide$shapes$add_chart(XL_CHART_TYPE$XY_SCATTER, ...)
# Bubble chart
bd <- BubbleChartData$new()
s <- bd$add_series("Bubbles")
s$add_data_point(x = 1.0, y = 2.0, size = 10)
s$add_data_point(x = 2.0, y = 3.0, size = 20)
slide$shapes$add_chart(XL_CHART_TYPE$BUBBLE, ...)Group shapes
# Group two existing shapes
grp <- slide$shapes$add_group_shape(list(shape1, shape2))Relationship to python-pptx
rpptx is a direct R port of python-pptx. The object model, property names, and API are intentionally kept as close as possible to the Python original. Key differences:
| python-pptx | rpptx |
|---|---|
Presentation() |
pptx_presentation() |
prs.slides[0] |
prs$slides[[1]] (1-based) |
shape.text_frame |
shape$text_frame |
font.bold = True |
font$bold <- TRUE |
Inches(1.5) |
Inches(1.5) |
MSO_SHAPE.RECTANGLE |
MSO_AUTO_SHAPE_TYPE$RECTANGLE |
