davidgohel / officer Goto Github PK
View Code? Open in Web Editor NEW:cop: officer: office documents from R
Home Page: https://ardata-fr.github.io/officeverse/
License: Other
:cop: officer: office documents from R
Home Page: https://ardata-fr.github.io/officeverse/
License: Other
I get an error when trying to add text to a slide after after adding a flextable. My understanding is that the code below should work, but instead it raises a "subscript out of bounds" error.
library(tidyverse)
library(flextable)
library(officer)
ft <- flextable(data.frame(a=1:2, b=3:4))
ppt <- (read_pptx()
%>% add_slide(layout="Title and Content", master="Office Theme")
%>% ph_with_flextable_at(ft, left=1, top=1)
%>% ph_with_text(type="body", str="foo")
%>% ph_add_par(level=2)
%>% ph_add_text(str="bar"))
Thanks!
Hi David, I have 9 slides on a powerpoint and when I add the 10th, it doesn't add anything and no error message appears either.
Is there any way to search (and point via cursor, eventually edit) through table cells? The keyword argument to cursor_reach seems to go through the docx's body only.
Would you like me to move the powerpoint color extraction here? I don't see in currently in officer
It doesn't require java so it is inline with this package.
When reading in a .pptx with template layouts, if the slide layout has an image, then PowerPoint will print out that there is unreadable content. This is replicated with my employers's PowerPoint template, which I can't use here, and a bunch of other templates I found online.
As an example:
Download the Wharton Powerpoint template and save in working directory as template.pptx
. Sorry for the manual work, for whatever reason download.file
does not work well with .pptx
for me.
library(officer)
library(magrittr)
officer_example <- read_pptx("template.pptx") %>%
add_slide(layout = "Deck Cover", master = "knowledge_master1-3_theme") %>%
ph_with_text(type = "title", str = "A title")
print(officer_example, target = "officer.pptx") %>% invisible()
When I open officer.pptx
I get the error message PowerPoint removed unreadable content in officer.pptx. You should review this presentation to determine whether any content was unexpectedly changed or removed
. On the new slide, slide 19, the image still appears but there is a box over it which says This image cannot currently be displayed.
In other examples, the image won't even appear. For example, save the Kellogg PowerPoint template as Kellogg_Template_4x3.pptx
in the working directory. Then run:
kellogg_example <- read_pptx("Kellogg_Template_4x3.pptx") %>%
add_slide(layout = "Title Slide", master = "Custom Design")
print(kellogg_example, target = "kellogg_officer.pptx") %>% invisible()
Just to add, I get mixed results with doing the equivalent in ReporteRs
. With my corporate theme, it actually works perfectly but in other examples there is technically no error but it loses the theme.
Hi David,
I've come across the following error when trying to write a flextable that contains a "<" in a cell to a document using officer:
Error in doc_parse_raw(x, encoding = encoding, base_url = base_url, as_html = as_html, :
StartTag: invalid element name [68]
Here's a minimal example to reproduce the error:
library(flextable)
library(officer)
library(tidyverse)
irishead = head(iris) %>% mutate(Sepal.Length_5 = ifelse(Sepal.Length<5,"Sepal.Length < 5","Sepal.Length \u2265 5"))
ft = flextable(irishead)
ft
doc = read_docx() %>%
body_add_flextable(ft)
irishead_unicode = irishead %>% mutate(Sepal.Length_5 = gsub("<","\u003c",Sepal.Length_5))
ft2 = flextable(irishead)
ft2
doc = read_docx() %>%
body_add_flextable(ft2)
Note that this error occurs even if I try using the unicode value of \u003c.
Also, here's the results from a call to sessionInfo():
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.6
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] bindrcpp_0.2 dplyr_0.7.2 purrr_0.2.3 readr_1.1.1 tidyr_0.7.0 tibble_1.3.4 ggplot2_2.2.1 tidyverse_1.1.1
[9] flextable_0.2.0 officer_0.1.6
loaded via a namespace (and not attached):
[1] zip_1.0.0 Rcpp_0.12.12 cellranger_1.1.0 plyr_1.8.4 bindr_0.1 forcats_0.2.0 R.methodsS3_1.7.1
[8] R.utils_2.5.0 base64enc_0.1-3 tools_3.3.2 digest_0.6.12 uuid_0.1-2 lubridate_1.6.0 jsonlite_1.5
[15] gtable_0.2.0 nlme_3.1-131 lattice_0.20-35 pkgconfig_2.0.1 rlang_0.1.2 psych_1.7.5 yaml_2.1.14
[22] parallel_3.3.2 haven_1.1.0 httr_1.3.1 stringr_1.2.0 xml2_1.1.1 hms_0.3 gdtools_0.1.6.0002
[29] htmlwidgets_0.9 tidyselect_0.2.0 grid_3.3.2 glue_1.1.1 R6_2.2.2 readxl_1.0.0 foreign_0.8-69
[36] modelr_0.1.1 reshape2_1.4.2 magrittr_1.5 scales_0.5.0 htmltools_0.3.6 rvest_0.3.2 assertthat_0.2.0
[43] mnormt_1.5-5 colorspace_1.3-2 stringi_1.1.5 lazyeval_0.2.0 munsell_0.4.3 broom_0.4.2 R.oo_1.21.0
To start off with: David this package is amazing!
This turned into an information share as I found the information I needed. I was having trouble with content placement, i.e. what is the type of certain placeholder in a powerpoint template (body, ftr, title etc.). Also it was hard to find the index or id of a placeholder and so on.
Solution: slide_summary(pptvariable, index = slideindexno) function in the package shows all this information for placeholders that have content!
Edit: The placeholder id is determined by the "Send forward" "Send backward" commands in powerpoint. This is best viewed by opening the selection pane found at HOME > Select > Selection Pane
The pptx is nothing more than a blank pptx file opened & saved.
library(officer)
library(magrittr)
# Load PPTX file:
pptx_file = read_pptx(path = 'officer-test-blank.pptx')
pptx_file = pptx_file %>% on_slide(index = 1) %>%
ph_with_text(type = "ctrTitle", str = "This is a title") %>%
ph_with_text(type = "subTitle", str = "This is a subtitle")
pptx_file = pptx_file %>% add_slide(layout="Title and Content", master="Office Theme") %>%
ph_with_text(type = "body", str = "Test 1", index = 1) %>%
ph_with_text(type = "body", str = "Test 2", index = 2) %>%
ph_with_text(type = "title", str = "This is a title")
print(pptx_file, target = 'officer-test-out.pptx') %>%
invisible()
This is all run on a Mac, and the result "needs repaired" and then lacks 'Test 2' bullet point.
officer-test-out.pptx
officer-test-blank.pptx
Not sure where to write this (so I apologize if this is not the right forum)... is it possible to modify the font and size of text in ph_with_table?
In ph_with_text, I can modify it using the function
style=fp_text()
but this does not work for ph_with_table and the table I just inserted has text that is quite large.
Hello again!
The code:
test <- read_pptx()
test <- add_slide(test, "Title and Content", "Office Theme")
slide_summary(test, 1)
shows that a new slide is created, but it has no placeholders. It is just an empty blank slide. There should be several placeholders, title, body etc.
Writing the name of the layout wrong results in an error, so it clearly is trying to add a certain layout
Is there something I am missing here?
BR, Juuso
Hi,
I need to add flextable inside word document in landscape mode as my table has many columns. I seen this function body_end_section and tried to use it for above purpose but its not working. Can you help me with this please? I tried below code
read_docx() %>%
body_add_par(value = "Hi", style = "Normal") %>%
body_end_section(landscape = TRUE) %>%
body_add_flextable(flexTableObj) %>%
body_end_section(landscape = TRUE )
Thanks
Hi there,
I am using the development version (0.1.1.9001).
after using cursor_forward(), I am unable to print the rdocx object as a word file.
I also tried other cursor-related functions, the same error was reported.
Here is my code
`
library(officer)
setwd("~/Desktop")
doc = read_docx("~/Desktop/empty.docx")
doc = cursor_reach(doc, keyword = "Project")
doc = cursor_forward(doc)
doc = body_add_toc(doc, pos = "on")
print(doc, target = "~/Desktop/test.docx")
Error in zip_internal(zipfile, files, recurse, compression_level, append = FALSE) :
Can't open zip file`
Thank you.
Referring to your documentation at: https://davidgohel.github.io/officer/articles/word.html#cursor-manipulation
I received an error message when using:
cursor_reach(keyword = "Hierdatum")%>%
slip_in_text(datum, pos = "on", style = "Default Paragraph Font")%>%
Error in slip_in_xml(x = x, str = xml_elt, pos = pos) :
unknown pos 'on', it should be 'after' or 'before'
Hallo ,
I used the Package ReporteRs and it's great and very helpfull but my Problem was that this Package need rJava .
Now I try with Officer to add a ggplot graphic into a Powerpoint Presentation . but it seems that there ist not a Specific function for this .
for example : the functionbody_add_gg()
is availble just for .doc files .
is it available in officer to add a ggplot graphic "as vecotr" to a pptx file ?
like :
addPlot(myppt,width,height,vector.graphic = TRUE)
-- from ReporteRs --
Hi, I would very much like to test this. Is there any documentation available?
I'm working on producing tables in Word from R. A big problem we have is tracing statistical report content that is then copy and pasted into other Word documents. I've got a method to generate unique keys for statistical content, and trace it with comments. Then later if data is changed, we can automatically substitute the content back in the final document.
My ideal Word generation from R needs are
Looks like you've hit the first two for me. The 3rd is really the big need. The last 2 I can live without and work toward later.
TL;DR will the library eventually support commenting?
Hello,
I am reading in a ppt presentation created using ReporteRs, and am trying to add internal links within the presentation. I have noticed that the slide index is not always equal to the slide number, is this expected?
doc <- on_slide(doc, index=5)
slide_summary(doc)
Source: local data frame [5 x 7]
type id offx offy cx cy text
(chr) (chr) (int) (int) (int) (int) (chr)
title 1 NA NA NA NA blahblah
body 2 1371600 1371600 2743200 2743200
body 3 5029200 1371600 2743200 2743200
sldNum 2 NA NA NA NA 13
ftr 3 NA NA NA NA 2017-05-08number
Hi, David.
I'm trying to make tibble output to .docx file using officer and got encoding error:
Error in doc_parse_raw(x, encoding = encoding, base_url = base_url, as_html = as_html, :
Input is not proper UTF-8, indicate encoding !
Bytes: 0xC2 0xF0 0xE5 0xEC [9]
Code to recreate error below:
library(tidyverse)
library(magrittr)
library(officer)
library(stringi)
# проверяем запись таблицы с русскими именами под win
out_df <- tibble(time=c(1, 2), value=c("ns", ":dm"))
bad_df <- tibble("Время"=c(1, 2), value=c("ns", ":dm"))
# nm <- names(bad_df) # doesn't not help
# stri_enc_mark(nm) # doesn't not help
# names(bad_df) <- nm # doesn't not help
doc <- read_docx() %>%
body_add_par(value='Первые 80 строк данных', style="heading 1") %>%
body_add_table(value=out_df, style="table_template") %>%
body_add_table(value=bad_df, style="table_template") %>%
print(target = "test1.docx")
sessionInfo() details below
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Russian_Russia.1251 LC_CTYPE=Russian_Russia.1251 LC_MONETARY=Russian_Russia.1251
[4] LC_NUMERIC=C LC_TIME=Russian_Russia.1251
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] stringi_1.1.5 officer_0.1.4 magrittr_1.5 dplyr_0.7.2 purrr_0.2.2.2
[6] readr_1.1.1.9000 tidyr_0.6.3 tibble_1.3.3 ggplot2_2.2.1.9000 tidyverse_1.1.1
loaded via a namespace (and not attached):
[1] zip_1.0.0 Rcpp_0.12.12 cellranger_1.1.0 compiler_3.4.1 plyr_1.8.4 bindr_0.1
[7] base64enc_0.1-3 R.methodsS3_1.7.1 R.utils_2.5.0 forcats_0.2.0 tools_3.4.1 uuid_0.1-2
[13] digest_0.6.12 lubridate_1.6.0 jsonlite_1.5 nlme_3.1-131 gtable_0.2.0 lattice_0.20-35
[19] pkgconfig_2.0.1 rlang_0.1.1 psych_1.7.5 yaml_2.1.14 parallel_3.4.1 haven_1.1.0
[25] bindrcpp_0.2 xml2_1.1.1 stringr_1.2.0 httr_1.2.1 gdtools_0.1.4 hms_0.3
[31] grid_3.4.1 glue_1.1.1 R6_2.2.2 readxl_1.0.0 foreign_0.8-69 modelr_0.1.0
[37] reshape2_1.4.2 scales_0.4.1 rvest_0.3.2 assertthat_0.2.0 mnormt_1.5-5 colorspace_1.3-2
[43] lazyeval_0.2.0 munsell_0.4.3 broom_0.4.2 R.oo_1.21.0
Hi David,
I am very interested by your new package. I use until now ReporteRs but Java dependency is one of my (little) problem with it.
So I am starting to look at officer.
If I think there is a problem with the way that graphics are resize in the document.
In this example using body_add_gg
the two plots have the same height, but content, especially fonts, are deformed.
library(ggplot2)
doc <- read_docx()
gg_plot <- ggplot(data = iris ) +
geom_point(mapping = aes(Sepal.Length, Petal.Length))
doc <- body_add_gg(doc, value = gg_plot, style = "centered", height = 3)
doc <- body_add_gg(doc, value = gg_plot, style = "centered", height = 6)
print(doc, target = "body_add_gg.docx" )
In your definition of the function you forget to change default values as variables:
body_add_gg <- function( x, value, width = 6, height = 5, style = NULL, ... ){
stopifnot(inherits(value, "gg") )
file <- tempfile(fileext = ".png")
png(filename = file, width = width, height = height, units = "in", res = 300, ...)
print(value)
dev.off()
on.exit(unlink(file))
body_add_img(x, src = file, style = style, width = 6, height = 5)
}
Regards,
Gilles
Hi David,
I am facing an issue related to special characters in flextable. Actually, my dataframe has special characters "<" and ">", when I try to create flextable from it, it is successfully created. But, when I run below code, it throws an error:
doc <- read_docx() %>%
body_add_flextable(flexTableObj) %>%
body_end_section(landscape = TRUE )
print(doc, target = filePath)
Error in doc_parse_raw(x, encoding = encoding, base_url = base_url, as_html = as_html, :
StartTag: invalid element name [68]
I tried to change encoding of my dataframe column as well, but it did not work.
I seen some threads from ReporteRs package specifying same problem, for example: #davidgohel/ReporteRs#63 but it was solved there. Not sure whether its been resolved for the officer package or not.
Thanks
It seems that a paragraph added with body_add_par() or body_add_fpar() has a white background, which effectively covers any background image in the page. A transparent default background would be more useful.
Hi, thanks for the package, definitely the best way to write PPT!
But is there any way to add one object right below another object? For instance, suppose I want to put a table and some related bullet points on the same slide. I can pre-set placeholders for the table and bullet points, but if the table is longer than expected, the table and bullet point may overlap. If the table is shorter than expected, there might be a lot of whitespace in-between.
In ReporteRs you could specify the x/y coords. This seems helpful, because if I knew the height of the table I could specify that the bullets be right below it. But how can you get the height of the table? I guess you can count the number of rows and multiply by a reasonable number, but the actual size seems to depend on how much word-wrapping occurs (because if a cell wraps around, the cell's row will get taller).
BTW, is officer+flextable recommended over ReporteRs going forward? Is ReporteRs basically deprecated? Thanks!
it would be convenient if with this function the bookmark content could differ from the bookmark name
body_bookmark(x, id, value)
doc <- read_docx() %>%
body_add_par("centered text", style = "centered") %>%
body_bookmark(id = "ref1", value = "text_to_replace")
In word bookmarks can define not only a position in the document but also #a (text) range as well.
Great job BTW!
Hi David,
I noticed when you try to write on an opened pptx and it crashes, as it should, that the working directory is not set back to its initial state.
P.S.: this is not a big thing, but I will let you know the few errors i might encounter as I believe it can help you for your development. Thanks for your work btw :)
Regards,
Maxime
This particular issue is about the cursor_bookmark
function, but it may also apply to one or more of the other cursor_*
functions.
I've a .docx file set up to serve as a template with all of the desired design and style details established, and I'm trying to modify some text that will serve as the document title. I have a bookmark set for that location, called "Document_Title", but when I attempt
doc <- read_docx(path = "template.docx") %>%
cursor_bookmark(id = "Document_Title") %>%
body_add_par(value = "New text", style = "Title", pos = "on")
I receive the following error: Error: bookmark "Document_Title" does not end in the same paragraph (or is on the whole paragraph)
.
After tracking down where that comes from (docx_document.R) and locally playing around with the code for cursor_bookmark
(lines 68--90), I found that things seem to work correctly if each of the single forward-slashes (/
) in the xpath_
assignments are replaced with double forward-slashes (//
).
I am not sure if I am barking up the right tree here, but my script works flawlessly in Rstudio, but I keep running into error 500 under Rapache.
Can you see if I am doing something wrong, or if I ran into a bug?
code that is executed:
print(doc, target = "output/report.docx")
Attributes of the output directory: 777
Error in the error log:
[Tue May 30 14:52:09.976480 2017] [:error] [pid 8657] [client 213.127.237.217:55812] rApache Notice!
Loading required package: officer
Loading required package: magrittr
Loading required package: xlsx
Loading required package: rJava
Loading required package: xlsxjars
Loading required package: limer
Loading required package: ggplot2
Loading required package: mailR
Loading required package: flextable
Saving 178 x 178 mm image
Error in value[3L] :
Could not write '/home/admin/domains/reporting.toolsforresearch.com/public_html/project/gw/output/demo-Werkgelukmeter.docx' [Can't create zip file entry]
Traceback:
11: stop("Could not write ", shQuote(target), " [", e$message, "]")
10: value[3L]
9: tryCatchOne(expr, names, parentenv, handlers[[1L]])
8: tryCatchList(expr, classes, parentenv, handlers)
7: tryCatch(zip(zipfile = target, files = list.files(all.files = TRUE,
recursive = TRUE)), error = function(e) {
stop("Could not write ", shQuote(target), " [", e$message,
"]")
}, finally = {
setwd(curr_wd)
})
6: pack_folder(folder = x$doc_obj$package_dirname(), target = target)
5: print.rdocx(doc, target = report_file_name)
4: print(doc, target = report_file_name)
3: eval(expr, envir, enclos)
2: eval(exprs[i], envir)
1: sys.source(file = "/home/admin/domains/reporting.toolsforresearch.com/private_html/project/gw/wgofficer.R",
envir = .rAenv)
Simple example:
library(officer)
my_doc <- read_docx()
body_add_par(my_doc, value="This value is <3")
Error in doc_parse_raw(x, encoding = encoding, base_url = base_url, as_html = as_html, :
StartTag: invalid element name [68]
Looks like body_add_xml does not escape value before creating the xml node. Was able to use htmltools::htmlEscape to escape before passing to body_add_xml. Not sure if you have a similar function in your package or in xml2.
Hi,
I want to export the flextable to PDF File, Do you have any idea how it can be done? I tried different ways but no success yet.
Thanks in advance
I encounter a strange behaviour when the number of slides in an officer generated PowerPoint document exceeds 9
As long as the number of slides is <=9 referring to slides (i.e. with slide_summary()) works as expected. Once slides number exceed 9, slide contents become incorrect. See example.
Example:
ppt= read_pptx()
for(i in 1:11){
ppt= ppt %>% add_slide("Title Slide", "Office Theme") %>% ph_with_text(i,"ctrTitle")
cat("Total number of slides:", i, "\n")
for(j in 1: pmin(i, 11)){
cat("Slide: ", j, "\n")
print(slide_summary(ppt, j))
}
cat("--------------------\n")
}
Output:
Total number of slides: 1
Slide: 1
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 1
--------------------
Total number of slides: 2
Slide: 1
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 1
Slide: 2
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 2
--------------------
Total number of slides: 3
Slide: 1
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 1
Slide: 2
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 2
Slide: 3
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 3
--------------------
Total number of slides: 4
Slide: 1
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 1
Slide: 2
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 2
Slide: 3
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 3
Slide: 4
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 4
--------------------
Total number of slides: 5
Slide: 1
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 1
Slide: 2
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 2
Slide: 3
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 3
Slide: 4
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 4
Slide: 5
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 5
--------------------
Total number of slides: 6
Slide: 1
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 1
Slide: 2
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 2
Slide: 3
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 3
Slide: 4
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 4
Slide: 5
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 5
Slide: 6
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 6
--------------------
Total number of slides: 7
Slide: 1
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 1
Slide: 2
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 2
Slide: 3
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 3
Slide: 4
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 4
Slide: 5
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 5
Slide: 6
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 6
Slide: 7
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 7
--------------------
Total number of slides: 8
Slide: 1
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 1
Slide: 2
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 2
Slide: 3
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 3
Slide: 4
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 4
Slide: 5
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 5
Slide: 6
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 6
Slide: 7
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 7
Slide: 8
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 8
--------------------
Total number of slides: 9
Slide: 1
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 1
Slide: 2
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 2
Slide: 3
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 3
Slide: 4
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 4
Slide: 5
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 5
Slide: 6
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 6
Slide: 7
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 7
Slide: 8
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 8
Slide: 9
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 9
--------------------
Total number of slides: 10
Slide: 1
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 1
Slide: 2
# A tibble: 0 × 7
# ... with 7 variables: type <chr>, id <chr>, offx <int>, offy <int>, cx <int>, cy <int>, text <chr>
Slide: 3
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 2
Slide: 4
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 3
Slide: 5
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 4
Slide: 6
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 5
Slide: 7
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 6
Slide: 8
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 7
Slide: 9
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 8
Slide: 10
# A tibble: 2 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 9
2 ctrTitle 3 NA NA NA NA 10
--------------------
Total number of slides: 11
Slide: 1
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 1
Slide: 2
# A tibble: 0 × 7
# ... with 7 variables: type <chr>, id <chr>, offx <int>, offy <int>, cx <int>, cy <int>, text <chr>
Slide: 3
# A tibble: 0 × 7
# ... with 7 variables: type <chr>, id <chr>, offx <int>, offy <int>, cx <int>, cy <int>, text <chr>
Slide: 4
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 2
Slide: 5
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 3
Slide: 6
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 4
Slide: 7
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 5
Slide: 8
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 6
Slide: 9
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 7
Slide: 10
# A tibble: 1 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 8
Slide: 11
# A tibble: 3 × 7
type id offx offy cx cy text
<chr> <chr> <int> <int> <int> <int> <chr>
1 ctrTitle 2 NA NA NA NA 9
2 ctrTitle 3 NA NA NA NA 10
3 ctrTitle 4 NA NA NA NA 11
--------------------
Hi David,
I try to define some center text using body_add_fpar
, fpar
and fp_par
without success.
Perhaps I do something wrong ?
doc <- officer::read_docx()
doc <- doc %>% officer::body_add_fpar(
officer::fpar(officer::ftext("I want a centered title", prop = officer::fp_text(font.size = 28)),
fp_p = officer::fp_par(text.align = 'center')))
print(doc, target = "title.docx")
hi,i have a error like this,i don't know why
> my_pres <- read_pptx("f:/R_packages_test/test.pptx")
>my_pres <- my_pres %>%
+ add_slide(layout = "Title and Content", master = "Office Theme")
# Error in if (grepl("<|>", x)) { : argument is of length zero
I changed the source code of you example slightly in order for me to understand where the cursor "is", when I try to position it.
library(officer)
library(magrittr)
doc <- read_docx() %>%
body_add_par("paragraph 1", style = "Normal") %>%
body_add_par("paragraph 2", style = "Normal") %>%
body_add_par("paragraph 3", style = "Normal") %>%
body_add_par("paragraph 4", style = "Normal") %>%
body_add_par("paragraph 5", style = "Normal") %>%
body_add_par("paragraph 6", style = "Normal") %>%
body_add_par("paragraph 7", style = "Normal") %>%
# default template contains only an empty paragraph
# Using cursor_begin and body_remove, we can delete it
cursor_begin() %>% body_remove() %>%
body_add_par("new 1", style = "Normal") %>%
cursor_forward() %>%
body_add_par("new 2", style = "Normal") %>%
cursor_forward() %>%
body_add_par("new 3", style = "Normal") %>%
cursor_forward() %>%
print(doc, target = "cursor.docx")
I expected the placement of "new 1" to be before "paragraph 1", because that is the position where body_remove() happens. Instead I found it after paragraph 2.
This boggles me. How should I understand this?
Here is the resulting cursor.docx
dear david:
I have a word file,and it has some paragraphs. Now how to get the text of a paragraph and edit it? Like edit <paragraphs.text>(in python ),thanks!
I'm trying to replace content which is inside a shape. The first occurrence of body_add_par()
deletes all the content from the document (and any further cursor_reach()
attempts will fail).
replacetest.docx contains two paragraphs inside a shape: "Title_1" and "Title_2".
Test 1, fails at the second cursor_reach()
with error Title_2 has not been found in the document
:
my_doc <- read_docx(path="replacetest.docx") %>%
cursor_reach(keyword="Title_1") %>%
body_add_par(value="Lore ipsum", pos="on") %>%
cursor_reach(keyword="Title_2") %>%
body_add_par(value="dolor sit amet", pos="on")
print(my_doc, target= "replacetest_result.docx")
Test 2, produces document with only one paragraph:
my_doc <- read_docx(path="replacetest.docx") %>%
cursor_reach(keyword="Title_1") %>%
body_add_par(value="Lore ipsum", pos="on")
print(my_doc, target= "replacetest_result.docx")
ReportRs supported bookmarks in docx files to replace content. The nearest functionality in officer
appears to be cursor_reach
, but that seems less precise and more dangerous than a bookmark.
Are there plans to add support for bookmarks. Something like a cursor_jump_to_bookmark()
function and/or a family of body_replace***(..., bookmark)
functions would be very nice.
Hi,
I heavely use ReporteRs in my institution. Does officer will replace ReporteRs in the near future?
Do we need to adapt our workflow to this new package soon?
Thank you very much for you incredible work.
Best,
David
Dear Mr. David Gohel,
I'm a newbie in R and right in the beginning I have started working with ReporteRs and Officer. I have noticed after a while that Oficcer's pptx-slides are sorted alphabettically while ReportRs' not.
For example: To adress the 10th. slide of a 23-pages pptx file, I have to write index = 2 instead of index = 10.
my_pres <- read_pptx(path = "*.pptx")
my_pres <- my_pres %>%
on_slide(index = 2)
I have managed to find the Officer-index for each slide of this 23-pages PowerPoint File, but when there's more pages, it would be a little bit time consuming. I hope that this issue would be fixed somewhen.
Update: Thank you very much! I updated the package and the problem is solved. Next time I'll ask a better question in the right way.
Hello,
I started using the officer package recently and it works great.
I would like to generate PowerPoint documents with "native" charts, so a user can right-click on a chart and has options to edit data in Excel, change the chart type, etc. Is it possible to do that using the officer package (or some other R package that you know about)? If not, do you plan to implement such a feature?
Thanks!
Hi David,
Hope you are doing well. I have a requirement to add footer to the generated Word document. I searched the documentation, but could not find any function for that.
Secondly, I also want to add page numbers to the document.
Do you think above 2 tasks can be achieved?
Thanks in advance
Hi there,
I am really enjoying using the functions you created. I have a question though, when I try to use the "bold = FALSE" parameter in fp_text, it ignores it and still prints my text in bold. Is this a bug or am I not using it correctly?
my_pres <- ph_add_text(my_pres,str = "Title - text",style=fp_text(font.size = 16, bold = FALSE,font.family="Calibri", vertical.align = 'baseline'))
Hi,
I was trying to use keyword to match some Chinese characters but failed.
I noticed that the text search is made via xpath 1.0, but I have no idea about it and am still working on it.
how can I match those Chinese characters with xpath 1.0?
Thank you.
Hi,
Just to let you know there is an error when you have a completely empty slide:
Error in
$<-.data.frame(
tmp, "master_file", value = "slideMaster1.xml") : replacement has 1 row, data has 0
See with and without last slide: test_template.pptx
Best,
Maxime
Although it is mentioned in the documentation, there is no officer::update() function available.
Hi David,
Small thing I noticed: when you have the "Hide background graphics"check box checked in a layout, it hides not only background graphics but some of the regular ones too.
Best,
Maxime
Hi David,
Hope you are doing well.
I have tested officer to generate word document, it is rather effective.
But I did not succeed in making continous break section (i have always a break page).
It is possible ??
An example of the expected result with ReporteRs
`# with ReporteRs
download.file("http://assets.howtobecome.com/assets/images/2014/01/police-officer.jpg", "img_test1.jpg")
download.file("https://russellkorets.files.wordpress.com/2013/09/police_officers.jpg", "img_test2.jpg")
doc <- ReporteRs::docx() %>%
ReporteRs::addParagraph(pot("Test ReporteRs"), textProperties(font.size = 24)) %>%
ReporteRs::addSection(ncol = 2, landscape = FALSE, columns.only = TRUE) %>%
ReporteRs::addImage(filename = "img_test1.jpg", width = 3, height = 2 ) %>%
ReporteRs::addImage(filename = "img_test1.jpg", width = 3, height = 2 ) %>%
ReporteRs::addSection(ncol = 2, landscape = FALSE, columns.only = TRUE) %>%
ReporteRs::addImage(filename = "img_test2.jpg", width = 3, height = 2 ) %>%
ReporteRs::addImage(filename = "img_test2.jpg", width = 3, height = 2 )
doc <- addSection(doc, columns.only = TRUE)
ReporteRs::writeDoc(doc, "test_ReporteRs.docx")`
And my code with officer
`# with officer
download.file("http://assets.howtobecome.com/assets/images/2014/01/police-officer.jpg", "img_test1.jpg")
download.file("https://russellkorets.files.wordpress.com/2013/09/police_officers.jpg", "img_test2.jpg")
doc <- officer::read_docx() %>%
officer::body_add_par("Test officer", style = "Normal")
doc <- officer::body_end_section(doc, colwidths = c(1)) %>%
officer::body_add_img(src = "img_test1.jpg", width = 3, height = 2 ) %>%
officer::body_add_img(src = "img_test1.jpg", width = 3, height = 2 ) %>%
officer::body_end_section(colwidths = c(.5, .5)) %>%
officer::body_add_img(src = "img_test2.jpg", width = 3, height = 2 ) %>%
officer::body_add_img(src = "img_test2.jpg", width = 3, height = 2 ) %>%
officer::body_end_section(colwidths = c(.5, .5))
print(doc, "test_officer.docx")`
Thanks in advance, a ++
Feature request: A better print method.
While interactively editing a document, it is a little cumbersome to check/confirm the cursor movement was successful, or to learn the current cursor position. Would it be possible to get a print method that perhaps prints to the console the paragraph before, current paragraph, and next paragraph after the current cursor position, with some unique ascii symbol showing exactly the current cursor position? Figures and tables could just have placeholders (e.g. ... [figure "figure title"]
This is not a help forum. Issues are a great way to keep track of tasks, enhancements, and bugs for your projects (https://guides.github.com/features/issues/).
If you are looking for help on how to use the package correctly, please visit Stackoverflow, officer
has its own tag! I may not answer when I am busy on something else but I usually read them and answer when possible (and when the example is reproducible).
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.