Asset Classes

Free investment financial education

Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
Getting Data from PDFs the Easy Way with R

Getting Data from PDFs the Easy Way with R

Posted August 4, 2023 at 10:00 am
Andrew Treadway
TheAutomatic.net

Originally posted on TheAutomatic.net.

Excerpt

If you don’t have tabulizer installed, just run install.packages(“tabulizer”) to get started.

Initial Setup

After you have tabulizer installed, we’ll load it, and define a variable referencing an example PDF.

library(tabulizer)
 
site <- "http://www.sedl.org/afterschool/toolkits/science/pdf/ast_sci_data_tables_sample.pdf"

The PDFs you manipulate with this package don’t have to be located on your machine — you can use tabulizer to reference a PDF by a URL. For our first example, we’re going to use a sample PDF file found here: http://www.sedl.org/afterschool/toolkits/science/pdf/ast_sci_data_tables_sample.pdf

How to extract all the tables from a PDF

You can extract tables from this PDF using the aptly-named extract_tables function, like this:

# default call with no parameters changed
matrix_results <- extract_tables(site)
 
# get back the tables as data frames, keeping their headers
df_results <- extract_tables(site, output = "data.frame", header = TRUE)

By default, this function will return a matrix for each table, as in the first line of code above. However, as in the second line, we can add parameters to the function to specify the output flag to be data.frame, and set header = TRUE, to get back a list of data frames corresponding to the tables in the PDF.

Once we have the results back, we can refer to any individual PDF table like any data frame we normally would in R.

first_df <- df_results[[1]]
 
first_df$Number.of.Coils

How to scrape text from a PDF

Scraping text from our sample PDF can be done using extract_text:

text <- extract_text(site)
 
# print text
cat(text)

How to split up a PDF by its pages

tabulizer can also create separate files for the pages in a PDF. This can be done using the split_pdf function:

# split PDF referenced above
# output separate page files to current directory
split_pdf(site, getwd())
 
# or output to different directory
split_pdf(site, "C:/path/to/other/folder")

The first argument of split_pdf is the filename or URL of your PDF; the second argument is the directory where you want the individual pages to be output.

How to merge a collection of PDFs

What if we want to reverse what we just did? We can use the merge_pdfs function, which takes as input a vector of file names and and the name of the output file which will be the result of merging the files together.

merge_pdfs("C:/path/to/pdf/files", "C:/path/to/merged_result.pdf")

How to get the number of pages in a PDF

Getting the number of pages in a PDF is made easy with the get_n_pages function, which you can call like this:

get_n_pages(site)

How to get metadata associated with a PDF

You can get metadata associated with our PDF using extract_metadata:

extract_metadata(site)

This function returns a list containing information showing the number of pages, title, created / modified dates, and more.

Join The Conversation

If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.

Leave a Reply

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from TheAutomatic.net and is being posted with its permission. The views expressed in this material are solely those of the author and/or TheAutomatic.net and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.