Hello Guys, I have 10 word documents as they have the same template and all but each one has different data in the tables which are not overlapping with others. these 4 tables are separate.
I was trying to find the code to loop through these documents and read from them but could not - if you know how please let me know because I did the code for one document:
library(docxtractr)
doc <- read_docx("Test.docx")
doc_tables <- docx_extract_all_tbls(doc, guess_header = TRUE, preserve = FALSE, trim = TRUE)
5 Tables and ignore the first one - its the deadlines
Covert the tables into dataframe
Create DF
Make sure to add "Target" to the first table, and change the first col to "Specific Objectives"
df1 <- as.data.frame(doc_tables[2])
df2 <- as.data.frame(doc_tables[3])
df3 <- as.data.frame(doc_tables[4])
df4 <- as.data.frame(doc_tables[5])
joined_df <- rbind(df1,df2,df3,df4)
##Create the final dataframe to be updated with cells that have data
final <- read_docx("final.docx")
final_tables <- docx_extract_all_tbls(final, guess_header = TRUE, preserve = FALSE, trim = TRUE)
fdf1 <- as.data.frame(final_tables[2])
fdf2 <- as.data.frame(final_tables[3])
fdf3 <- as.data.frame(final_tables[4])
fdf4 <- as.data.frame(final_tables[5])
f_joined_df <- rbind(fdf1,fdf2,fdf3,fdf4)
Loop through joined DF to read the data and update the final accordingly
for (row in 1:nrow(joined_df)) {
sep_figure <- joined_df[row, "September.Report"]
if(sep_figure != "") {
f_joined_df[row, "September.Report"] <-sep_figure
print(sep_figure)
print(row)
}
}
My issue now that I want to update the final documents with the updated data frames but I cannot find a way to update the existing table in the word document from R, we should add a new table and I have 4 tables and there is text between them ... Any ideas please.
Best Regards, Hanna