| Title: | Convert 'HTML' Tables to 'Excel' Files |
|---|---|
| Description: | Reads tables from 'HTML' web pages or local documents. The tables are returned as a list of 'tibbles' and may be written to 'Excel' files. |
| Authors: | Paul Northrop [aut, cre, cph] |
| Maintainer: | Paul Northrop <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 1.0.1 |
| Built: | 2026-07-22 16:12:59 UTC |
| Source: | https://github.com/paulnorthrop/html2excel |
Reads tables from HTML web pages or local documents. The tables are returned as a list of tibbles and may be written to Excel files.
The main function is html2excel.
See also summary.html2excel.
Maintainer: Paul Northrop [email protected] [copyright holder]
Useful links:
Report bugs at https://github.com/paulnorthrop/html2excel/issues
Reads tables from HTML documents using rvest::read_html and
rvest::html_table. The tables are returned as tibbles and may be written
to Excel .xlsx files using openxlsx::write.xlsx.
html2excel( html, ext = "*.html", write = TRUE, dir, sheets, read_args = list(), html_args = list(), write_args = list() )html2excel( html, ext = "*.html", write = TRUE, dir, sheets, read_args = list(), html_args = list(), write_args = list() )
html |
A character vector containing one of the following:
|
ext |
A character scalar. If |
write |
A logical scalar. Should the tibbles be written to Excel
|
dir |
A path to a directory. If
If |
sheets |
A numeric vector or list of numeric vectors. Component |
read_args |
A list of arguments for |
html_args |
A list of arguments for |
write_args |
A list of arguments for |
HTML files are
read using rvest::read_html,
converted to tables (tibbles) using rvest::html_table,
written to Excel documents using openxlsx::write.xlsx.
If rvest::read_html fails to connect to a URL in html then a message
is printed and html is returned.
The output filename for an input HTML file file.html is usually
file.xlsx. Exceptions are when a combination of html and ext means
that duplicate filenames would be produced, for example, if files
file.html and file.mhtml are read. Then the output filenames are
distinguished by file_a.xlsx and file_b.xlsx, and similarly if there are
more than two identical filenames.
The initial motivation for creating the html2excel package was to convert
to Excel format HTML files that has mistakenly been given a .xlsx file
extension. If such files are supplied by html then each output Excel
file will be written to a directory output created in the directory of the
respective input file.
An object of class c("html2excel", "list"). A list of (lists of)
tibbles created from objects returned from rvest::html_table.
The names of the list are the input HTML filenames. Each list component
is named sheet1, sheet2 etc. with each sheet containing a separate
table found in the input HTML file. The numbering of these sheets is the
same as the output .xlsx files, not the order of the tables in the
input HTML files.
If write = TRUE then a character vector containing the file paths to the
.xlsx files created is added as an attributed named files.
### HTML files ## .html and .mhtml versions of the URL example below html <- system.file("extdata", "tables.html", package = "html2excel") mhtml <- system.file("extdata", "tables.mhtml", package = "html2excel") ## .html # Table 5 only t1 <- html2excel(html, sheets = 5, write = FALSE) # The table t1[[1]] # The dimensions of the table summary(t1) ## .mhtml # Pass UTF-8 encoding to avoid error "Unsupported encoding: 3DUTF-8" t2 <- html2excel(mhtml, read_args = list(encoding = "UTF-8"), write = FALSE) # The same table as above t2[[1]][5] # The dimensions of all tables summary(t2) ### A directory ## Contains the .html and .mhtml files above directory <- system.file("extdata", package = "html2excel") # Change the ext argument to include the .mhtml file # Extract tables 3 and 5 from .html and table 5 from .mhtml x <- html2excel(directory, ext = "*.*html", sheets = list(c(3, 5), 5), read_args = list(encoding = "UTF-8"), write = FALSE) ### A URL url <- "https://afd.calpoly.edu/web/sample-tables" x <- html2excel(url, write = FALSE)### HTML files ## .html and .mhtml versions of the URL example below html <- system.file("extdata", "tables.html", package = "html2excel") mhtml <- system.file("extdata", "tables.mhtml", package = "html2excel") ## .html # Table 5 only t1 <- html2excel(html, sheets = 5, write = FALSE) # The table t1[[1]] # The dimensions of the table summary(t1) ## .mhtml # Pass UTF-8 encoding to avoid error "Unsupported encoding: 3DUTF-8" t2 <- html2excel(mhtml, read_args = list(encoding = "UTF-8"), write = FALSE) # The same table as above t2[[1]][5] # The dimensions of all tables summary(t2) ### A directory ## Contains the .html and .mhtml files above directory <- system.file("extdata", package = "html2excel") # Change the ext argument to include the .mhtml file # Extract tables 3 and 5 from .html and table 5 from .mhtml x <- html2excel(directory, ext = "*.*html", sheets = list(c(3, 5), 5), read_args = list(encoding = "UTF-8"), write = FALSE) ### A URL url <- "https://afd.calpoly.edu/web/sample-tables" x <- html2excel(url, write = FALSE)
html2excel objectssummary method for class html2excel.
## S3 method for class 'html2excel' summary(object, ...) ## S3 method for class 'summary.html2excel' print(x, ...)## S3 method for class 'html2excel' summary(object, ...) ## S3 method for class 'summary.html2excel' print(x, ...)
object |
An object inheriting from class |
... |
Further arguments to be passed to |
x |
An object returned by |
The default print method for a list of tibbles prints the
dimensions of the tibbles and up to 10 rows and 6 columns of each tibble.
summary.html2excel prints a shorter summary that contains only the
dimensions of the tibbles. These print and/or summary methods may be
helpful in deciding which sheets are required for which excel file, that
is, how to set the argument sheets to html2excel.
A list containing the dimensions (number of rows and number of columns) of each tibble.
See html2excel.