library(officer)
doc <- read_docx()
doc <- body_add_par(doc, "Placeholder one")
doc <- body_add_par(doc, "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
doc <- body_replace_all_text(
doc,
old_value = "Placeholder",
new_value = "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
doc <- body_replace_all_text(
doc,
old_value = "placeholder",
new_value = "new",
only_at_cursor = FALSE,
ignore.case = TRUE
)
doc <- cursor_backward(doc)
docx_show_chunk(doc) # Output is 'new one'
# Use regex : replace all words starting with "n" with the word "example"
doc <- body_replace_all_text(doc, "\\bn.*?\\b", "example")
docx_show_chunk(doc) # Output is 'example one'
Run the code above in your browser using DataLab