简体   繁体   中英

How to use index argument in ph_with_text function from officer package

Ciao,

I'm working on a custom pptx presentation using officer package on R. The truth is that I am porting the code form Reporters .

My question is about the structure of a layout. The starting point is an empty pptx I upload in order to import themes and formatting settings; In particular I do not use

ppt <- read_pptx()

to create the pptx but rather I import it from an existing file:

ppt <- read_pptx("Blank_officer.pptx")

This is how it is the standard slide layout 在此处输入图片说明

The problem is that I dont't know how to fill fields at this point. This is what I want to reach:

在此处输入图片说明

I tried using the following code:

ppt <- read_pptx( ROOT_PATH( "template/Blank_officer.pptx" ) )
ppt <-  ppt <- add_slide( ppt, "Internal Slide - 1 Column Text", master = "Blank" )

ppt <- ph_with_text(x = ppt, str = "A Title", type = "title")
ppt <- ph_with_text(ppt, "Bullet text 4", type = "body", index = 4)
ppt <- ph_with_text(ppt, "Bullet text 11", type = "body", index = 11)
ppt <- ph_with_text(ppt, "\nBullet text 11.2", type = "body", index = 11)
ppt <- ph_with_text(ppt, 2, type = "sldNum")

print( ppt, target = "officerTest.pptx" )

and I get the following result: 在此处输入图片说明

It seems fine but the truth is that I am choosing index argument by a "try-and-fail" approach, ie by random choosing the value and see what happens.

Can you explain me the how the index argument works?

Is there a command that shows me, for a given slide layout, which are the index I can use to write in text box?

In alternative:

I was wondering if there exists a "Tab"-like function, ie a funtion that let me navigate in text box like addParagraph function in Reporters package.

Thanks for your help,

Ciao

At the end I get it. The truth is that the problem is not trivial since it happens that index value refer to different part of the ppt, ie they change in time!!!

This of course means that we can not use integer values but we have to use a different 1:1 mapping.

To do that we have to know the name of the different boxes which are in the slide layout. Namely:

library(officer)
ppt <- read_pptx()
layout_properties( ppt ) 

This dataframe contains the information about the slide layout structure. In particular the slide name ( name column ), the component type ( type column ) and the paragraph label ( ph_label column ).

The idea is to use this dataframe to build a dynamic map which, for every slide, type and ph_label will return the correct index.

To do that I wrote the following functions: The pptIndexInfo function filters the layout dataframe by layout name and assign to the objects a progressive index (indipendent from id ). Infact ( I have no idea why ) indexes we will use are those we have just generated and not id column values.

pptIndexInfo <- function( ppt, layout, master ){
  aux = layout_properties(ppt, master = master, layout = layout)
  aux$index = NA
  for(type in unique(aux$type) ){
    aux[ aux$type == type, "index" ] = 1:length(aux[ aux$type == type, "index" ])
  }
  return(aux)
}

At this point we just need to extract index for the desired ph_label .

Title_index <- function(ppt, layout, master){
  pptInd = pptIndexInfo(ppt, layout, master)
  return( pptInd$index[ which(pptInd$ph_label == "Title") ] )
}

This function for example generate the filtered layout dataframe and extract the assigned index to ph_label "Title".

These functions are usefull when you have multiple boxes for the same "type".

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