knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" )
Extract Data Tables and Comments from 'Microsoft' 'Word' Documents
An R package for extracting tables & comments out of Word documents (docx). Development versions are available here and production versions are on CRAN.
Microsoft Word docx files provide an XML structure that is fairly straightforward to navigate, especially when it applies to Word tables. The docxtractr package provides tools to determine table count, table structure and extract tables from Microsoft Word docx documents.
Many tables in Word documents are in twisted formats where there may be labels or other oddities mixed in that make it difficult to work with the underlying data. docxtractr
provides a function—assign_colnames
—that makes it easy to identify a particular row in a scraped (or any, really) data.frame
as the one containing column names and have it become the column names, removing it and (optionally) all of the rows before it (since that's usually what needs to be done).
The following functions are implemented:
read_docx
: Read in a Word document for table extractiondocx_describe_tbls
: Returns a description of all the tables in the Word documentdocx_describe_cmnts
: Returns a description of all the comments in the Word documentdocx_extract_tbl
: Extract a table from a Word documentdocx_extract_cmnts
: Extract comments from a Word documentdocx_extract_all_tbls
: Extract all tables from a Word document (docx_extract_all
is now deprecated)docx_tbl_count
: Get number of tables in a Word documentdocx_cmnt_count
: Get number of comments in a Word documentassign_colnames
: Make a specific row the column names for the specified data.framemcga
: Make column names great againset_libreoffice_path
: Point to Local soffice.exe FileThe following data file are included:
system.file("examples/data.docx", package="docxtractr")
: Word docx with 1 tablesystem.file("examples/data3.docx", package="docxtractr")
: Word docx with 3 tablessystem.file("examples/none.docx", package="docxtractr")
: Word docx with 0 tablessystem.file("examples/complex.docx", package="docxtractr")
: Word docx with non-uniform tablessystem.file("examples/comments.docx", package="docxtractr")
: Word docx with commentssystem.file("examples/realworld.docx", package="docxtractr")
: A "real world" Word docx file with tables of all shapes and sizessystem.file("examples/trackchanges.docx", package="docxtractr")
: Word docx with track changes in a table# devtools::install_github("hrbrmstr/docxtractr") # OR install.packages("docxtractr")
options(width=120)
library(docxtractr) library(tibble) library(dplyr) # current version packageVersion("docxtractr")
# one table doc <- read_docx(system.file("examples/data.docx", package="docxtractr")) docx_tbl_count(doc) docx_describe_tbls(doc) docx_extract_tbl(doc, 1) docx_extract_tbl(doc) docx_extract_tbl(doc, header=FALSE) # url budget <- read_docx("http://rud.is/dl/1.DOCX") docx_tbl_count(budget) docx_describe_tbls(budget) docx_extract_tbl(budget, 1) docx_extract_tbl(budget, 2) # three tables doc3 <- read_docx(system.file("examples/data3.docx", package="docxtractr")) docx_tbl_count(doc3) docx_describe_tbls(doc3) docx_extract_tbl(doc3, 3) # no tables none <- read_docx(system.file("examples/none.docx", package="docxtractr")) docx_tbl_count(none) # wrapping in try since it will return an error # use docx_tbl_count before trying to extract in scripts/production try(docx_describe_tbls(none)) try(docx_extract_tbl(none, 2)) # 5 tables, with two in sketchy formats complx <- read_docx(system.file("examples/complex.docx", package="docxtractr")) docx_tbl_count(complx) docx_describe_tbls(complx) docx_extract_tbl(complx, 3, header=TRUE) docx_extract_tbl(complx, 4, header=TRUE) docx_extract_tbl(complx, 5, header=TRUE) # a "real" Word doc real_world <- read_docx(system.file("examples/realworld.docx", package="docxtractr")) docx_tbl_count(real_world) # get all the tables tbls <- docx_extract_all_tbls(real_world) # see table 1 tbls[[1]] # make table 1 better assign_colnames(tbls[[1]], 2) # make table 1's column names great again mcga(assign_colnames(tbls[[1]], 2)) # see table 5 tbls[[5]] # make table 5 better assign_colnames(tbls[[5]], 2) # preserve lines intracell_whitespace <- read_docx(system.file("examples/preserve.docx", package="docxtractr")) docx_extract_all_tbls(intracell_whitespace, preserve=TRUE) docx_extract_all_tbls(intracell_whitespace) # comments cmnts <- read_docx(system.file("examples/comments.docx", package="docxtractr")) print(cmnts) glimpse(docx_extract_all_cmnts(cmnts))
pandoc
being available)# original read_docx( system.file("examples/trackchanges.docx", package="docxtractr") ) %>% docx_extract_all_tbls(guess_header = FALSE) # accept read_docx( system.file("examples/trackchanges.docx", package="docxtractr"), track_changes = "accept" ) %>% docx_extract_all_tbls(guess_header = FALSE) # reject read_docx( system.file("examples/trackchanges.docx", package="docxtractr"), track_changes = "reject" ) %>% docx_extract_all_tbls(guess_header = FALSE)
library(docxtractr) library(testthat) date() test_dir("tests/")
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.