通过 officer 向 PowerPoint 幻灯片添加多个文本项目符号
Add multiple bullets of text to PowerPoint slide via officer
使用 R 中的 officer
包,在使用 PowerPoint 时,您可以使用函数 ph_with_text
添加文本。但是,尚不清楚如何添加多个文本项目符号或如何设置缩进级别。我想实现以下结构:
- 问题一
- 答案 1
- 答案 2
- 问题2
- 答案 1
- 答案 2
我试过两种方法,但都产生了非常错误的结果。我已经尝试使用我的文本并添加 \n
和 \n\t
来创建换行符和制表符(就像我在 PowerPoint 中创建结构一样。
doc = read_pptx()
doc = add_slide(layout = "Title and Content", master = "Office Theme")
doc = ph_with_text(doc,type = "body",
str = "Question 1\n\tAnswer 1\n\tAnswer 2\nQuestion 2\n\tAnswer 1\n\tAnswer 2",
index = 1)
这会创建项目符号,但不会创建深度。每个答案之前的每个项目符号后都有一个空格标签。此外,这些不是新的项目符号,如果我手动编辑文件并在一个项目符号点上按 Tab 键,则之后的每个点也会移动。显然没有实现正确的结构。
我也试过反复调用 ph_with_text
。
doc = add_slide(layout = "Title and Content", master = "Office Theme")
doc = ph_with_text(doc,type = "body", str = "Question 1", index = 1)
doc = ph_with_text(doc,type = "body", str = "Answer 1", index = 1)
doc = ph_with_text(doc,type = "body", str = "Answer 2", index = 1)
doc = ph_with_text(doc,type = "body", str = "Question 2", index = 1)
doc = ph_with_text(doc,type = "body", str = "Answer 1", index = 1)
doc = ph_with_text(doc,type = "body", str = "Answer 2", index = 1)
但这最终会将文本叠加在同一行上,而且一团糟无法阅读。
如何通过 officer
向幻灯片添加文本以实现多个项目符号和缩进子元素?
函数ph_with_ul
就是你需要的函数
library(magrittr)
pptx <- read_pptx()
pptx <- add_slide(x = pptx, layout = "Title and Content", master = "Office Theme")
pptx <- ph_with_text(x = pptx, type = "title", str = "Example title")
pptx <- ph_with_ul(
x = pptx, type = "body", index = 1,
str_list = c("Level1", "Level2", "Level2", "Level3", "Level3", "Level1"),
level_list = c(1, 2, 2, 3, 3, 1),
style = fp_text(color = "red", font.size = 0) )
print(pptx, target = "example2.pptx")