change_styles: Replace Styles in a Word Document

Description Usage Arguments Examples

View source: R/read_docx.R

Description

Replace styles with others in a Word document. This function can be used for paragraph, run/character and table styles.

Usage

1
change_styles(x, mapstyles)

Arguments

x

an rdocx object

mapstyles

a named list, names are the replacement style, content (as a character vector) are the styles to be replaced. Use styles_info() to display available styles.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# creating a sample docx so that we can illustrate how
# to change styles
doc_1 <- read_docx()

doc_1 <- body_add_par(doc_1, "A title", style = "heading 1")
doc_1 <- body_add_par(doc_1, "", style = "Normal")
doc_1 <- slip_in_text(doc_1, "Message is: ",
  style = "Default Paragraph Font"
)
doc_1 <- body_add_par(doc_1, "Hello ", style = "Normal")
doc_1 <- slip_in_text(doc_1, "world", style = "Default Paragraph Font")
doc_1 <- slip_in_text(doc_1, " with a link",
  style = "strong",
  pos = "after", hyperlink = "https://davidgohel.github.io/officer/"
)
doc_1 <- body_add_par(doc_1, "Another title", style = "heading 2")
doc_1 <- body_add_par(doc_1, "Hello world!", style = "Normal")

file <- print(doc_1, target = tempfile(fileext = ".docx"))

# now we can illustrate how
# to change styles with `change_styles`
doc_2 <- read_docx(path = file)
mapstyles <- list(
  "centered" = c("Normal", "heading 2"),
  "strong" = "Default Paragraph Font"
)
doc_2 <- change_styles(doc_2, mapstyles = mapstyles)

print(doc_2, target = tempfile(fileext = ".docx"))

Example output



officer documentation built on July 21, 2021, 5:07 p.m.