officer R package

repository·main·Indexed 20 days ago

https://github.com/davidgohel/officer

An R package for corporate reporting that allows users to programmatically create, manipulate, and extract content from Microsoft Word (.docx) and PowerPoint (.pptx) documents. It supports adding images, tables, and text, as well as importing document content into data.frames via functions like docx_summary() and pptx_summary(). It is part of the officeverse ecosystem and integrates with flextable, rvg, mschart, and officedown.

Tokens
933
Snippets
5
Records
6
Agent score
22%

What's inside officer

  1. Extend officer with specialized packages

    main

    The officer package is part of the officeverse ecosystem and integrates with several other packages to enhance document creation:

    • flextable: Provides a full API for creating high-quality tables for use in officer and R Markdown.
    • rvg: Enables embedding high-quality vector graphics in PowerPoint or Excel workbooks.
    • mschart: Allows the creation of native Office charts (rather than static images) within PowerPoint and Word.
    • officedown: Facilitates advanced formatting of Word documents when using R Markdown.
  2. Manipulate PowerPoint (*.pptx) documents

    main

    Use read_pptx() to initialize a PowerPoint document. You can then add content to existing or new slides. To finalize the file, use the print() function.

    Supported additions:

    • Images: Add plots (png or emf) to a slide.
    • Tables: Add data.frame objects; formatting is determined by the associated PowerPoint table style.
    • Text: Add text as paragraphs or within existing paragraphs; formatting is determined by the slide layout.

    Additionally, you can select specific slides or target specific shapes within a slide to remove them or add text.

    # Example workflow
    pptx <- read_pptx()
    pptx <- add_slide(pptx, layout = "Title Slide", master = "Office Theme")
    print(pptx, target = "my_presentation.pptx")
  3. Install the officer R package

    main

    You can install the latest stable version from CRAN or the development version from GitHub using devtools.

    # From CRAN
    install.packages("officer")
    
    # From GitHub (development version)
    devtools::install_github("davidgohel/officer")
  4. Manipulate Word (.docx) documents

    main

    Use read_docx() to initialize a Word document (either by loading an existing file or starting with an empty one). You can then add various R outputs to the document. To finalize and save the document, use the print() function.

    Supported additions:

    • Images: Add plots as png or emf files as whole paragraphs or inside existing paragraphs.
    • Tables: Add data.frame objects; formatting is determined by the associated Word table style.
    • Text: Add text as paragraphs or within existing paragraphs; formatting is determined by Word paragraph and text styles.
    • Field codes: Add MS Word field codes (e.g., for tables of contents, automatic numbering, or hyperlinks) inside paragraphs.
    # Example workflow
    doc <- read_docx()
    doc <- body_add_par(doc, "Hello World", style = "Normal")
    print(doc, target = "my_document.docx")
  5. Import PowerPoint content into a data.frame

    main

    Use pptx_summary() to read and import the content of a PowerPoint document (including paragraphs, tables, and images) into a data.frame.

    # Summarize PowerPoint content
    df <- pptx_summary("path/to/presentation.pptx")
  6. Import Word document content into a data.frame

    main

    To extract content from a Word document for analysis in R, use the following functions:

    • docx_summary(): Reads and imports paragraphs, tables, and section breaks into a data.frame.
    • docx_comments(): Reads comments from a Word document and organizes them into a data.frame.
    # Summarize document content
    df <- docx_summary("path/to/document.docx")
    
    # Extract comments
    comments_df <- docx_comments("path/to/document.docx")