Search code examples
rpowerpointofficer

How to add formatted paragraph (fpar) to Power Point presentation, right aligned, inside a place holder


I have the following code in R, using the library officeR, to create power point presentations.

No matter how I set the property "text.align" of the fp_par object (of fpar), the title remains centered.

Is there a way to add the new paragraph of text, right aligned, in a place holder added with the function add_ph_empty_at()?

Thank you!

require(magrittr)
require(officer)

def_text <- fp_text(color = "black", italic = FALSE, font.size = 20)

lastPhId <- function(presentation) {
  index = presentation$cursor
  x <- slide_summary(presentation, index = index)
  x <- x[x$type == "body", ]
  max(as.numeric(x$id))
}

TITLE = fpar(ftext("My Title", prop = def_text))
TITLE <- update(TITLE, fp_p = fp_par(text.align = "left"))

doc <- read_pptx()
doc %<>% add_slide(layout = "Title and Content", master = "Office Theme") 
doc %<>% ph_empty_at(left = 3, top = 3, height = 1, width = 4, bg = "yellow") 
doc %<>% ph_add_fpar(value = TITLE, id_chr = lastPhId(doc))

print(doc, target = "ph_add_fpar.pptx")
system("cmd.exe", input = "ph_add_fpar.pptx")

Solution

  • I have just updated officer on Github, it's now solved. You will need to use new argument par_default that says (if FALSE): don't use the placeholder default paragraph properties but use the one of the fparobject.

    require(magrittr)
    require(officer)
    
    def_text <- fp_text(color = "black", italic = FALSE, font.size = 20)
    
    lastPhId <- function(presentation) {
      index = presentation$cursor
      x <- slide_summary(presentation, index = index)
      x <- x[x$type == "body", ]
      max(as.numeric(x$id))
    }
    
    TITLE = fpar(ftext("My Title", prop = def_text))
    TITLE <- update(TITLE, fp_p = fp_par(text.align = "left"))
    
    doc <- read_pptx()
    doc %<>% add_slide(layout = "Title and Content", master = "Office Theme") 
    doc %<>% ph_empty_at(left = 3, top = 3, height = 1, width = 4, bg = "yellow") 
    doc %<>% ph_add_fpar(value = TITLE, id_chr = lastPhId(doc), par_default = FALSE)
    
    print(doc, target = "ph_add_fpar.pptx") %>% browseURL()