我要把1000个pdf文件转换成文本进行数据分析。我正在使用包pdftools。
我已经能够使用以下代码转换2pdf:
library(pdftools)
file_list <- list.files('pdf', full.names = TRUE, pattern = 'pdf')
for(i in 1:length(file_list)){
temp <- pdf_text(file_list[i])
temp <- tolower(temp)
file_name = paste(file_list[i], '.txt')
sink(file_name)
cat(temp)
sink()
}但是当我添加超过2个时,我得到以下错误:
" Error in poppler_pdf_text(loadfile(pdf), opw, upw) : PDF parsing failure." 另外,我希望最终的文本文件只有"file_name.txt“,现在我得到的是"file_name.pdf .txt”
谢谢,
发布于 2017-10-01 06:29:15
library(pdftools)
library(purrr)
setwd("/tmp/test")
file_list <- list.files(".", full.names = TRUE, pattern = '.pdf$')
s_pdf_text <- safely(pdf_text) # helps catch errors
walk(file_list, ~{ # iterate over the files
res <- s_pdf_text(.x) # try to read it in
if (!is.null(res$result)) { # if successful
message(sprintf("Processing [%s]", .x))
txt_file <- sprintf("%stxt", sub("pdf$", "", .x)) # make a new filename
unlist(res$result) %>% # cld be > 1 pg (which makes a list)
tolower() %>%
paste0(collapse="\n") %>% # make one big text block with line breaks
cat(file=txt_file) # write it out
} else { # if not successful
message(sprintf("Failure converting [%s]", .x)) # show a message
}
})https://stackoverflow.com/questions/46507410
复制相似问题