将扫描的PDF文件转换为可搜索的PDF文件(使用R语言)

3

我正在尝试使用tesseractpdftools软件包将一系列扫描的PDF转换为可搜索的PDF。我已经完成了两个步骤,现在需要将其写回到可搜索的PDF中。

  1. 读取扫描的PDF
  2. 运行OCR
  3. 将其写回到可搜索的PDF中
eg <- download.file("https://www.fujitsu.com/global/Images/sv600_c_automatic.pdf", "example.pdf", mode = "wb")

results <- tesseract::ocr_data("example.pdf", engine = "eng")

R> results
# A tibble: 406 x 3
   word        confidence bbox             
   <chr>            <dbl> <chr>            
 1 PFU               96.9 228,181,404,249  
 2 Business          96.2 459,180,847,249  
 3 report            96.2 895,182,1145,259 
 4 |                 52.5 3980,215,3984,222
 5 No.068            91.0 4439,163,4754,237
 6 New               96.0 493,503,1005,687 
 7 customer's        94.6 1069,484,2231,683
 8 development       96.5 2304,483,3714,732
 9 di                90.4 767,763,1009,959 
10 ing               96.3 1754,773,1786,807
# ... with 396 more rows

另外,我是否可以在Windows的R中调用其他软件包或命令行工具来替代?


似乎tesseract的命令行版本可以在图像C:\Users\tspeidel\AppData\Local\Programs\Tesseract-OCR\tesseract.exe example_1.png out -l eng PDF上执行此操作。但我还不确定如何使用它。 - Thomas Speidel
你可以使用 R 的 system 函数调用该代码。 - IRTFM
1
几天前有一个类似的问题在这里,我认为还没有实现PDF输出。我认为系统调用可能是最简单的方法,但您也可以使用rmarkdown::render,例如results <- tesseract::ocr("example.pdf", engine = "eng") ; cat(results, file=temp<-tempfile()) ; rmarkdown::render(temp, "pdf_document", "~/test.pdf") - user20650
请参见相关的 Github 问题:https://github.com/ropensci/tesseract/issues/51 - Bryan Shalloway
3个回答

2

我有类似的需求,并编写了一个简单的R函数来调用OCRmyPDF的命令行。

我正在使用Ubuntu,因此首先通过以下方式在Ubuntu中安装OCRmyPDF:

sudo apt install ocrmypdf

这里是在其他操作系统上安装它的信息

然后通过运行以下命令在R中加载R函数:

    ocr_my_pdf <- function(path_read, ..., path_save = NULL){
      
      path_read <- here::here(path_read)
      if(is.null(path_save)){ 
        path_save <- stringr::str_replace(path_read, '(?i)\\.pdf$','_ocr.pdf') 
      } else {
        path_save <- here::here(path_save)
      }
      
      sys_args <- c(
        glue::glue("'{unlist(list(...))}'"), 
        glue::glue("'{path_read}'"), 
        glue::glue("'{path_save}'"))
      system2('ocrmypdf', args = sys_args) 
      
    }

然后在测试PDF上调用该函数:

    ocr_my_pdf('/home/test.pdf')

或者,您可以传递任何其他参数:

    ocr_my_pdf('test.pdf', '--deskew', '--clean', '--rotate-pages')

这里是可用参数的信息。


1

这里有一种基于RDCOMClient R软件包的方法。基本上,我们将PDF转换为Word。在此过程中,Word使用嵌入式OCR。之后,我们使用Word软件将Word文档转换为可搜索的PDF。

library(RDCOMClient)

download.file("https://www.fujitsu.com/global/Images/sv600_c_automatic.pdf", "example.pdf", mode = "wb")

path_PDF <- "C:/example.pdf"
path_Word <- "C:/example.docx"

################################################################
#### Step 1 : Convert PDF to word document with OCR of Word ####
################################################################
wordApp <- COMCreate("Word.Application")
wordApp[["Visible"]] <- TRUE
wordApp[["DisplayAlerts"]] <- FALSE

doc <- wordApp[["Documents"]]$Open(normalizePath(path_PDF),
                                   ConfirmConversions = FALSE)

doc$SaveAs2(path_Word)
doc_Selection <- wordApp$Selection()

##########################################################
#### Step 3 : Convert word document to searchable pdf ####
##########################################################
path_PDF_Searchable <- "C:/example_searchable.pdf"
wordApp[["ActiveDocument"]]$SaveAs(path_PDF_Searchable, FileFormat = 17) # FileFormat = 17 saves as .PDF
doc$Close()
wordApp$Quit() # quit wordApp

1
如果您的计算机上安装了ECopy软件(非免费软件),您可以使用以下功能将扫描的PDF文件转换为可搜索的PDF文件:
ecopy_Scanned_PDF_To_Numeric_PDF <- function(directory_Scanned_PDF, directory_Numeric_PDF)
{
  path_To_BatchConverter <- "C:/Program Files (x86)/Nuance/eCopy PDF Pro Office 6/BatchConverter.com"
  args <- paste0("-I", directory_Scanned_PDF, "\\*.pdf -O", directory_Numeric_PDF, " -Tpdfs -Lfre -W -V1.5 -J -Ao")
  system2(path_To_BatchConverter, args = args)
}

我在工作中使用这个函数,它非常有效


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接