简体   繁体   English

使用 R 上的官员包在 pptx 上使用多种格式的文本

[英]Mutliple formatted text on pptx by using officer package on R

Ciao,再见,

I'm writing a code to generate an automatic report using officer package.我正在编写代码以使用官员包生成自动报告。 I was wondering how and if I can write some text with different font.我想知道如何以及是否可以用不同的字体编写一些文本。 In my case I'd like to write some normal text and some bold words.就我而言,我想写一些普通文本和一些粗体字。

Let me show you what I get.让我告诉你我得到了什么。 I use these functions to generate fp_text objects:我使用这些函数来生成 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") )
}

I used to use combination of pot function by using sum operator and function textProperties :我曾经通过使用 sum 运算符和函数textProperties来使用pot函数的组合:

pot("not bold ") + pot("and bold", textProperties(font.weight = "bold") )

My question is: how should I combine fp_normal and fp_bold functions with ph_with_text function?我的问题是:我应该如何将fp_normalfp_bold函数与ph_with_text函数结合起来?

I have updated the package to make that kind of operation easier.我已经更新了这个包,使这种操作更容易。 Usage of id_chr is not easy and the code below give the advantage to not use it :) 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")

结果

Ok, at the end I think I've got it.好吧,最后我想我明白了。

To apply different styles is in enough to combine ph_with_text function with ph_add_text function.应用不同的样式足以将ph_with_text函数与ph_add_text函数结合起来。

Namely ph_add_text do the same sum operator do for pot function.ph_add_text函数执行相同的求和运算符。 Keep in mind that, in order to refer to a certain line you have to provide id_chr argument.请记住,为了引用某一行,您必须提供id_chr参数。 You can deduce the correct value by using slide_summary(ppt) command just after you run ph_with_text .您可以在运行ph_with_text之后使用slide_summary(ppt)命令推断出正确的值。

ppt <- read_pptx()
ppt <- add_slide(ppt, "Title and Content", master = "Office Theme")
ppt <- ph_with_text(ppt, "Some NOT bold text ", type = "body", index = 1)

slide_summary(ppt) # I see that the id is 2. Now I can delete this line.

ppt <- ph_add_text(ppt, "and some bold text", type = "body", style = fp_bold(), id_chr = 2)
print(ppt, target = "boldTest.pptx")

For the fp_bold() function see above in the question.对于fp_bold()函数,请参见上面的问题。 Ad this point we can add other pieces of text with different formats by keeping using ph_add_text (and maybe "\\n" if we want write in new lines.在这一点上,我们可以通过继续使用ph_add_text来添加其他不同格式的文本(如果我们想在新行中写入,也可以使用“\\n”。

Ciao再见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM