1

Hi there I want to be able to change the style, font, and color of my text in body_add_par from the officer package. Is there a simple way to do this?

Note: I was able to change the font, size, and color) using fp_text (see directly below) but then it wouldnt let me keep the styles ("heading" or "normal")

format_paragraph <- fp_text(font.family='Arial', font.size=10, color = "black") format_title <- fp_text(font.family='Arial', font.size=12, color = "red")

data <- structure(list(Site = "Waterbody1", `Water Quality Paragraph` = "Water temperature was 9.9°C, conductivity was 6.9 microseimens per centimetre (uS/cm), and pH was 7.9. The dissolved oxygen concentration was 8.9 mg/L, which was above the guideline for protection of aquatic life (6.5 mg/L) (CCME 1999) and suitable for most fish species."), row.names = c(NA, 
-1L), class = "data.frame")

Example input:

library(officer)
library(tidyverse)
# create empty Word file
sample_doc <- read_docx()
#Waterbody 1
sample_doc <- sample_doc %>% body_add_par(data$Site, style = "heading 1") 
sample_doc <- sample_doc %>% body_add_par(data$`Water Quality Paragraph`, style = "Normal")
print(sample_doc, target = "sample_file.docx")

Current Output enter image description here

Desired Output (

enter image description here

Phil
  • 7,287
  • 3
  • 36
  • 66

1 Answers1

0

It looks like you need create a fpar from you ftext with the fp_text options, to create a paragraph :

                                                                                                                                                                                                                                                                                                                                                                          
sample_doc <- read_docx()

formatted_text <- ftext(data$Site,fp_text(color = "red")) %>%
  fpar()
#Waterbody 1
sample_doc <- sample_doc %>% body_add(formatted_text, style = "heading 1") 
sample_doc <- sample_doc %>% body_add_par(data$`Water Quality Paragraph`, style = "Normal")
print(sample_doc, target = "sample_file.docx")

I do not see simpler solution

denis
  • 5,580
  • 1
  • 13
  • 40