| Title: | Convert an XLSForm to a 'Microsoft Word' or PDF Document |
|---|---|
| Description: | Converts XLSForm questionnaires, as used by 'Open Data Kit' ('ODK') and 'KoboToolbox', into formatted paper versions for interviewing and review. See <https://xlsform.org> for the XLSForm standard. Output is a 'Microsoft Word' document (.docx) or, optionally, a PDF produced from that document with 'LibreOffice'. Supports multilingual forms, nested question numbering, skip logic display, and question and choice media. |
| Authors: | Masud Rahman [aut, cre, cph] |
| Maintainer: | Masud Rahman <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.0.9000 |
| Built: | 2026-10-22 10:12:12 UTC |
| Source: | https://github.com/masud90/convertxlsform |
Runs convertxlsform() with options taken from the command line. Use it
from a shell without opening R:
cli(args = commandArgs(trailingOnly = TRUE))cli(args = commandArgs(trailingOnly = TRUE))
args |
Character vector of arguments. Defaults to the trailing command-line arguments. |
Rscript -e 'convertxlsform::cli()' form.xlsx --format pdf --lang bn
Put the XLSForm path and the options after the quoted expression. If a
script or CI job needs the exit status, use
Rscript -e 'quit(status = convertxlsform::cli())' form.xlsx. The plain
form above also works for normal use.
This function is a terminal interface. Its help and version text go to
standard output, and usage errors go to standard error, because that
output is its purpose. Progress messages are sent with message() and
--quiet suppresses them (the same as verbose = FALSE in
convertxlsform()).
The exit status invisibly: 0 on success, 1 on a usage error or a failed conversion. The function does not quit R.
cli(args): form.xlsx [options]. Options accept --name value and
--name=value.
-o, --output PATH: output file or folder (default: next to the
form).
-f, --format docx|pdf|both: output format (default: from the
extension of --output, else docx).
-l, --lang LANG: language code, name or header suffix.
--flat-numbers: number questions 1, 2, 3 instead of 1.1, 1.2.
--no-relevants: hide skip logic.
--no-constraints: hide constraints.
--no-hints: hide hints.
--no-choice-codes: hide choice codes.
--show-names: show variable names.
--include-hidden: include calculate, start, end and similar rows.
--media-root DIR: folder for media and external files (default: the
folder of the form).
--title TEXT: form title.
--page-size A4|letter (default A4).
--landscape: landscape orientation.
--margins IN: margins in inches (default 1).
--font NAME (default Arial) and --font-size PT (default 11).
--template FILE: reference .docx for styles.
--max-choices N: longest choice list printed in full (default 30).
--repeat-copies N: copies of each repeat group (default 1).
--overwrite: replace existing output.
--quiet: no progress messages.
-h, --help: show the help and stop.
--version: show the package version and stop.
path <- system.file("extdata", "minimal", "minimal.xlsx", package = "convertxlsform") cli(c(path, "-o", tempdir(), "--overwrite", "--quiet"))path <- system.file("extdata", "minimal", "minimal.xlsx", package = "convertxlsform") cli(c(path, "-o", tempdir(), "--overwrite", "--quiet"))
Reads an XLSForm (as used by Open Data Kit and KoboToolbox) and writes a
formatted paper version for interviewing and review. The default output is
a Word document next to the input file. A PDF is made by converting the
Word document with LibreOffice (or Microsoft Word through the doconv
package); see docx_to_pdf() and has_libreoffice().
convertxlsform(xlsform, output = NULL, format = NULL, lang = NULL, ...)convertxlsform(xlsform, output = NULL, format = NULL, lang = NULL, ...)
xlsform |
Path to an XLSForm ( |
output |
Output file or folder, see Details. |
format |
|
lang |
Language to use: a code ( |
... |
Further options, all named. Defaults are in brackets.
|
The path or paths of the files written, as a character vector, invisibly.
output = NULL: same folder and base name as xlsform, with the new
extension.
output is an existing folder: <base name>.<ext> inside it.
output is a file path: used as given. Its extension (.docx or
.pdf) sets format when format is NULL, and must agree with
format otherwise.
Two formats (format = c("docx", "pdf")) write two files with the same
base name. With a file path in output, its extension is replaced.
An existing file is never replaced unless overwrite = TRUE.
xlsform_outline() to inspect a form before converting it,
docx_to_pdf(), and cli() for the terminal.
path <- system.file("extdata", "minimal", "minimal.xlsx", package = "convertxlsform") out <- file.path(tempdir(), "minimal.docx") convertxlsform(path, output = out, verbose = FALSE, overwrite = TRUE) # A multilingual form: another language, flat question numbers, US letter path_ml <- system.file("extdata", "multilang", "multilang.xlsx", package = "convertxlsform") convertxlsform(path_ml, output = tempdir(), lang = "bn", nested_serial_numbers = FALSE, page_size = "letter", verbose = FALSE, overwrite = TRUE) # PDF needs LibreOffice if (interactive() && has_libreoffice()) { convertxlsform(path, output = tempdir(), format = "pdf", verbose = FALSE, overwrite = TRUE) }path <- system.file("extdata", "minimal", "minimal.xlsx", package = "convertxlsform") out <- file.path(tempdir(), "minimal.docx") convertxlsform(path, output = out, verbose = FALSE, overwrite = TRUE) # A multilingual form: another language, flat question numbers, US letter path_ml <- system.file("extdata", "multilang", "multilang.xlsx", package = "convertxlsform") convertxlsform(path_ml, output = tempdir(), lang = "bn", nested_serial_numbers = FALSE, page_size = "letter", verbose = FALSE, overwrite = TRUE) # PDF needs LibreOffice if (interactive() && has_libreoffice()) { convertxlsform(path, output = tempdir(), format = "pdf", verbose = FALSE, overwrite = TRUE) }
Converts a .docx file to PDF with LibreOffice, or with Microsoft Word
through the doconv package. LibreOffice is free and runs on all
platforms. Each call uses a fresh temporary LibreOffice profile, so it
works while a LibreOffice window is open and in parallel runs.
docx_to_pdf( docx, pdf = NULL, engine = c("auto", "libreoffice", "word"), overwrite = FALSE, verbose = TRUE )docx_to_pdf( docx, pdf = NULL, engine = c("auto", "libreoffice", "word"), overwrite = FALSE, verbose = TRUE )
docx |
Path to an existing |
pdf |
Path of the PDF to write. |
engine |
|
overwrite |
Overwrite an existing |
verbose |
Print progress messages? Default |
If LibreOffice is installed in an unusual place, set the option
convertxlsform.soffice or the environment variable
CONVERTXLSFORM_SOFFICE to the path of the soffice binary.
The path of the PDF, invisibly.
if (interactive() && has_libreoffice()) { docx <- tempfile(fileext = ".docx") doc <- officer::read_docx() doc <- officer::body_add_par(doc, "Hello") print(doc, target = docx) pdf <- docx_to_pdf(docx, pdf = tempfile(fileext = ".pdf")) file.exists(pdf) }if (interactive() && has_libreoffice()) { docx <- tempfile(fileext = ".docx") doc <- officer::read_docx() doc <- officer::body_add_par(doc, "Hello") print(doc, target = docx) pdf <- docx_to_pdf(docx, pdf = tempfile(fileext = ".pdf")) file.exists(pdf) }
Reports whether a LibreOffice binary can be found, which is needed to
convert Word documents to PDF with docx_to_pdf(). The search checks the
option convertxlsform.soffice, the environment variable
CONVERTXLSFORM_SOFFICE, the PATH, and the default install locations
for the operating system.
has_libreoffice()has_libreoffice()
TRUE or FALSE.
has_libreoffice()has_libreoffice()
Finds the languages used in the label::<language> style columns of the
survey and choices sheets, for example label::English (en), label::en
or label::English.
xlsform_languages(xlsform)xlsform_languages(xlsform)
xlsform |
Path to an XLSForm ( |
A data.frame with one row per language and columns suffix (the
text after ::), code, name and default (TRUE for the language used
when no lang is requested: the default_language of the settings sheet
if it matches, otherwise the first language). The data.frame has no rows
for a single-language form.
path <- system.file("extdata", "multilang", "multilang.xlsx", package = "convertxlsform") xlsform_languages(path)path <- system.file("extdata", "multilang", "multilang.xlsx", package = "convertxlsform") xlsform_languages(path)
Parses an XLSForm and returns one row for each item that would appear on the paper form: sections, questions, notes and the end of repeat groups. It shows the question numbers, the answer type, the number of choices, and the skip logic and constraints in plain language, so a form can be checked before it is converted.
xlsform_outline(xlsform, lang = NULL, ...)xlsform_outline(xlsform, lang = NULL, ...)
xlsform |
Path to an XLSForm ( |
lang |
Language to use: a code ( |
... |
Further options: |
A data.frame with one row per block and columns row (Excel row of
the form), serial (question number, NA when unnumbered), depth
(number of enclosing groups), block ("section", "section_end",
"note" or "question"), kind (the question kind, for example
"select_one", or "begin_group", "begin_repeat", "end_repeat",
"note"), name, label, required, n_choices (all choices of a
select or rank question, not counting an "other" row), relevant_text,
constraint_text (the constraint message when the form has one, else the
humanized expression) and layout (for select and rank questions:
"list", "columns" or "code_box", from the number of choices and
max_choices; NA otherwise). A label may contain line breaks (\n).
path <- system.file("extdata", "minimal", "minimal.xlsx", package = "convertxlsform") out <- xlsform_outline(path) out[, c("serial", "block", "kind", "name", "relevant_text")] # A multilingual form: another language, with flat question numbers path_ml <- system.file("extdata", "multilang", "multilang.xlsx", package = "convertxlsform") out <- xlsform_outline(path_ml, lang = "bn", nested_serial_numbers = FALSE) head(out[, c("serial", "name")], 5)path <- system.file("extdata", "minimal", "minimal.xlsx", package = "convertxlsform") out <- xlsform_outline(path) out[, c("serial", "block", "kind", "name", "relevant_text")] # A multilingual form: another language, with flat question numbers path_ml <- system.file("extdata", "multilang", "multilang.xlsx", package = "convertxlsform") out <- xlsform_outline(path_ml, lang = "bn", nested_serial_numbers = FALSE) head(out[, c("serial", "name")], 5)