From 66acdea557226fb5b7d55788ab51f349b02a741a Mon Sep 17 00:00:00 2001 From: David Gohel Date: Sun, 5 May 2024 13:21:02 +0200 Subject: [PATCH] feat: `remove_slide()` gains new argument `rm_images` to enable images deletion fix #565 --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/pptx_slide_manip.R | 11 ++++++++++- man/remove_slide.Rd | 5 ++++- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index fd7c6833..a57316c1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: officer Type: Package Title: Manipulation of Microsoft Word and PowerPoint Documents -Version: 0.6.6.003 +Version: 0.6.6.004 Authors@R: c( person("David", "Gohel", role = c("aut", "cre"), email = "david.gohel@ardata.fr"), person("Stefan", "Moog", role = "aut", email = 'moogs@gmx.de'), diff --git a/NEWS.md b/NEWS.md index e1571329..60514193 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,6 +9,8 @@ - add coverage to rtf, contributions from Davide Garolini at roche. - `cursor_reach()` gains new argument `fixed` to enable exact matching. It defaults to FALSE for backward compatibility. +- `remove_slide()` gains new argument `rm_images` to enable images deletion +from ppt/media. # officer 0.6.5 diff --git a/R/pptx_slide_manip.R b/R/pptx_slide_manip.R index 63de65b4..229d31bd 100644 --- a/R/pptx_slide_manip.R +++ b/R/pptx_slide_manip.R @@ -88,6 +88,8 @@ on_slide <- function(x, index) { #' @description Remove a slide from a pptx presentation. #' @param x an rpptx object #' @param index slide index, default to current slide position. +#' @param rm_images if TRUE (defaults to FALSE), images presented in +#' the slide to remove are also removed from the file. #' @note cursor is set on the last slide. #' @examples #' my_pres <- read_pptx() @@ -95,7 +97,7 @@ on_slide <- function(x, index) { #' my_pres <- remove_slide(my_pres) #' @family functions slide manipulation #' @seealso [read_pptx()], [ph_with()], [ph_remove()] -remove_slide <- function(x, index = NULL) { +remove_slide <- function(x, index = NULL, rm_images = FALSE) { l_ <- length(x) if (l_ < 1) { stop("presentation contains no slide to delete", call. = FALSE) @@ -111,6 +113,13 @@ remove_slide <- function(x, index = NULL) { filename <- basename(x$presentation$slide_data()$target[index]) location <- which(x$slide$get_metadata()$name %in% filename) + if (rm_images) { + media_files <- pptx_summary(x)$media_file + if (length(media_files)) { + unlink(file.path(x$package_dir, media_files), force = TRUE) + } + } + del_file <- x$slide$remove_slide(location) # update presentation elements diff --git a/man/remove_slide.Rd b/man/remove_slide.Rd index 49f46059..8e63ded0 100644 --- a/man/remove_slide.Rd +++ b/man/remove_slide.Rd @@ -4,12 +4,15 @@ \alias{remove_slide} \title{Remove a slide} \usage{ -remove_slide(x, index = NULL) +remove_slide(x, index = NULL, rm_images = FALSE) } \arguments{ \item{x}{an rpptx object} \item{index}{slide index, default to current slide position.} + +\item{rm_images}{if TRUE (defaults to FALSE), images presented in +the slide to remove are also removed from the file.} } \description{ Remove a slide from a pptx presentation.