【问题标题】:Converting Multiple PDF files into Text (R Language)将多个 PDF 文件转换为文本(R 语言)
【发布时间】:2021-07-31 04:26:58
【问题描述】:

我正在使用 R 中的“tesseract”库将“PDF 文件转换为文本”,如下所示:https://cran.r-project.org/web/packages/tesseract/vignettes/intro.html

library(pdftools)
library(tesseract)

pngfile <- pdftools::pdf_convert('myfile_1.pdf', dpi = 600)
text <- tesseract::ocr(pngfile)
cat(text)

上面的代码完美运行。现在,我正在尝试“大量上传”大量 PDF 文件并将它们转换为文本 - 目前,我想出了如何手动执行此操作

#import and convert 1st file
   pngfile_1 <- pdftools::pdf_convert('myfile_1.pdf', dpi = 600)
    text_1 <- tesseract::ocr(pngfile_1)

#import and convert 2nd file (note: the files do not have the same naming convention)
   pngfile_2 <- pdftools::pdf_convert('second_file.pdf', dpi = 600)
    text_2 <- tesseract::ocr(pngfile_2)

etc

我将上面的代码复制/粘贴了 50 次(同时更改了“索引”,即pngfile_i, text_i)并且能够完成我想做的事情。但是,我正在寻找一种“自动”的方式来导入和转换所有 pdf 文件。

目前,我所有的 pdf 文件都在以下文件夹中:

"C:/Users/me/Documents/mypdfs"

我发现以下代码可用于将 pdf 文件“大量导入”到 R 中:

library(dplyr)
library(data.table)


tbl_fread <- 
    list.files(pattern = "*.pdf") %>% 
    map_df(~fread(.))

但我不确定如何指示此代码从正确的目录 ("C:/Users/me/Documents/mypdfs") 导入所有 pdf。我也不知道如何指示 R 将每个导入的 pdf “重命名”为“pdf_1、pdf_2 等”。

如果所有 pdf 文件都正确导入和创建,我可以编写一个“循环”并执行所需的命令,例如

# "n" would be the total number of pdf files 

for (i in 1:n)
{
pngfile_i <- pdftools::pdf_convert('myfile_i.pdf', dpi = 600)
text_i <- tesseract::ocr(pngfile_i)
}

有人可以告诉我怎么做吗?

谢谢

【问题讨论】:

  • 这个问题有答案了吗?

标签: r pdf dplyr tesseract


【解决方案1】:

您可以在list.files 函数中添加full.names = TRUE,但这假设"C:/Users/me/Documents/mypdfs" 包含在您的project 中。

或者,您可以将path = "Documents/mypdfs 与full.names = TRUE 一起使用,这会将path 定向到mypdfs。

list.files(
        path = "Documents/mypdfs"
        full.names = TRUE,
        pattern = "*.pdf"
        )

要根据您pdf_n 保存它们,那么您可以使用paste 和map。在这里,我使用data.frames 提供了一个示例,因为我不使用 pdf,也没有我愿意处理的批量。

library(tidyverse)

1:length(tbl_fread) %>% map(
        .f = function(i) {
                
                # Your regular function
                # related to PDF
                
                
                # Saving according to desired names
                write.table(
                        tbl_fread[[i]],
                        file = paste0("pdf_", i, ".csv")
                )
                
                
        }
)

为了验证它是否按预期工作,我们可以相应地阅读它,

read.table(
        file = "pdf_1.csv"
)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

【讨论】:

  • 谢谢!我可以问 - 你为什么不使用循环?
  • 实际上,我确实使用了for-loop - 它只是包裹在map 中,而不是for () {} 或lapply。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 2014-02-22
  • 2022-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多