使用R语言officer包替换文本报错:is_scalar_character(new_value) is not TRUE
解决officer包
body_replace_all_text报错:is_scalar_character(new_value) is not TRUE 我用R的officer包替换Word模板文本,之前使用正常,今天遇到报错:
Error in body_replace_all_text(., "Zahl", data_base$Einheiten[[x]]) : is_scalar_character(new_value) is not TRUE
我的代码如下:
library(dplyr) print_docx_data_base <- lapply(1:nrow(data_base), function(x) { # empty_docs <- officer::read_docx() # empty_docs <- officer::read_docx(docx_template) empty_docs <- officer::read_docx("Teilnahmebestaetigung_template.docx") sample_doc <- empty_docs %>% body_replace_all_text("Person",data_base$person[[x]]) sample_doc <- empty_docs %>% body_replace_all_text("Veranstaltungsname",data_base$Kursart[[x]]) sample_doc <- empty_docs %>% body_replace_all_text("Zahl",data_base$Einheiten[[x]]) sample_doc <- empty_docs %>% body_replace_all_text("Daten",data_base$Tage[[x]]) sample_doc <- empty_docs %>% body_replace_all_text("Ort",data_base$Ort[[x]]) sample_doc <- empty_docs %>% body_replace_all_text("Datum",format(Sys.Date(),format = "%d.%m.%Y")) sample_doc <- empty_docs %>% body_replace_all_text("Firmenname",data_base$Firmenname[[x]]) sample_doc <- empty_docs %>% body_replace_all_text("Strasse",data_base$Strasse[[x]]) sample_doc <- empty_docs %>% body_replace_all_text("PLZ",as.character(data_base$PLZ[[x]])) print(sample_doc, target = file_name[[x]]) if (any(list.files() == file_name[[x]])) {cat(paste("File is saved.\n"))} } )
测试数据:
data_base <- structure(list(nr = c(3, 1, 4, 2), kursteilnehmer = paste(c("Some name"),c(1:4)), titel_1 = c("NA", "Magª. Dr. ", NA, "Magª."), titel_2 = c("NA", "NA", "M.A.", "NA"), Kursart = rep("Some course", 4), Ort = rep("Some City", 4), Firmenname = rep("Some company", 4), Strasse = rep("Some street", 4), PLZ = rep("Some ZIP", 4), person = c(paste(data_base$titel_1,kursteilnehmer,data_base$titel_2)), Einheiten = c(4, 5, 4, 4), Tage = c("23.09.2022, 30.09.2022, 07.10.2022, 14.10.2022", "23.09.2022, 30.09.2022, 07.10.2022, 14.10.2022, 21.10.2022", "23.09.2022, 30.09.2022, 14.10.2022, 21.10.2022", "23.09.2022, 30.09.2022, 14.10.2022, 21.10.2022")), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"))
报错原因及解决办法
1. 类型不匹配导致报错
body_replace_all_text的new_value参数要求是单个字符型值,但data_base$Einheiten是数值型列,直接取data_base$Einheiten[[x]]得到的是数值类型,不符合函数要求,这是报错的直接原因。
解决:将数值转换为字符型,要么在替换时用as.character(data_base$Einheiten[[x]]),要么提前把Einheiten列转为字符型:
data_base <- data_base %>% mutate(Einheiten = as.character(Einheiten))
2. 替换逻辑错误(隐性问题)
你的代码每次替换都重新从模板读取empty_docs,导致之前的替换全部丢失,最终只有最后一次替换生效。
解决:替换操作要基于上一次修改后的sample_doc对象,而不是每次都用原始模板。
修正后的完整代码
library(dplyr) library(officer) # 提前转换数值列为字符型 data_base <- data_base %>% mutate(Einheiten = as.character(Einheiten)) print_docx_data_base <- lapply(1:nrow(data_base), function(x) { sample_doc <- read_docx("Teilnahmebestaetigung_template.docx") %>% body_replace_all_text("Person", data_base$person[[x]]) %>% body_replace_all_text("Veranstaltungsname", data_base$Kursart[[x]]) %>% body_replace_all_text("Zahl", data_base$Einheiten[[x]]) %>% body_replace_all_text("Daten", data_base$Tage[[x]]) %>% body_replace_all_text("Ort", data_base$Ort[[x]]) %>% body_replace_all_text("Datum", format(Sys.Date(), format = "%d.%m.%Y")) %>% body_replace_all_text("Firmenname", data_base$Firmenname[[x]]) %>% body_replace_all_text("Strasse", data_base$Strasse[[x]]) %>% body_replace_all_text("PLZ", as.character(data_base$PLZ[[x]])) print(sample_doc, target = file_name[[x]]) if (file.exists(file_name[[x]])) { cat(paste("File", file_name[[x]], "is saved.\n")) } })
内容的提问来源于stack exchange,提问作者Nadiine El Nino
相关产品推荐
相关产品推荐

