简体   繁体   中英

R::officer cannot finds rows or id in powerpoint slide

R package officer cannot write to powerpoint slide. Layout_properties confirms field ctrTitle exists but slide_summary reports zero rows.

> pres<-read_pptx()
> add_slide(x=pres,layout='Title Slide', master="Office Theme")
pptx document with 1 slide(s)
Available layouts and their associated master(s) are:
             layout       master
1       Title Slide Office Theme
2 Title and Content Office Theme
3    Section Header Office Theme
4       Two Content Office Theme
5        Comparison Office Theme
6        Title Only Office Theme
7             Blank Office Theme
> ph_add_text(x=pres, type="ctrTitle", str= "This is the title")
Error: unvalid id 0 (1 slide(s))
> 

Fields exit in layout_properties.

> layout_properties(x=pres, layout="Title Slide", master="Office Theme")
    master_name        name     type id                   ph_label
9  Office Theme Title Slide       dt  4         Date Placeholder 3
17 Office Theme Title Slide      ftr  5       Footer Placeholder 4
27 Office Theme Title Slide   sldNum  6 Slide Number Placeholder 5
35 Office Theme Title Slide ctrTitle  2                    Title 1
36 Office Theme Title Slide subTitle  3                 Subtitle 2
                                            ph     offx     offy       cx
9         <p:ph type="dt" sz="half" idx="10"/> 0.500000 6.951389 2.333333
17    <p:ph type="ftr" sz="quarter" idx="11"/> 3.416667 6.951389 3.166667
27 <p:ph type="sldNum" sz="quarter" idx="12"/> 7.166667 6.951389 2.333333
35                     <p:ph type="ctrTitle"/> 0.750000 2.329861 8.500000
36             <p:ph type="subTitle" idx="1"/> 1.500000 4.250000 7.000000
          cy
9  0.3993056
17 0.3993056
27 0.3993056
35 1.6076389
36 1.9166667
> 

I tried to chain the commands; the error message is different.

>     pres<-read_pptx()
>     add_slide(x=pres,layout="Title Slide", master="Office Theme") %>%
+     ph_add_text(type='ctrTitle',str="This is the title")
Error: selection does not match any row in slide_summary. Use function slide_summary.
> 

This gave me a clue.

> slide_summary(pres, index=1)
[1] type     id       ph_label offx     offy     cx       cy       text    
<0 rows> (or 0-length row.names)
>

slide_summary reports zero rows . Can someone please help with this?

Here is my working code.

pres <- read_pptx()
pres <- add_slide(pres, layout = "Title and Content", master = "Office Theme")
pres <- ph_empty(pres, type = "body")
pres <- ph_add_par(pres, level = 1)
pres <- ph_add_text(pres, str = "这里是文字")

And I think you should consider the library magrittr. Then it can be write as follows:

library("magrittr")
pres <- read_pptx() %>%
    add_slide(layout = "Title and Content", master = "Office Theme") %>%
    ph_empty(type = "body") %>%
    ph_add_par(level = 1) %>%
    ph_add_text(str = "这里是文字")

I see you want Title Slide , then you could write like this simply:

pres <- read_pptx() %>%
    add_slide(layout = "Title Slide", master = "Office Theme") %>%
    ph_with_text(type = "ctrTitle", str = "这里是文字")

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