Title: | Extract Text from RTF File |
---|---|
Description: | Extracts plain text from RTF (Rich Text Format) file. |
Authors: | Kota Mori [aut, cre] |
Maintainer: | Kota Mori <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.6.0 |
Built: | 2024-11-07 03:26:00 UTC |
Source: | https://github.com/kota7/striprtf |
Validate if a file looks like an RTF. The test should be seen as a minimal requirement; If failed, the file is highly likely that the file is invalid, while passed, there is still possibility that the file does not follw the rule of RTF files.
looks_rtf(con, n = 1000)
looks_rtf(con, n = 1000)
con |
A connection object or string of file name |
n |
Integer that specifies the length of contents to be tested. If smaller than 10, forced to 10. |
Logical.
Parses an RTF file and extracts plain text as character vector.
read_rtf( file, verbose = FALSE, row_start = "*| ", row_end = "", cell_end = " | ", ignore_tables = FALSE, check_file = TRUE, ... ) strip_rtf( text, verbose = FALSE, row_start = "*| ", row_end = "", cell_end = " | ", ignore_tables = FALSE )
read_rtf( file, verbose = FALSE, row_start = "*| ", row_end = "", cell_end = " | ", ignore_tables = FALSE, check_file = TRUE, ... ) strip_rtf( text, verbose = FALSE, row_start = "*| ", row_end = "", cell_end = " | ", ignore_tables = FALSE )
file |
Path to an RTF file. Must be character of length 1. |
verbose |
Logical. If TRUE, progress report is printed on console. While it can be informative when parsing a large file, this option itself makes the process slow. |
row_start , row_end
|
strings to be added at the beginning and end of table rows |
cell_end |
string to be put at the end of table cells |
ignore_tables |
if |
check_file |
if |
... |
Addional arguments passed to |
text |
Character of length 1. Expected to be contents of an RTF file. |
Rich text format (RTF) files are written as a text file consisting of ASCII characters. The specification has been developed by Microsoft. This function interprets the character strings and extracts plain texts of the file. Major part of the algorithm of this function comes from a stack overflow thread (https://stackoverflow.com/a/188877) and the references therein. This function is a translation of the above to R language, associated with C++ codes for enhancement.
An advance from the preceding implementation is that the function
accomodates with various ANSI code pages. For example, RTF files created
by Japanese version of Microsoft Word marks \ansicpg932
, which indicates
the code page 932 is used for letter-code conversion.
The function detects the code page indication and
convert the characters to UTF-8 where possible. Conversion tables are retrieved from
here: (https://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/).
Character vector of extracted text
Original discussion thread: https://stackoverflow.com/a/188877
Code page table: https://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/
read_rtf(system.file("extdata/king.rtf", package = "striprtf"))
read_rtf(system.file("extdata/king.rtf", package = "striprtf"))
From ver 0.3.1, the functions are renamed as follows:
striprtf(file, verbose = FALSE, ...) rtf2text(text, verbose = FALSE)
striprtf(file, verbose = FALSE, ...) rtf2text(text, verbose = FALSE)
file |
Path to an RTF file. Must be character of length 1. |
verbose |
Logical. If TRUE, progress report is printed on console. While it can be informative when parsing a large file, this option itself makes the process slow. |
... |
Addional arguments passed to |
text |
Character of length 1. Expected to be contents of an RTF file. |
Character vector of extracted text
Returns letters not used in strings
unused_letters( s, n = 1, avoid_strifrtf_internal = TRUE, as_number = FALSE, as_vector = FALSE )
unused_letters( s, n = 1, avoid_strifrtf_internal = TRUE, as_number = FALSE, as_vector = FALSE )
s |
character vector |
n |
number of letters to return |
avoid_strifrtf_internal |
If |
as_number |
if |
as_vector |
if |
This function can be useful when some special characters must be temporarily converted to another letter without being confused with the same letters used elsewhere.
Letters are first searched from \u0001
upto \uffff
.
Do not specify too large n
; An error is raised if a sufficient number of unsed letters are not found.
unsed characters, format depends on as_number
and as_vector
arguments