簡體   English   中英

使用 R 上的官員包在 pptx 上使用多種格式的文本

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

再見,

我正在編寫代碼以使用官員包生成自動報告。 我想知道如何以及是否可以用不同的字體編寫一些文本。 就我而言,我想寫一些普通文本和一些粗體字。

讓我告訴你我得到了什么。 我使用這些函數來生成 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") )
}

我曾經通過使用 sum 運算符和函數textProperties來使用pot函數的組合:

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

我的問題是:我應該如何將fp_normalfp_bold函數與ph_with_text函數結合起來?

我已經更新了這個包,使這種操作更容易。 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")

結果

好吧,最后我想我明白了。

應用不同的樣式足以將ph_with_text函數與ph_add_text函數結合起來。

ph_add_text函數執行相同的求和運算符。 請記住,為了引用某一行,您必須提供id_chr參數。 您可以在運行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")

對於fp_bold()函數,請參見上面的問題。 在這一點上,我們可以通過繼續使用ph_add_text來添加其他不同格式的文本(如果我們想在新行中寫入,也可以使用“\\n”。

再見

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM