我在一个文件夹 ("C:/Users/Documents/files_i_want"
) 中有许多 pdf 文件(这些文件是“扫描的”)。 PDF 的文件如下所示:
https://jeroen.github.io/images/ocrscan.pdf
所有的pdf文件都有不同的名字。我正在尝试使用以下命令将它们全部导入 R:pdftools::pdf_convert
library(pdftools)
library(tesseract)
#Get the path of filenames
filenames <- list.files("C:/Users/Documents/files_i_want", full.names = TRUE)
#Read them in a list
list_data <- lapply(filenames, pdftools::pdf_convert)
#Name them as per your choice (df_1, df_2 etc)
names(list_data) <- paste('df', seq_along(filenames), sep = '_')
#Create objects in global environment.
list2env(list_data, .GlobalEnv)
这将返回以下错误:
Error in names(list_data) <- paste("df", seq_along(filenames), sep = "_") :
'names' attribute [1] must be the same length as the vector [0]
有谁知道为什么会产生这个错误?
谢谢
更新
我想出了如何从文件夹上传所有 pdf:
library(pdftools)
library(tesseract)
directory <- "C:/Users/OneDrive/Documents/files_i_want"
file.list <- paste(directory, "/",list.files(directory, pattern = "*.pdf"), sep = "")
b = lapply(file.list, FUN = function(files) {
pdf_convert(files, format = "jpeg")
})
a = data.frame(file.list)
现在,我必须弄清楚如何在对象“a”中的每个“条目”上应用以下函数,例如“i”代表对象“a”中的每个“条目”(目标是创建“text_1”和“text_2”,例如text_1 <- tesseract::ocr(a[1, "file.list"]) )>
convert_function <- function(i){
text_i <- tesseract::ocr(i)
}
答案 0 :(得分:1)
你可以试试这个代码 -
data <- lapply(sub('pdf$', 'jpeg', file.list), tesseract::ocr)
names(data) <- paste0('text', seq_along(data))
list2env(data, .GlobalEnv)