【问题标题】:Convert .pdf to .txt将 .pdf 转换为 .txt
【发布时间】:2016-10-18 10:51:28
【问题描述】:

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 副本。我检查了路径中的pathwhite spacesxpdf common installation issues 是否存在问题,但没有任何反应。

这是我正在处理的repository。如果有用,我可以粘贴SessionInfo。提前致谢。

【问题讨论】:

  • 在命令行输入命令,程序能工作吗?
  • 抱歉,回答迟了。我刚试过,但什么也没发生。
  • 可以使用 pdftools R 包作为替代方案吗?
  • @PeterEllis 谢谢你的建议,下次我一定会试试的。

标签: r pdf tm


【解决方案1】:

迟到的答案:

但我最近发现,如果您安装了 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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 2017-09-13
    • 2020-06-11
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多