将.pdf转换为.txt

Stackoverflow上的问题
is not new,但我很确定我错过了一些明显的东西.

我正在尝试将一些.pdf文件转换为.txt文件,以便挖掘它们的文本.我的方法基于这个excellent script.pdf文件中的文本不是由图像组成的,因此不需要OCR.

# Load tm package
library(tm)

# The folder containing my PDFs
dest <- "./pdfs"

# Correctly installed xpdf from http://www.foolabs.com/xpdf/download.html

file.exists(Sys.which(c("pdfinfo", "pdftotext")))
[1] TRUE TRUE

# Delete white spaces from pdfs' names
sapply(myfiles, FUN = function(i){
  file.rename(from = i, to =  paste0(dirname(i), "/", gsub(" ", "", basename(i))))
})

# make a vector of PDF file names
myfiles <- list.files(path = dest, pattern = "pdf",  full.names = TRUE)

lapply(myfiles, function(i) system(paste('"C:/Program Files/xpdf/bin64/pdftotext.exe"', 
paste0('"', i, '"')), wait = FALSE)) 

它应该在dest文件夹中创建任何.pdf文件的.txt副本.我检查了路径white spaces path的问题,但是没有任何反应.

这是我正在研发的repository.如果它有用,我可以粘贴SessionInfo.提前致谢.

最佳答案 迟到的答案:

但是我最近发现,如果你安装了pdftools(install.packages(“pdftools”)),你可以使用当前版本的tm(0.7-4)将pdfs直接读入语料库.

library(tm)

directory <- getwd() # change this to directory where pdf-files are located

# read the pdfs with readPDF, default engine used is pdftools see ?readPDF for more info
my_corpus <- VCorpus(DirSource(directory, pattern = ".pdf"), 
                               readerControl = list(reader = readPDF))
点赞