Skip to content

Instantly share code, notes, and snippets.

@smithjd
Created March 2, 2022 16:12
Show Gist options
  • Select an option

  • Save smithjd/5fec0629f26a362c8895583daae7edb4 to your computer and use it in GitHub Desktop.

Select an option

Save smithjd/5fec0629f26a362c8895583daae7edb4 to your computer and use it in GitHub Desktop.

Most bank data is readily available for download, but some credit card companies only offer a monthly PDF file with transactions. R makes it easy to pull out the data.

@smithjd

smithjd commented Mar 2, 2022

Copy link
Copy Markdown
Author

Here are the libraries I use:

library(pdftools) 
library(tidyverse) 
library(here) 
library(lubridate) 
library(googlesheets4) 

This function retrieves all the data from one month and returns a decent data frame. Visual inspection shows that the first 3 pages of the PDF can be skipped.


month_pages <- pdf_info(pdf_name)$pages
month_df <- pdf_text(pdf_name) %>% str_split("\n")
temp <- month_df[3:month_pages]
df <- tibble(line = flatten_chr(temp))

month_data <- df %>% 
  filter(str_detect(line, '^  *\\d\\d\\/\\d\\d ')) %>% 
  mutate(date = mdy( paste0(str_trim(str_sub(line, 3, 10)), "/2021")),
         descrip = str_trim(str_sub(line, 11, -10)),
         amount = parse_number(str_sub(line, -10, -1))) %>% 
  select(-line)
 month_data
}

Provide a list of PDF files downloaded from the credit card company:

pdf_list <- c("20210115-statements-6488-.pdf", "20210215-statements-6488-.pdf", ...)
Then process all the files with a call as follows:

cc_transactions_2021 <-  map_df(pdf_list, get_transactions)

Then upload to a Google Sheet or wherever you want to inspect and process your data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment