将带有官包的合并docx文档导入R

时间:2019-08-27 09:37:52

标签: r import docx officer

我想使用Officer包导入和修改R中的docx文件。但是,当我使用建议的功能时,R仅导入一个空的data.frame。考虑以下示例:

# Packages
library("magrittr")
library("officer")

# Create example docx on computer
my_doc1 <- read_docx() %>% 
  body_add_par('aaa', style = 'Normal')
my_doc2 <- read_docx() %>% 
  body_add_par('bbb', style = 'Normal')
my_doc3 <- read_docx() %>% 
  body_add_par('ccc', style = 'Normal')

print(my_doc1, target = 'C:/your-path/aaa.docx')
print(my_doc2, target = 'C:/your-path/bbb.docx')
print(my_doc3, target = 'C:/your-path/ccc.docx')

# Combine all docx
my_doc_all <- read_docx() %>% 
  body_add_docx(src = 'C:/your-path/aaa.docx') %>%
  body_add_docx(src = 'C:/your-path/bbb.docx') %>%
  body_add_docx(src = 'C:/your-path/ccc.docx')

# Print combined docx to computer
print(my_doc_all, target = 'C:/your-path/all.docx')

这是我目前的情况。请注意,这种情况已给出。先前的步骤无法更改。

现在,我想导入合并的docx文件并在R中对其进行修改。根据documentation of officer (p. 26)this thread (answer by David Gohel),我应该能够使用以下代码来实现它:

# Read combined docx file
read_my_doc_all <- read_docx("C:/your-path/all.docx")

# Return dataset representing the docx document
docx_summary(my_doc_all)

但是,输出是一个data.frame,其中有一个空行:

### doc_index content_type style_name text level num_id
### 1         1    paragraph         NA         NA     NA

我本人正在研究此问题,并发现,如果我们不必在最前面合并多个docx文档(如本示例开头所示),则一切正常。如果我们在R中创建一个文件并将其导出/导入,则一切正常。

我如何将带有doctor包的合并的docx文档导入R?如果可能的话,我想坚持使用Officer包以保持R语法的连贯性。

0 个答案:

没有答案