I am trying to find a way to automate generating word documents based on a data frame containing addresses that I would like inserted into separate word documents:
`DF <- data.frame(Company=c('Alpha','Beta', 'Gamma'),
Name= c('John Doe', 'Jane Doe', 'Jack Smith'),
Address=c('123 Main St', '234 Maple St', '456 30th St' ),
Address_Line_2=c('Suite A', '', 'Suite B'),
City_State_Zip=c('Los Angeles, CA 12345', 'New York City, NY 23456', 'Chicago, IL 3456'))`
I have a word doc template I use and I use "Find and Replace" function code for generating word documents:
document <- read_docx("C:\\Mock_Path\\Letter.docx")
document <- document %>%
officer::body_replace_all_text(., old_value = "Today’s Date", new_value = "July 24, 2023", only_at_cursor = FALSE, ignore.case = TRUE) %>%
officer::body_replace_all_text(., old_value = "Company Name",new_value = "Alpha", only_at_cursor = FALSE, ignore.case = TRUE) %>%
officer::body_replace_all_text(., old_value = "First Name/Last Name", new_value = "John Doe", only_at_cursor = FALSE, ignore.case =TRUE) %>%
officer::body_replace_all_text(., old_value = "Company Address", new_value = "123 Main St", only_at_cursor = FALSE, ignore.case =TRUE)%>%
officer::body_replace_all_text(., old_value = "Address Line 2", new_value = "Suite A", only_at_cursor = FALSE, ignore.case = TRUE)%>%
officer::body_replace_all_text(document, old_value = "City, State Zip", new_value = "Los Angeles, CA 1234", only_at_cursor =FALSE, ignore.case = TRUE)
print(document ,""C:\\Mock_Path\\Addressed_Letter.docx")`
My method for using a find and replace function from a template works but since my actual data frame is much larger, I was wondering if there is a way to automate the process of printing multiple word documents working down the rows in my data frame. I am hoping to generate something like this: I could really appreciate any help or advice. Thanks!