abi*_*son 5 powerpoint r officer
我是Reporters and Officer软件包的狂热用户,目前正在尝试转换为使用Powerpoint工作流的Officer。我使用的幻灯片模板在母版中包含幻灯片编号占位符。
使用Reporters时,我可以使用添加幻灯片编号,doc <-addPageNumber( doc )并且页码反映每张幻灯片在卡座中的当前位置。我正在寻找Officer中的相同功能,并在移动幻灯片时寻找适当更新的幻灯片编号。
使用时ph_with_text(doc, type = "sldNum", str = "slide 1"),需要提供带有静态数字或文本的字符串,并且不会根据幻灯片在卡座中的显示位置进行更新。例如,如果我知道幻灯片将是幻灯片2,则可以输入str = "2",但是即使将幻灯片移动到演示文稿中的幻灯片3位置,幻灯片编号也将显示为2。
我尝试使用str = ""或来将字符串留空,ph_empty(type= "sldNum")但这些导致在幻灯片上显示字符串“幻灯片编号”。
任何帮助或朝着正确方向的指示,将不胜感激!
完成演示后,我已使用带有 for 循环的官员 0.3.4 成功添加了页码。
library(officer)
library(magrittr)
my_pres <- read_pptx() %>%
add_slide('Title Only', 'Office Theme') %>%
ph_with(value = 'Slide 2 Title', location = ph_location_type(type = "title")) %>%
add_slide('Title Only', 'Office Theme') %>%
ph_with(value = 'Slide 3 Title', location = ph_location_type(type = 'title'))
# add slide numbers starting on slide 2
n_slides <- length(my_pres)
for (i_slide in 2:n_slides) {
my_pres <- my_pres %>%
on_slide(index = i_slide) %>%
ph_with(value = i_slide, location = ph_location_type('sldNum'))
}
Run Code Online (Sandbox Code Playgroud)
小智 0
在与官员遇到类似的问题并查看源代码后,我提出了以下解决方案
ph_with_text_fld(doc, type = "sldNum", str = "2")
Run Code Online (Sandbox Code Playgroud)
该函数的代码如下:
library(htmltools)
library(xml2)
ph_with_text_fld <- function( x, str, type = "title", index = 1 ){
stopifnot( type %in% c("ctrTitle", "subTitle", "dt", "ftr", "sldNum", "title", "body") )
slide <- x$slide$get_slide(x$cursor)
sh_pr_df <- slide$get_xfrm(type = type, index = index)
sh_pr_df$str <- str
xml_elt <- do.call(pml_shape_str_fld, sh_pr_df)
node <- as_xml_document(xml_elt)
xml_add_child(xml_find_first(slide$get(), "//p:spTree"), node)
slide$fortify_id()
x
}
pml_shape_str_fld <- function(str, ph, offx, offy, cx, cy, ...) {
sp_pr <- sprintf("<p:spPr><a:xfrm><a:off x=\"%.0f\" y=\"%.0f\"/><a:ext cx=\"%.0f\" cy=\"%.0f\"/></a:xfrm></p:spPr>", offx, offy, cx, cy)
# sp_pr <- "<p:spPr/>"
nv_sp_pr <- "<p:nvSpPr><p:cNvPr id=\"\" name=\"\"/><p:cNvSpPr><a:spLocks noGrp=\"1\"/></p:cNvSpPr><p:nvPr>%s</p:nvPr></p:nvSpPr>"
nv_sp_pr <- sprintf( nv_sp_pr, ifelse(!is.na(ph), ph, "") )
paste0( pml_with_ns("p:sp"),
nv_sp_pr, sp_pr,
"<p:txBody><a:bodyPr/><a:lstStyle/><a:p><a:fld id=\"{GUID FROM THE MASTER TEMPLATE}\" type=\"slidenum\"><a:rPr/><a:t>",
htmlEscape(str),
"</a:t></a:fld></a:p></p:txBody></p:sp>"
)
}
pml_with_ns <- function(x){
base_ns <- "xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\""
sprintf("<%s %s>", x, base_ns)
}
Run Code Online (Sandbox Code Playgroud)
重要的部分是
<a:fld id=\"{GUID FROM THE MASTER TEMPLATE}\" type=\"slidenum\">
Run Code Online (Sandbox Code Playgroud)
其中主模板中的 GUID需要替换为该字段的幻灯片母版布局中的 GUID
| 归档时间: |
|
| 查看次数: |
454 次 |
| 最近记录: |