'How can I change global fonts in officer

I am trying to make a word document from R. I use the officer package.

doc = docx(title = 'test')

When I do this, the standard template is used. Is there a way to change the template inside R? Or do i need to make a template outside R (in word) and then use that?

For example; How do i set the font in the document to calibri, size=10 ?

r


Solution 1:[1]

The workaround I found was to:

  1. open word and create a blank document
  2. Manually change the font size, font family, font color
  3. Save the document
  4. go back to R and read the document in as a template document
  5. Add unformatted paragraphs

For example:

  
patient_name <- "John Doe"

  d1 <- read_docx("your_template.docx") %>%
  body_add_par("")%>%
  body_add_par(format(Sys.Date(),"%m/%d/%Y"))%>%
  body_add_par("")%>%
  body_add_par(paste0("Dear ",patient_name,","))%>%
  body_add_par("")%>%
  body_add_par("I would like my document to have size 12, times new roman font.")%>%
  body_add_par("")%>%
  body_add_par("Sincerely,")%>%
  print(target="mytemplate_formatting.docx")

This is what happens without using the template

patient_name <- "John Doe"

d2 <- read_docx() %>%
  body_add_par("")%>%
  body_add_par(format(Sys.Date(),"%m/%d/%Y"))%>%
  body_add_par("")%>%
  body_add_par(paste0("Dear ",patient_name,","))%>%
  body_add_par("")%>%
  body_add_par("I would like my document to have size 12, times new roman font.")%>%
  body_add_par("")%>%
  body_add_par("Sincerely,")%>%
  print(target="default_formatting.docx")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Russ