Skip to content

ms609/doconv

 
 

Repository files navigation

doconv

doconv logo The tool offers a set of functions for converting ‘Microsoft Word’ or ‘Microsoft PowerPoint’ documents to ‘PDF’ format and also for converting them to images in the form of thumbnails.

In order to work, the package will use ‘Microsoft Word’, ‘Microsoft PowerPoint’, if they are not available program ‘LibreOffice’ can be used. A function is also provided to update all fields and table of contents of a Word document using ‘Microsoft Word’.

These features are also used to provide functions for visual-testing documents, format ‘doc’, ‘docx’, ‘ppt’, ‘pptx’, ‘html’, ‘pdf’ and ‘png’ are supported. The functions can be used with packages “testthat” and package “tinytest”.

R build status

Installation

You can install the latest version from GitHub with:

# install.packages("devtools")
devtools::install_github("ardata-fr/doconv")

Example

library(doconv)

Generate thumbails from file

You can generate thumbails as an image by using to_miniature:

docx_file <- system.file(package = "doconv", "doc-examples/example.docx")
to_miniature(
  filename = docx_file, 
  row = c(1, 1, 2, 2))

It uses ‘Microsoft Word’ or ‘Microsoft PowerPoint’ program to convert Word or PowerPoint documents to PDF. If program ‘Microsoft Word’ or ‘Microsoft PowerPoint’ is not available, it uses ‘LibreOffice’ to convert Word or PowerPoint documents to PDF.

Thus, this package can only be used when ‘Microsoft Word’ and ‘Microsoft PowerPoint’ programs are available or eventually ‘LibreOffice’.

Convert a PowerPoint file to PDF

docx_file <- system.file(package = "doconv", "doc-examples/example.pptx")
to_pdf(docx_file, output = "pptx_example.pdf")
to_miniature("pptx_example.pdf", width = 1000)

Convert a Word file to PDF

to_pdf(docx_file, output = "docx_example.pdf")

Update Word fields and TOC

library(officer)
library(doconv)

read_docx() |> 
  body_add_fpar(
    value = fpar(
      run_word_field("DOCPROPERTY \"coco\" \\* MERGEFORMAT"))) |>
  set_doc_properties(coco = "test") |>
  print(target = "output.docx") |>
  docx_update()

Setup

If not available on your machine and if possible, install ‘Microsoft Word’ or ‘Microsoft PowerPoint’ programs.

If ‘Microsoft Word’ and ‘Microsoft PowerPoint’ can not be installed, install ‘LibreOffice’ on your machine; please visit https://www.libreoffice.org/ and follow the installation instructions.

Use function check_libreoffice_export() to check that the software is installed and can export to PDF:

check_libreoffice_export()
#> [1] TRUE

If ‘Microsoft Word’ or ‘Microsoft PowerPoint’ are available on your machine, you can get images or pdf that looks exactly the same than the original document. If not ‘LibreOffice’ is used to convert Word documents to PDF or as an image, in this case, be aware that ‘LibreOffice’ does not always render the document as ‘Microsoft Word’ would do (sections can be misunderstood for example).

Authorization on macOS

If you are running R for ‘macOS’, you have to authorize few things before starting.

PDF processing will happen into a working directory managed with function working_directory().

Manual interventions are necessary to authorize ‘Word’ and ‘PowerPoint’ applications to write in a single directory: the working directory.

These permissions must be set manually, this is required by the macOS security policy. We think that this is not a problem because it is unlikely that you will use a Mac machine as a server.

User must manually click on few buttons:

  1. allow R to run ‘AppleScript’ scripts that will control Word
  2. allow Word to write to the working directory.

Don’t worry, these are one-time operations.

Related work

  • Packages docxtractr is providing convert_to_pdf() that works very well. The functionality integrated in Bob Rudis’ package depends only on ‘LibreOffice’.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • R 92.1%
  • AppleScript 4.1%
  • PowerShell 3.8%