Using reportRs pacakge, I'm trying to add several graphs(.png/.jpg) which are named as e.g. test-0,test-1,test-2 etc into a pptx file. These graphs have been extracted from a pdf named e.g. test using im.convert function.I can add them individually but not able to automate the code for graphs,title, slide number, date etc in loop which can figure out how many graphs with 'test' name are there in a folder and then import them in the pptx one by one in a new slide ata time and one final pptx file.
sample code:
library(animation)
im.convert("Test.pdf", output = "Test.png", extra.opts="-density 150")
library("ReporteRs")
doc <- pptx()
doc <- pptx(template = templateDir)
doc <- addSlide( doc, slide.layout = 'Competative Landscape' )
doc <- addTitle(doc, paste("Test-0"))
doc <- addImage(doc, "Test-0.png")
:
:
:
:
doc <- addSlide( doc, slide.layout = 'Competative Landscape' )
doc <- addTitle(doc, paste("Test-3"))`enter code here`
doc <- addImage(doc, "Test-3.png")
You could try using the list.files function to find the number of png files with the name Test in a folder.
sample code:
list_of_files=list.files(path = "C:/output_folder", pattern = c("Test",".png"))
library("ReporteRs")
doc <- pptx()
doc <- pptx(template = templateDir)
for( i in 0:(length(list_of_files)-1))
{
doc <- addSlide( doc, slide.layout = 'Competative Landscape' )
doc <- addTitle(doc, paste0("Test-",i))
doc <- addImage(doc, paste0("Test-",i,".png"))
}
You could also try the eoffice package:
install.package("eoffice")
fig<-infigure("figes",savegg=T)
topptx(fig,file="test.pptx")
##or
infigure("figs",showfig=T)
topptx(fig,file="test.pptx")
Related
i'm trying to create a ggplot editable with ppt or word
Here's the code:
library(tidyverse)
library(rvg)
library(officer)
##I create a dml object
anyplot = dml(code = barplot(1:5, col = 2:6), bg = "wheat")
## add slide
doc <- read_pptx()
##specify object and location of object
doc <- add_slide(doc, "Title and Content", "Office Theme")
doc <- ph_with.dml(doc, anyplot, location = ph_location_fullsize())
## print it in pptx
print(doc, target = 'plot.pptx')
But I keep getting could not find function "ph_with.dml"
note: I understand ph_with.dml is the new version of ph_with_vg (which cannot be find either)
Any suggestion or different approach would be appreciated!
I am working on an R Shiny app where a user supplies information that modifies an existing Word document for the user to download. I've had trouble getting R Shiny to download the resulting new Word document. I've tried regular hyperlinks, and that doesn't seem to work.
(Edit: After typing up this post, I came across how to download files with hyperlinks. I forgot files need to be placed inside a www folder, as specified here: Shiny hyperlink relative path to a file. So although I can get my Shiny App to work using this approach, I'd still like to know why my example below is not working).
I came across Github Issue #145 (https://github.com/davidgohel/officer/issues/145) which almost has the solution. But the pptx being downloaded is created from scratch whereas I want to start from an EXISTING Word docx.
In my code example, there are 3 downloadHandler buttons:
Uses the original pptx download code example from Github Issue #145
The second modifies the above to download a docx
The third button is my attempt to download an existing and modified docx file
The third button is not working as I had hoped. If I had to guess, I think it has to do with my template being from from read_docx. It looks like it creates some temporary file behind the scenes. But I don't know where to go from here.
For completeness, here are some related links:
Reporters package to download docx report from shiny (uses ReporeRs which is older than officer R package)
Writing word documents with the officer package: How to combine several rdocx objects? (Helpful if merging existing docx to the tempfile in my example)
downloadHandler reference: https://shiny.rstudio.com/reference/shiny/latest/downloadHandler.html
# -------- Example code ------------
library(shiny)
library(officer)
library(mschart)
library(dplyr)
# Create template folder and file. (Irrelevant if already exists.)
dir.create("www")
read_docx() %>%
body_add_par("My template file") %>%
print(., target = "www/template.docx")
# Existing file as Template
mytemplate <- read_docx(path = "www/template.docx")
# For Button 1
gen_pptx <- function(chart, file) {
read_pptx() %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with_chart(chart = chart) %>%
print(target = file)
}
chart <- data.frame(x = letters[1:3], y = 1:3) %>%
ms_barchart(x = "x", y = "y")
# For button 2
gen_docx <- function(file) {
read_docx() %>%
body_add_par("Hello World") %>%
print(target = file)
}
# For button 3
gen_docx2 <- function(file, doc) {
file %>%
body_add_par("Hello World") %>%
body_add_docx(src = doc) %>%
print(target = file)
}
ui <- fluidPage(
titlePanel("Example"),
downloadButton("chart", "Get Chart"),
downloadButton("document", "Get New Doc"),
downloadButton("document2", "Get Doc from Template"),
tags$hr(),
tags$p("Example hyperlink works"),
tags$a(href='template.docx', target='_blank', 'Can only download from www folder', download = 'template.docx')
)
server <- function(input, output) {
output$chart <- downloadHandler(
filename = function() paste0("chart_", Sys.Date(), ".pptx"),
content = function(file) {
file_pptx <- tempfile(fileext = ".pptx")
gen_pptx(chart, file_pptx)
file.rename( from = file_pptx, to = file )
}
)
output$document <- downloadHandler(
filename = function() paste0("doc_", Sys.Date(), ".docx"),
content = function(file) {
file_docx <- tempfile(fileext = ".docx")
gen_docx(file_docx)
file.rename( from = file_docx, to = file )
}
)
output$document2 <- downloadHandler(
filename = function() paste0("doc_", Sys.Date(), ".docx"),
content = function(file) {
file_docx <- tempfile(fileext = ".docx")
gen_docx2(file_docx, mytemplate)
file.rename( from = file_docx, to = file )
}
)
}
shinyApp(ui = ui, server = server)
Do not use print to generate docx file in helper function gen_docx2.
Instead, you should use print as a last step in content function to return file to the downloadHandler.
Simple example:
gen_docx2 <- function(file, doc) {
file %>%
body_add_par("Hello World") %>%
body_add_docx(src = doc)
}
notice the function above does not print anything
outputdocument2 <- downloadHandler(
filename = function() {
paste0("doc_", Sys.Date(), ".docx")
},
content = function(file) {
doc <- read_docx() %>% gen_docx2(file_docx, mytemplate)
print(doc, target = file)
}
)
Use read_docx() to generate temporary word file and then use print(doc, target = file) in the end to pass it to the downloadHandler. The function file.rename is not needed.
I'm trying to add a paragraph in an pptx object in R using ReporteRs, but I can't set the height and the width even when using the right arguments.
For example :
doc = pptx()
doc <- addSlide(doc, "Slide")
doc <- addParagraph(doc,"ok",height=2,width=2,offx = 0,offy = 0)
writeDoc(doc, "test.pptx" )
Anyone manage to get a paragraph with custom sizes ?
Thank you
The following should solve your issue
library(ReporteRs)
doc = pptx()
doc <- addSlide(doc, "Title and Content")
doc <- addParagraph(doc,
pot("ok"),
par.properties = parProperties(),height=2,width=2,offx = 0,offy = 0)
writeDoc(doc, "test.pptx" )
I'm trying (as hard as i can) to create a script that will generate formatted word documents from plain text files using R language and reporteRs.
To extract text from one txt i'm using this code found on this thread Dealing with readLines() function in R :
fileName <- "C:/MyFolder/TEXT_TO_BE_PROCESSED.txt"
con <- file(fileName,open="r")
line <- readLines(con)
close(con)
Then add the extracted text to docx with this :
doc <- docx(template="temp.docx")
Next, adding the title (first line of the txt file)
doc <- addParagraph( doc, value = line[1], bookmark = "titre", stylename = "Titre")
then the body of the txt file
doc <- addParagraph( doc, value = line[2:length(line)], value = line[2:55], stylename = "Contenu")
Finally I create the docx
writeDoc(doc, file = "output-file.docx")
I want to be able to create a loop so I can generate multiple docx from multiple txt files. I will really appreciate your help
You can do something like this with lapply
myFiles <- c("C:/MyFolder/TEXT_TO_BE_PROCESSED.txt", "C:/MyFolder/TEXT_TO_BE_PROCESSED2.txt") # or use list.files()
lapply(myFiles, function(fileName){
con <- file(fileName,open="r")
line <- readLines(con) # you could just call readLines(fileName)
close(con)
doc <- docx(template="temp.docx")
doc <- addParagraph( doc, value = line[1], bookmark = "titre", stylename = "Titre")
doc <- addParagraph( doc, value = line[2:length(line)], value = line[2:55], stylename = "Contenu")
writeDoc(doc, file = paste0(fileName, "out.docx"))
})
The solution :
myFiles <- list.files()
lapply(myFiles, function(fileName){
line <- readLines(fileName)
doc <- docx(template="temp.docx")
doc <- addParagraph( doc, value = line[1], bookmark = "titre",
stylename = "Titre ยป)
doc <- addParagraph( doc, value = line[2:length(line)], stylename = "Contenu")
writeDoc(doc, file = paste0(fileName, ".docx"))
})
Thank you again Richard
I wanted to highlighted some text in a PDF document using R. I want to search a PDF document for some text and highlight the text if found. I searched for packages which could do this.
pdftools and pdfsearch are packages which help in handling PDF documents. These packages mainly handle converting pdf to text and doing any sort of manipulation.
Is there a way in which we can highlight a PDF document using R?
I was able to highlight some keywords in a PDF with the following code. There are four steps :
Save wikipedia page to PDF;
Convert the PDF to word document with the Word Software (There is an OCR!!);
Highlight the keywords in the word document;
Save the word document as PDF.
library(RDCOMClient)
library(DescTools)
library(pagedown)
#############################################
#### Step 1 : Save wikipedia page as PDF ####
#############################################
chrome_print(input = "https://en.wikipedia.org/wiki/Cat",
output = "C:\\Text_PDF_Cat.pdf")
path_PDF <- "C:\\Text_PDF_Cat.pdf"
path_Word <- "C:\\Text_PDF_Cat.docx"
################################################################
#### Step 2 : Convert PDF to word document with OCR of Word ####
################################################################
wordApp <- COMCreate("Word.Application")
wordApp[["Visible"]] <- TRUE
wordApp[["DisplayAlerts"]] <- FALSE
doc <- wordApp[["Documents"]]$Open(normalizePath(path_PDF),
ConfirmConversions = FALSE)
doc$SaveAs2(path_Word)
doc_Selection <- wordApp$Selection()
######################################################
#### Step 3 : Highlight keywords in word document ####
######################################################
move_To_Beginning_Doc <- function(doc_Selection)
{
doc_Selection$HomeKey(Unit = wdConst$wdStory) # Need DescTools for wdConst$wdStory
}
highlight_Text_Regex_Word <- function(doc,
doc_Selection,
words_To_Highlight,
colorIndex = 7,
nb_Max_Word = 100)
{
for(i in words_To_Highlight)
{
move_To_Beginning_Doc(doc_Selection)
for(j in 1 : nb_Max_Word)
{
doc_Selection$Find()$Execute(FindText = i, MatchCase = FALSE)
doc_Selection_Range <- doc_Selection$Range()
doc_Selection_Range[["HighlightColorIndex"]] <- colorIndex
}
}
}
highlight_Text_Regex_Word(doc, doc_Selection,
words_To_Highlight = c("cat", "domestic", "quick"),
colorIndex = 7, nb_Max_Word = 100)
###############################################
#### Step 4 : Convert word document to pdf ####
###############################################
path_PDF_Highlighted <- "C:\\Text_PDF_Cat_Highlighted.pdf"
wordApp[["ActiveDocument"]]$SaveAs(path_PDF_Highlighted, FileFormat = 17) # FileFormat = 17 saves as .PDF
doc$Close()
wordApp$Quit() # quit wordApp