将 .pdf 转换为 .txt
Convert .pdf to .txt
Whosebug 上的问题 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 副本。我检查了 path, for white spaces in the path, for xpdf common installation issues 的问题,但没有任何反应。
这是我正在处理的 repository。如果有用的话,我可以贴SessionInfo
。提前致谢。
迟到的答案:
但我最近发现,如果安装了 pdftools (install.packages("pdftools")
),使用当前版本的 tm (0.7-4) 可以将 pdf 直接读入语料库。
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))
Whosebug 上的问题 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 副本。我检查了 path, for white spaces in the path, for xpdf common installation issues 的问题,但没有任何反应。
这是我正在处理的 repository。如果有用的话,我可以贴SessionInfo
。提前致谢。
迟到的答案:
但我最近发现,如果安装了 pdftools (install.packages("pdftools")
),使用当前版本的 tm (0.7-4) 可以将 pdf 直接读入语料库。
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))