Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R officer package: Add slide numbers that reflect current slide position

I'm an avid user of the Reporters and Officer packages and currently trying to transition to Officer for a Powerpoint workflow. I'm using a slide template that includes slide number placeholders in the master.

When using Reporters, I am able to add the slide numbers using doc <-addPageNumber( doc ) and the page numbers reflect the current position each slide has in the deck. I am looking for the same functionality in Officer, and looking for the slide numbers to update appropriately when I move the slides.

When I use ph_with_text(doc, type = "sldNum", str = "slide 1"), I am required to supply a string with a static number or text, and it does not update according to where the slide appears in the deck. For example, if I know my slide will be slide 2, I can enter str = "2", but then the slide number will read as 2 even if I move that slide to the slide 3 position in the presentation.

I tried leaving the string empty with str = "" or with ph_empty(type= "sldNum") but these result in the string "Slide Number" appearing on the slide.

Any help or pointers in the right direction would be appreciated!

like image 498
abidawson Avatar asked May 22 '18 18:05

abidawson


People also ask

What displays the current slide number?

Answer: Status bar is located at the bottom of the PowerPoint window, it shows messages and information about the view, such as the slide number and the current theme template used.

How will you add the slide number at the bottom of each side?

Answer: On the Insert tab, in the Text group, click Slide Number. In the Header and Footer dialog box, click the Slide tab. Do one of the following: To number the slide that you have selected, select the Slide number check box, and then click Apply.

How do I add slide numbers to all slides in PowerPoint?

To add slide numbers to your PowerPoint slides, click the “Slide Number” checkbox in the “Slides” tab. Press the “Apply to All” button to add slide numbers to all of your slides. Once applied, your slide numbers will appear on each of your slides in the bottom-right corner.


1 Answers

I've successfully added page numbers using officer 0.3.4 with a for loop after I'm done with the presentation.

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'))
}
like image 156
Alejandro Sotolongo Avatar answered Nov 07 '22 02:11

Alejandro Sotolongo