我想使用pdf_text自动将PDF文档读入R

时间:2019-06-20 02:10:21

标签: r pdf download

我目前有一个代码可以提取PDF文档中的某些详细信息。但是,由于我还有成千上万个其他PDF文档可从中提取信息,因此我想使这一过程自动化。我正在使用pdf_text选项将PDF读入R。我的代码如下所示:

library(pdftools)

x <- pdf_text("Test.pdf")   
y1 <- str_split(x, "\r")

#pdf output contains a total of 7 lists

a <- y1 [[4]]
b <- c(a[4],a[11:13]) #Obtain only rows 4, 11 to 13 from list 4

n2 <- y1[[3]]
n3 <- c(n2[3]) #Obtain only rows 3 from list 3

n <- y1[[5]]
n1 <- c(n[3]) #Obtain only rows 3 from list 5

c <- y1[[6]]
d <- c(c[4:18]) #Obtain only rows 4 to 18 from list 6

e <- c(n3,b,d,n1) #Combining all necessary information into one list

z <- substr(s[1:21], start = 15, stop = 200) #to remove white spaces between quotes

Name <- z[1]
InterestedParty <- z[2]
TotalOwnBefore <- substr(z[11], start = 97, stop = 120)
Ownership <- list(NM = Name, Party = InterestedParty, OwnBefore = TotalOwnBefore)

write.csv(Ownership, file="MyData.csv")

上面的代码允许我输出单个公司的文件。但是,我还有成千上万个PDF(“ Test_1.pdf”到“ Test_1000.pdf”)需要阅读。有没有办法用pdf_text自动将PDF文件读入R?如果我有办法将所有结果存储到一个文件中,而不是每个文件存储一个公司,那也很好。

1 个答案:

答案 0 :(得分:0)

此后,我设法使用如下所示的for循环使过程自动化:

for (i in 1:1000){
    x <- paste("Test_",i,".pdf", sep="")
    y <- pdf_text(print(x))
    total <- strsplit(y, "\r")
    print(y1)
}