Import body content and footnotes of a Word document into an rdocx object.
The function is similar to body_add_docx()
but instead of adding
the content as an external object, the document is read and all its
content is appended to the target document.
Arguments
- x
an rdocx object
- src
path to docx file to import
- par_style_mapping, run_style_mapping, tbl_style_mapping
Named lists describing how to remap styles from the source document (
src
) to styles available in the target documentx
. For each list entry, the name of the element is the target style (inx
), and the value is a character vector of style names from the source document that should be replaced by this target style.par_style_mapping
: applies to paragraph styles.run_style_mapping
: applies to character (run) styles.tbl_style_mapping
: applies to table styles.
Examples:
par_style_mapping = list( "Normal" = c("List Paragraph", "Body Text"), "heading 1" = "Heading 1" ) run_style_mapping = list( "Emphasis" = c("Emphasis", "Italic") ) tbl_style_mapping = list( "Normal Table" = c("Light Shading") )
Use
styles_info()
to inspect available styles and verify their names.- prepend_chunks_on_styles
A named list of run chunks to prepend to runs with specific styles. The names of the list are paragraph style names and the values run chunks to prepend. The first motivation for this argument is to allow prepending of runs in paragraphs with a defined style, for example to add a
run_autonum()
with all image captions.
Details
The following operations are performed when importing a document:
Numbering are copied from the source document to the target document.
Styles are not copied. If styles in the source document do not exist in the target document, the style specified in the
par_style_mapping
,run_style_mapping
andtbl_style_mapping
arguments will be used instead. If no mapping is provided, the default style will be used and a warning is emitted.
See also
Other functions for adding content:
body_add_blocks()
,
body_add_break()
,
body_add_caption()
,
body_add_docx()
,
body_add_fpar()
,
body_add_gg()
,
body_add_img()
,
body_add_par()
,
body_add_plot()
,
body_add_table()
,
body_add_toc()
,
body_append_start_context()
Examples
library(officer)
# example file from the package
file_input <- system.file(
package = "officer",
"doc_examples/example.docx"
)
# create a new rdocx document
x <- read_docx()
# import content from file_input
x <- body_import_docx(
x = x,
src = file_input,
# style mapping for paragraphs and tables
par_style_mapping = list(
"Normal" = c("List Paragraph")
),
tbl_style_mapping = list(
"Normal Table" = "Light Shading"
)
)
# Create temporary file
tf <- tempfile(fileext = ".docx")
# write to file
print(x, target = tf)