Like @Justin Coco hinted, this was a lot of fun. The code ended up a bit more complex than I anticipated, but I think the result should be what you imagined.
I used pdf_data
instead of pdf_text
so I can work with the position of words.
library(pdftools)
#> Using poppler version 0.86.1
library(tidyverse)
pdf_location <- "/location/of/pdf"
pdf_raw <- pdf_data(pdf_location)
I then wrote a function which can process a page from the PDF:
get_table <- function(x, page) {
x[[page]] %>% # select page, I use this variable again below, which is why I'm not simply looping through the whole object
filter(y > 25, y < 833) %>% # above and below these positions is the pdf header which we are not interested in
mutate(column = case_when( # I check the x-positions where the columns start an end and transformed them into column numbers
x >= 36 & x < 220 ~ 1L,
x >= 220 & x < 403 ~ 2L,
x >= 403 ~ 3L,
)) %>%
mutate(newrow = case_when( # check if this is a new line
column == 1L & x == 36 ~ TRUE,
column == 2L & x == 220 ~ TRUE,
column == 3L & x == 403 ~ TRUE,
TRUE ~ FALSE
),
row = cumsum(newrow), # get the row number
subsidiary = newrow & text == ".") %>% # as you say, subsidiary names start with "."
group_by(row, column) %>% # grouping and summarising moves the text into one 'cell'
summarise(text = paste(text, collapse = " "),
subsidiary = sum(subsidiary) > 0,
.groups = "drop") %>%
mutate(headline = !str_detect(text, "\\s")) %>% # the category headlines (@, A, B, C, etc.) are still in there but can be identified easily since they lack whitespace
mutate(row = ifelse(row > 1 & !subsidiary & !lag(subsidiary) & !lag(headline), lag(row), row),
row = ifelse(row > 1 & !subsidiary & !lag(subsidiary) & !lag(headline), lag(row), row)) %>% # some company names stretch over up to three lines but lines are not indented
group_by(row, column) %>%
summarise(text = paste(text, collapse = " "),
subsidiary = sum(subsidiary) > 0,
headline = head(headline, 1),
.groups = "drop") %>%
mutate(page = page, .before = row) # finally add the page number to keep track
}
You can test this on one page or loop through all of them at once:
pdf_df <- map_df(19:1428, ~get_table(pdf_raw, page = .x))
I already like the df, but you requested that the table should be "showing company names and their subsidiaries". So let's do some more wrangling on the pdf_df
object.
pdf_df %>%
filter(!headline) %>%
mutate(company_nr = cumsum(!subsidiary)) %>%
group_by(company_nr) %>%
mutate(company = text[!subsidiary & !headline]) %>%
filter(subsidiary) %>%
select(company_nr, company, subsidiary = text)
#> # A tibble: 303,380 x 3
#> # Groups: company_nr [115,477]
#> company_nr company subsidiary
#> <int> <chr> <chr>
#> 1 1 ?WHAT IF! HOLDINGS LIMITED The Gla… . ?What If! China Holdings Li…
#> 2 1 ?WHAT IF! HOLDINGS LIMITED The Gla… . . ?What If! Innovation Sing…
#> 3 1 ?WHAT IF! HOLDINGS LIMITED The Gla… . ?What If! Joint Ventures Li…
#> 4 1 ?WHAT IF! HOLDINGS LIMITED The Gla… . ?What If! Limited England
#> 5 1 ?WHAT IF! HOLDINGS LIMITED The Gla… . . ? What If ! Inventors Lim…
#> 6 1 ?WHAT IF! HOLDINGS LIMITED The Gla… . . ? What If ! Training Limi…
#> 7 1 ?WHAT IF! HOLDINGS LIMITED The Gla… . Nobby Styles Limited Englan…
#> 8 2 @A COMPANY LIMITED Premier Suite 4… . Aviva Holdings Limited Engl…
#> 9 2 @A COMPANY LIMITED Premier Suite 4… . Copper Mountain Networks Li…
#> 10 2 @A COMPANY LIMITED Premier Suite 4… . Just Ties Limited England
#> # … with 303,370 more rows
Created on 2021-05-23 by the reprex package (v2.0.0)
Let me know in a comment if there are problems. I obviously didn't go through all pages to check if the script has some quirks with specific company names etc. but the first pages look fine to me.