简体   繁体   中英

Add multiple bullets of text to PowerPoint slide via officer

Using the officer package in R, when working with a PowerPoint you can add text with the function ph_with_text . However, it's not clear how to add multiple bullets of text or how to set the level of indentation. I would like to achieve the following structure:

  • Question 1
    • Answer 1
    • Answer 2
  • Question 2
    • Answer 1
    • Answer 2

I have tried two methods which both produce very wrong results. I have tried taking my text and adding \\n and \\n\\t to create linebreaks and tabs (like how I would create the structure in 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)

This creates the bullets, but not the depth. There's a whitespace tab after each bullet before each answer. Additionally, these are not new bullets, if I manually edit the file and press tab on one bullet point, every point after is also shifted. Clearly the correct structure hasn't been acheived.

I have also tried just calling ph_with_text repeatedly.

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)

But this ends up overlaying the text on the same line and it's an unreadable mess.

How do I, via officer , add text to a slide achieving multiple bullets and indented sub-elements?

The function ph_with_ul is the function you need

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM