简体   繁体   中英

Address and manipulate text in a textfield and footer in Word documents with R officer package

I am working on a solution to change the text in several Word documents with similar structure. The text positions that I want to change are 1. in a textfield related to the header/footer and second in the first footer . I don't know how to address the text inside these elements does anyone? Is it possible at all? Image of that document Here is my example:

library(officer)
library(magrittr)

docact <- read_docx("PATH/officertest/textfieldfooter.docx")

docx_summary(docact)

doc_index content_type style_name text level num_id 1 1 paragraph NA Here is some other text that is easy to address and manipulate. NA NA

cursor_reach(docact, keyword = "I want to address this textfield") %>%
  slip_in_text("This is the text that I want to add ", pos = "after", style = "Default Paragraph Font")

gives an Error: I want to address this textfield has not been found in the document

headers_replace_all_text(docact, "footertext", "the text that should be the replacement",
         only_at_cursor = FALSE,
         warn = TRUE)

Warning messages: 1: In header$replace_all_text(old_value, new_value, only_at_cursor, : Found 0 instances of 'footertext' in the document. 2: In header$replace_all_text(old_value, new_value, only_at_cursor, : Found 0 instances of 'footertext' in the document. 3: In header$replace_all_text(old_value, new_value, only_at_cursor, : Found 0 instances of 'footertext' in the document.

I think you need this function in the officer package:

docact <- footers_replace_all_text(docact,"old_value","new_value")

Replacing text in a text field did not work for me yet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM