Search code examples
rofficer

Officer - Format text


I am trying to change the font size of a paragraph using Officer but I am not able to do it. Can anyone tell me what I am doing wrong?

library(officer)
text_style <- fp_text(font.size = 12)

my_doc <- read_docx()
body_add_par(my_doc,"This is a test", style = text_style)
print(my_doc, target = "dummy.docx")

Solution

  • Function body_add_par() is expecting a style name (taken from those existing in the original document).

    If you want to add a paragraph made of formatted chunk of text, you will need to use body_add_fpar() as illustrated below .

    library(officer)
    text_style <- fp_text(font.size = 12)
    par_style <- fp_par(text.align = "justify")
    my_doc <- read_docx()
    my_doc <- body_add_fpar(my_doc, fpar( ftext("This is a test", prop = text_style), fp_p = par_style ) )
    print(my_doc, target = "dummy.docx")