Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用pdftools将批量pdf转换为文本_R_Pdf_Batch Processing - Fatal编程技术网

R 使用pdftools将批量pdf转换为文本

R 使用pdftools将批量pdf转换为文本,r,pdf,batch-processing,R,Pdf,Batch Processing,我打算把1000个PDF转换成文本进行数据分析。我正在使用pdftools软件包 我已经能够使用以下代码转换2 pdf: 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) fil

我打算把1000个PDF转换成文本进行数据分析。我正在使用pdftools软件包

我已经能够使用以下代码转换2 pdf:

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()

}
另外,我希望最后的文本文件是“file_name.txt”,现在我得到的是“file_name.pdf.txt”

谢谢,

库(pdftools)
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
  }

})
图书馆(purrr) setwd(“/tmp/test”) 文件_list%#制作一个带有换行符的大文本块 cat(file=txt#u file)#写出来 }否则{#如果不成功 消息(sprintf(“转换[%s],.x]失败”)#显示消息 } })
Thanks@hrbrmstr! 然而,我只能转换1000个pdf中的20个。我尝试过使用另一个代码(见下文),但是使用了txt。我得到的文件正在破坏字符(我所有的文件都是西班牙语的,所以我有多个特殊字符´´ñ,í,ó,ú”,我需要所有字符都是小写)pdf_文件0{for(我在pdf_文件中){system(粘贴(粘贴(''”,getwd(),'/xpdf/bin64/pdftotext.exe',sep=''),paste0(“”,i,“”)),wait=FALSE)}cat(“\n转换为文本完成。\n\n”)不幸的是,人们实际上不可能为您编写所有代码。
stringi
包具有
stri_trans_tolower()
,这有助于从不同字符集进行翻译。
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
  }

})