# NOT RUN {
library(officer)
doc <- read_docx()
doc <- body_add_par(doc, "paragraph 1", style = "Normal")
doc <- body_add_par(doc, "paragraph 2", style = "Normal")
doc <- body_add_par(doc, "paragraph 3", style = "Normal")
doc <- body_add_par(doc, "paragraph 4", style = "Normal")
doc <- body_add_par(doc, "paragraph 5", style = "Normal")
doc <- body_add_par(doc, "paragraph 6", style = "Normal")
doc <- body_add_par(doc, "paragraph 7", style = "Normal")
# default template contains only an empty paragraph
# Using cursor_begin and body_remove, we can delete it
doc <- cursor_begin(doc)
doc <- body_remove(doc)
# Let add text at the beginning of the
# paragraph containing text "paragraph 4"
doc <- cursor_reach(doc, keyword = "paragraph 4")
doc <- slip_in_text(doc, "This is ", pos = "before", style = "Default Paragraph Font")
doc <- # move the cursor forward and end a section
doc <- cursor_forward(doc)
doc <- body_add_par(doc, "The section stop here", style = "Normal")
doc <- body_end_section_landscape(doc)
# move the cursor at the end of the document
doc <- cursor_end(doc)
doc <- body_add_par(doc, "The document ends now", style = "Normal")
print(doc, target = tempfile(fileext = ".docx"))
# cursor_bookmark ----
doc <- read_docx()
doc <- body_add_par(doc, "centered text", style = "centered")
doc <- body_bookmark(doc, "text_to_replace")
doc <- body_add_par(doc, "A title", style = "heading 1")
doc <- body_add_par(doc, "Hello world!", style = "Normal")
doc <- cursor_bookmark(doc, "text_to_replace")
doc <- body_add_table(doc, value = iris, style = "table_template")
print(doc, target = tempfile(fileext = ".docx"))
# }
Run the code above in your browser using DataLab