如何从officer删除docx开头的空行

How to delete blank line at beginning of docx from officer

我正在使用 officerR 创建一个 Word (docx) 文档。这是代码:

library(officer)
library(magrittr)

my_docx = read_docx() %>% 
  # cursor_begin() %<>% 
  body_add_par("Hello here is a test sentence to see if there is a blank line above it.", style = "Normal") %>% 
  print(target = "test.docx")

它创建一个 docx 文档,在句子上方有一个空行。在字体的样式中设置的字体样式之前不是间距。我已经取消注释 cursor_begin() 但空白行仍然存在。我怎样才能摆脱这个?

感谢任何帮助!

要删除段落,您需要使用函数body_remove()。请参阅此处的文档:https://davidgohel.github.io/officer/articles/word.html#remove-content

library(officer)
library(magrittr)

my_docx <- read_docx() %>% 
  cursor_begin() %>% body_remove() %>% 
  body_add_par("Hello here is a test sentence to see if there is a blank line above it.", 
               style = "Normal") %>% 
  print(target = "test.docx")