I am looking at adding an image to a word document using the officer
package. The word document template looks like this:
These are all text boxes and I am able to replace all the text such as StudentName, Enrollment number etc. but don't know how to add an image to the text box called picture
. This is what I have tried:
my_doc <- read_docx('templates/form_101.docx') %>%
body_replace_all_text('Coursename', 'NTCC 01') %>%
body_replace_all_text('XXX', 'III') %>%
body_replace_all_text('FromDate', '26 Oct') %>%
body_replace_all_text('ToDate', '02 Nov 20') %>%
body_replace_all_text('StudentName', 'Dhiraj Khanna') %>%
body_replace_all_text('12345', '51511-W') %>%
body_replace_all_text('ABC', 'A') %>%
cursor_reach('picture') %>%
# cursor_reach('picture') %>%
# body_remove()
# body_bookmark('picture') %>%
# body_replace_img_at_bkm('picture',
# value = external_img(src = 'data/images/pic1.png',
# width = .2, height = .2))
body_add_img(src = 'data/images/pic1.png', width = .2, height = .2,
pos = 'on')
print(my_doc, target = 'templates/output.docx')
None of these approaches (including the commented out ones) give me the desired result. So how do I go about inserting an image in the text box picture
.
The template is available here
It works when you alter the template. You need to
I have uploaded a correctly altered file here.
You can then use the function
body_replace_img_at_bkm()
as follows: