-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathREADME.Rmd
218 lines (148 loc) · 6.44 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, echo=FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
[](https://travis-ci.org/hrbrmstr/docxtractr)
[](https://ci.appveyor.com/project/hrbrmstr/docxtractr)
[](https://codecov.io/github/hrbrmstr/docxtractr?branch=master)
[](http://cran.r-project.org/package=docxtractr)

# docxtractr
Extract Data Tables and Comments from 'Microsoft' 'Word' Documents
## Description
An R package for extracting tables & comments out of Word documents (docx). Development versions are available here and production versions are [on CRAN](https://cran.rstudio.com/web/packages/docxtractr/index.html).
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).
## What's in the tin?
The following functions are implemented:
- `read_docx`: Read in a Word document for table extraction
- `docx_describe_tbls`: Returns a description of all the tables in the Word document
- `docx_describe_cmnts`: Returns a description of all the comments in the Word document
- `docx_extract_tbl`: Extract a table from a Word document
- `docx_extract_cmnts`: Extract comments from a Word document
- `docx_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 document
- `docx_cmnt_count`: Get number of comments in a Word document
- `assign_colnames`: Make a specific row the column names for the specified data.frame
- `mcga` : Make column names great again
- `set_libreoffice_path`: Point to Local soffice.exe File
The following data file are included:
- `system.file("examples/data.docx", package="docxtractr")`: Word docx with 1 table
- `system.file("examples/data3.docx", package="docxtractr")`: Word docx with 3 tables
- `system.file("examples/none.docx", package="docxtractr")`: Word docx with 0 tables
- `system.file("examples/complex.docx", package="docxtractr")`: Word docx with non-uniform tables
- `system.file("examples/comments.docx", package="docxtractr")`: Word docx with comments
- `system.file("examples/realworld.docx", package="docxtractr")`: A "real world" Word docx file with tables of all shapes and sizes
- `system.file("examples/trackchanges.docx", package="docxtractr")`: Word docx with track changes in a table
## Installation
```{r inst, eval=FALSE}
# devtools::install_github("hrbrmstr/docxtractr")
# OR
install.packages("docxtractr")
```
```{r opts, echo=FALSE}
options(width=120)
```
## Usage
```{r libs, message=FALSE, warning=FALSE}
library(docxtractr)
library(tibble)
library(dplyr)
# current version
packageVersion("docxtractr")
```
```{r sample}
# 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))
```
### Track Changes (depends on `pandoc` being available)
```{r track-changes}
# 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)
```
## Test Results
```{r test}
library(docxtractr)
library(testthat)
date()
test_dir("tests/")
```
### Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md).
By participating in this project you agree to abide by its terms.