通过在R上使用Officer包在pptx上使用多格式文本

cla*_*aio 4 powerpoint r reporters officer

再见,

我正在编写代码,以使用Officer包生成自动报告。我想知道如何以及是否可以用不同的字体写一些文本。就我而言,我想写一些普通的文字和一些粗体字。

让我告诉你我得到的。我使用以下函数来生成fp_text对象:

fp_normal <- function(){
return( fp_text(color = "black", font.size = 16,
        bold = FALSE, italic = FALSE,
        underlined = FALSE, font.family = "Arial", 
        shading.color = "transparent") )
}


fp_bold <- function(){
return( fp_text(color = "black", font.size = 16,
        bold = TRUE, italic = FALSE,
        underlined = FALSE, font.family = "Arial", 
        shading.color = "transparent") )
}
Run Code Online (Sandbox Code Playgroud)

我以前通过使用sum运算符和函数textProperties来组合使用pot函数:

pot("not bold ") + pot("and bold", textProperties(font.weight = "bold") )
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何将fp_normalfp_bold函数与ph_with_text函数组合?

Dav*_*hel 5

我已经更新了软件包,以使这种操作更加容易。使用id_chr不容易,下面的代码具有不使用它的优势:)

library(magrittr)
library(officer)

fp_normal <- fp_text(font.size = 24)
fp_bold <- update(fp_normal, bold = TRUE)
fp_red <- update(fp_normal, color = "red")

pars <- block_list(
  fpar(ftext("not bold ", fp_normal), ftext("and bold", fp_bold)),
  fpar(ftext("red text", fp_red))
)
my_pres <- read_pptx() %>%
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  ph_with(pars, location = ph_location_type(type = "body") ) 

print(my_pres, target = "test.pptx")
Run Code Online (Sandbox Code Playgroud)

结果