我正在尝试使用R中的Officer包为一组单词(例如,导入的单词文档中的word1和word2)着色。在设置单词的文本属性后,我现在陷入困境。
我已经使用read_docx(filename)
读取了数据文件,其中filename是一个txt文件。我还使用fp_text()
和ftext()
设置了文本属性。
my_doc <- read_docx(file.path(filepath,myfile))
color1 <- fp_text(color = "blue", font.size = 10, bold = FALSE,
italic = FALSE, underlined = FALSE, font.family = "Arial",
vertical.align = "baseline", shading.color = "transparent")
ftext("word1", color1)
color2 <- fp_text(color = "red", font.size = 10, bold = FALSE,
italic = FALSE, underlined = FALSE, font.family = "Arial",
vertical.align = "baseline", shading.color = "transparent")
ftext("word2", color1)
但是,如果我的文本是This is my text. word1 should be colored blue, and word2 should be colored red
之类的文本,如何将文本中的单词设置为颜色,以及如何将其保存到带有颜色的格式化Word文档中?
答案 0 :(得分:0)
你是这个意思吗?
color1 <- fp_text(color = "blue", font.size = 10, bold = FALSE,
italic = FALSE, underlined = FALSE, font.family = "Arial",
vertical.align = "baseline", shading.color = "transparent")
ftext("word1", color1)
color2 <- fp_text(color = "red", font.size = 10, bold = FALSE,
italic = FALSE, underlined = FALSE, font.family = "Arial",
vertical.align = "baseline", shading.color = "transparent")
ftext("word2", color2)
fpar_1 <- fpar(ftext("word1", color1), "?")
fpar_2 <- fpar(ftext("word2", color2), "?")
my_doc <- read_docx() %>%
body_add_fpar(fpar_1) %>%
body_add_fpar(fpar_2)