'Automatic positioning of images and tables in PowerPoint slide officer
Hi so I have I am trying to write my own function that will automatically size and central align the images/tables that I am generating from officer. The top/left functions in the add_image and add_table are good when I add each table manually as The differing tables are differing sizes but I wasn't sure how to automatically do it for different shaped images and tables. I have the following code for images:
Image <- function(PP,title,footer,pageNO,path){
im <- load.image(path)
width <- imager::width(im)
height <- imager::height(im)
ratio <- width/height
if(ratio < 9.15 / 3.6){
y <- 3.6
x <- 3.6 * ratio
}
if(ratio >= 9.15 / 3.6){
y <- 9.15 / ratio
x <- 9.15
}
PP <- PP %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with_text(type = "title", str = title) %>%
ph_with_text(type = "dt", str = format(Sys.Date())) %>%
ph_with_img(type = "body", src = path, height = y, width = x) %>%
ph_with_text(type = "ftr",str = footer) %>%
ph_with_text(type = "sldNum", str = pageNO)
}
I would then like to align this image in the centre of the slide but am not sure how I would do this.
Then the same for tables but I have no idea how to do this as I would have to automatically rescale the width and height of columns based on original width and height of the flextable image as I wouldn't want to lose the aspect ratio unless there is a different way? Current code for this is: but it doesn't do any resizing:
Slide <- function(PP,title,footer,pageNO,table){
PP <- PP %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with_text(type = "title", str = title) %>%
ph_with_text(type = "ftr",str = footer) %>%
ph_with_text(type = "dt", str = format(Sys.Date())) %>%
ph_with_flextable(value = table, type = "body") %>%
ph_with_text(type = "sldNum", str = pageNO)
}
Many thanks in advance, any help would be greatly appreciated
Solution 1:[1]
Old question, but if anyone's looking here's a reproducible example that does the following:
- Create a new PowerPoint deck;
- Create a
flextable
table that we'd like to include; - Put the table in the centre of a new slide; and,
- Write it to file.
You can extend this to solve more particular problems, e.g. by using PowerPoint templates, using officer::layout_properties()
to get more details about layouts, and so on.
library(tidyverse)
library(officer)
library(flextable)
# create a PowerPoint slide deck
deck <- officer::read_pptx()
# make a fletable with the first 5 rows of mtcars
deck_table <- mtcars %>%
head(5) %>%
flextable::flextable()
# get table dimensions
table_width <- sum(dim(deck_table)$widths)
table_height <- sum(dim(deck_table)$heights)
# get slide dimensions in inches
# these are from PowerPoint defaults for 3:4; widescreen width is 13.333 in
slide_width <- 10
slide_height <- 7.5
# find the left and top margins to centre the table
table_leftmargin <- (slide_width - table_width)/2
table_topmargin <- (slide_height - table_height)/2
# add a slide, then add the table at the desired location
deck %>%
officer::add_slide() %>%
officer::ph_with(value = deck_table,
location = officer::ph_location(left = table_leftmargin,
top = table_topmargin))
# save to file
print(deck, "test.pptx")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Christopher Belanger |