嵌套for循环使用officer为ppt报告添加幻灯片编号
Nested for loop to add slide numbers for ppt reports using officer
使用下面的代码,我可以使用 officer
包动态生成多个 ppt 报告:
url_list <- c(
'http://www.csrc.gov.cn/csrc/c101921/c1758587/1758587/files/2022%E5%B9%B4%E7%AC%AC1%E5%91%A8((2022-01-04%E8%87%B32022-01-07)%E6%9C%9F%E8%B4%A7%E5%B8%82%E5%9C%BA%E4%B8%BB%E8%A6%81%E5%93%81%E7%A7%8D%E4%BA%A4%E6%98%93%E7%BB%9F%E8%AE%A1%E6%83%85%E5%86%B5%E8%A1%A8.xlsx',
'http://www.csrc.gov.cn/csrc/c101921/c1714636/1714636/files/2021%E5%B9%B4%E7%AC%AC52%E5%91%A8((2021-12-27%E8%87%B32021-12-31)%E6%9C%9F%E8%B4%A7%E5%B8%82%E5%9C%BA%E4%B8%BB%E8%A6%81%E5%93%81%E7%A7%8D%E4%BA%A4%E6%98%93%E7%BB%9F%E8%AE%A1%E6%83%85%E5%86%B5%E8%A1%A8.xlsx',
'http://www.csrc.gov.cn/csrc/c101921/c1664367/1664367/files/2021%E5%B9%B4%E7%AC%AC51%E5%91%A8(2021-12-20%E8%87%B32021-12-24)%E6%9C%9F%E8%B4%A7%E5%B8%82%E5%9C%BA%E4%B8%BB%E8%A6%81%E5%93%81%E7%A7%8D%E4%BA%A4%E6%98%93%E7%BB%9F%E8%AE%A1%E6%83%85%E5%86%B5%E8%A1%A8.XLS'
)
# https://davidgohel.github.io/officer/reference/ph_location_type.html
loc_dt <- ph_location_type(type = "dt") # date
loc_slidenum <- ph_location_type(type = "sldNum") #loc_slidenum
for (url_path in url_list) {
for (i in (1:length(url_list))) {
df <- openxlsx::read.xlsx(url_path, rows = c(5:7),
cols = c(5:7), na.strings = "-")
temp <- read_pptx()
report <- add_slide(temp, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value='content', location = ph_location_label(ph_label = "Content Placeholder 2"))
report <- add_slide(report, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = df, location = ph_location_label(ph_label = "Content Placeholder 2"))
n_slides <- length(report)
for (i_slide in 1:n_slides) {
report <- report %>%
ph_with(value = format(Sys.Date()), location = loc_dt) %>%
ph_with(value = i_slide, location = loc_slidenum)
}
print(report, target=glue('report{i}.pptx'))
}
}
输出:
您可能会注意到,幻灯片编号(由 for (i_slide in 1:n_slides)
添加)显示有拼写错误,似乎与编号 1
和 2
.
重叠
有人能帮我弄清楚为什么会出现这个错误吗?另外,如果可能的话,请帮助将我的代码转换为 purrr::map()
或 purrr:iwalk()
。谢谢。
如果我们想要获取 'url_list' 的值和索引,而不是嵌套循环,只需循环 'url_list' 的序列,其中 returns 索引和使用该索引对相应的 'url' 进行子集化。此外,更改 read.xlsx
中的 colNames = FALSE
默认情况下为 TRUE,因此第一行将作为列名
for (i in seq_along(url_list)) {
# create the 'url_path' so that we don't have to change the OP's code below
url_path <- url_list[i]
df <- openxlsx::read.xlsx(url_path, rows = c(5:7),
cols = c(5:7), na.strings = "-", colNames = FALSE)
temp <- read_pptx()
report <- add_slide(temp, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value='content', location = ph_location_label(ph_label = "Content Placeholder 2"))
report <- add_slide(report, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = df, location = ph_location_label(ph_label = "Content Placeholder 2"))
n_slides <- length(report)
for (i_slide in 2:n_slides) {
report <- report %>%
ph_with(value = format(Sys.Date()), location = loc_dt) %>%
ph_with(value = i_slide, location = loc_slidenum)
}
print(report, target=glue('report{i}.pptx'))
}
或使用iwalk
library(purrr)
iwalk(url_list, ~ {
url_path <- .x
i <- .y
df <- openxlsx::read.xlsx(url_path, rows = c(5:7),
cols = c(5:7), na.strings = "-", colNames = FALSE)
temp <- read_pptx()
report <- add_slide(temp, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value='content', location = ph_location_label(ph_label = "Content Placeholder 2"))
report <- add_slide(report, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = df, location = ph_location_label(ph_label = "Content Placeholder 2"))
n_slides <- length(report)
for (i_slide in 2:n_slides) {
report <- report %>%
ph_with(value = format(Sys.Date()), location = loc_dt) %>%
ph_with(value = i_slide, location = loc_slidenum)
}
print(report, target=glue('report{i}.pptx'))
})
-输出
使用下面的代码,我可以使用 officer
包动态生成多个 ppt 报告:
url_list <- c(
'http://www.csrc.gov.cn/csrc/c101921/c1758587/1758587/files/2022%E5%B9%B4%E7%AC%AC1%E5%91%A8((2022-01-04%E8%87%B32022-01-07)%E6%9C%9F%E8%B4%A7%E5%B8%82%E5%9C%BA%E4%B8%BB%E8%A6%81%E5%93%81%E7%A7%8D%E4%BA%A4%E6%98%93%E7%BB%9F%E8%AE%A1%E6%83%85%E5%86%B5%E8%A1%A8.xlsx',
'http://www.csrc.gov.cn/csrc/c101921/c1714636/1714636/files/2021%E5%B9%B4%E7%AC%AC52%E5%91%A8((2021-12-27%E8%87%B32021-12-31)%E6%9C%9F%E8%B4%A7%E5%B8%82%E5%9C%BA%E4%B8%BB%E8%A6%81%E5%93%81%E7%A7%8D%E4%BA%A4%E6%98%93%E7%BB%9F%E8%AE%A1%E6%83%85%E5%86%B5%E8%A1%A8.xlsx',
'http://www.csrc.gov.cn/csrc/c101921/c1664367/1664367/files/2021%E5%B9%B4%E7%AC%AC51%E5%91%A8(2021-12-20%E8%87%B32021-12-24)%E6%9C%9F%E8%B4%A7%E5%B8%82%E5%9C%BA%E4%B8%BB%E8%A6%81%E5%93%81%E7%A7%8D%E4%BA%A4%E6%98%93%E7%BB%9F%E8%AE%A1%E6%83%85%E5%86%B5%E8%A1%A8.XLS'
)
# https://davidgohel.github.io/officer/reference/ph_location_type.html
loc_dt <- ph_location_type(type = "dt") # date
loc_slidenum <- ph_location_type(type = "sldNum") #loc_slidenum
for (url_path in url_list) {
for (i in (1:length(url_list))) {
df <- openxlsx::read.xlsx(url_path, rows = c(5:7),
cols = c(5:7), na.strings = "-")
temp <- read_pptx()
report <- add_slide(temp, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value='content', location = ph_location_label(ph_label = "Content Placeholder 2"))
report <- add_slide(report, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = df, location = ph_location_label(ph_label = "Content Placeholder 2"))
n_slides <- length(report)
for (i_slide in 1:n_slides) {
report <- report %>%
ph_with(value = format(Sys.Date()), location = loc_dt) %>%
ph_with(value = i_slide, location = loc_slidenum)
}
print(report, target=glue('report{i}.pptx'))
}
}
输出:
您可能会注意到,幻灯片编号(由 for (i_slide in 1:n_slides)
添加)显示有拼写错误,似乎与编号 1
和 2
.
有人能帮我弄清楚为什么会出现这个错误吗?另外,如果可能的话,请帮助将我的代码转换为 purrr::map()
或 purrr:iwalk()
。谢谢。
如果我们想要获取 'url_list' 的值和索引,而不是嵌套循环,只需循环 'url_list' 的序列,其中 returns 索引和使用该索引对相应的 'url' 进行子集化。此外,更改 read.xlsx
中的 colNames = FALSE
默认情况下为 TRUE,因此第一行将作为列名
for (i in seq_along(url_list)) {
# create the 'url_path' so that we don't have to change the OP's code below
url_path <- url_list[i]
df <- openxlsx::read.xlsx(url_path, rows = c(5:7),
cols = c(5:7), na.strings = "-", colNames = FALSE)
temp <- read_pptx()
report <- add_slide(temp, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value='content', location = ph_location_label(ph_label = "Content Placeholder 2"))
report <- add_slide(report, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = df, location = ph_location_label(ph_label = "Content Placeholder 2"))
n_slides <- length(report)
for (i_slide in 2:n_slides) {
report <- report %>%
ph_with(value = format(Sys.Date()), location = loc_dt) %>%
ph_with(value = i_slide, location = loc_slidenum)
}
print(report, target=glue('report{i}.pptx'))
}
或使用iwalk
library(purrr)
iwalk(url_list, ~ {
url_path <- .x
i <- .y
df <- openxlsx::read.xlsx(url_path, rows = c(5:7),
cols = c(5:7), na.strings = "-", colNames = FALSE)
temp <- read_pptx()
report <- add_slide(temp, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value='content', location = ph_location_label(ph_label = "Content Placeholder 2"))
report <- add_slide(report, layout = "Title and Content", master = "Office Theme") %>%
ph_with(value = df, location = ph_location_label(ph_label = "Content Placeholder 2"))
n_slides <- length(report)
for (i_slide in 2:n_slides) {
report <- report %>%
ph_with(value = format(Sys.Date()), location = loc_dt) %>%
ph_with(value = i_slide, location = loc_slidenum)
}
print(report, target=glue('report{i}.pptx'))
})
-输出