# NOT RUN {
library(magrittr)
doc <- read_docx() %>%
body_add_par("Placeholder one") %>%
body_add_par("Placeholder two")
# Show text chunk at cursor
docx_show_chunk(doc) # Output is 'Placeholder two'
# Simple search-and-replace at current cursor, with regex turned off
body_replace_all_text(doc, "Placeholder", "new", only_at_cursor=TRUE, fixed=TRUE)
docx_show_chunk(doc) # Output is 'new two'
# Do the same, but in the entire document and ignoring case
body_replace_all_text(doc, "placeholder", "new", only_at_cursor=FALSE, ignore.case=TRUE)
cursor_backward(doc)
docx_show_chunk(doc) # Output is 'new one'
# Use regex : replace all words starting with "n" with the word "example"
body_replace_all_text(doc, "\\bn.*?\\b", "example")
docx_show_chunk(doc) # Output is 'example one'
# }
Run the code above in your browser using DataCamp Workspace