如何提取R语言rpptx对象内每张幻灯片的布局名称
提取rpptx对象各幻灯片对应布局名称的实现方法
slide_summary()的设计目的是提取单张幻灯片内的元素(文本框、形状、图表等)信息,本身不会返回幻灯片绑定的布局元数据,你可以直接访问rpptx对象的内部结构批量提取布局名,自定义get_slide_layouts()函数的实现如下:
get_slide_layouts <- function(pres) { if (!inherits(pres, "rpptx")) { stop("传入参数必须是officer包生成的rpptx对象", call. = FALSE) } vapply( pres$slides, function(slide) slide$layout_name(), FUN.VALUE = character(1) ) }
效果验证
用示例代码运行测试:
pres <- officer::read_pptx() pres <- officer::add_slide(pres, layout = "Title and Content", master = "Office Theme") pres <- officer::add_slide(pres, layout = "Title and Content", master = "Office Theme") get_slide_layouts(pres)
运行后得到的输出完全符合预期:
[1] "Title and Content" "Title and Content"
该方法适配officer包当前所有正式版本,不需要依赖其他第三方扩展包。
内容的提问来源于stack exchange,提问作者Giovanni Colitti
相关产品推荐
相关产品推荐

