How can i change in R the font family of a Title with officer?

十年热恋 提交于 2021-01-28 09:08:52

问题


How can I change the font family of a title in R with officer? I'm trying this with the function fp_text(font.family = "Arial"), but the problem is that the title which I define with fp_text does not end up in the table of contents.


回答1:


It's kind of a pain, but the way I do it is:

  1. Add an empty placeholder in the title slot with ph_empty()
  2. Add formatted text with ph_add_fpar(), using fpar(), ftext(), and fp_text() to create the formatted text object.

Here is an example of how to change the title on a Title Slide and on a Title and Content slide, assuming the fonts you want to use are "Rage Italic" and "Goudy Stout":

library(officer)
library(magrittr)

# the formatting you want to use goes here -- check your fonts()
format_main_title <- fp_text(font.family='Rage Italic', font.size=72)
format_page_title <- fp_text(font.family='Goudy Stout', font.size=24)

read_pptx() %>% 
  add_slide(layout = 'Title Slide', master='Office Theme') %>%
  ph_empty(type='ctrTitle') %>%
  ph_add_fpar(fpar(ftext('Fancy Main Title', prop=format_main_title)), 
                      type = 'ctrTitle') %>%
  add_slide(layout = 'Title and Content', master='Office Theme') %>%
  ph_empty(type='title') %>%
  ph_add_fpar(fpar(ftext('Fancy Page Title', prop=format_page_title)), 
                                     type = 'title')  %>%
  ph_with_text(type = 'body', str = 'Boring stuff goes here') %>%
  print('test.pptx')

produces:

You can see these titles in the table of contents:

Having said that -- If you find yourself consistently changing title fonts to the same new formatting you would probably be better off creating a template deck with your own Slide Master (versus the default "Office Theme") that uses your desired font and beginning your chain with that (i.e., read_pptx('your_template.pptx') %>% etc.)



来源:https://stackoverflow.com/questions/50726187/how-can-i-change-in-r-the-font-family-of-a-title-with-officer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!