【问题标题】:Create searchable (multipage) PDF with Python使用 Python 创建可搜索(多页)PDF
【发布时间】:2021-10-18 09:24:09
【问题描述】:

我在网上找到了一些关于如何在扫描 PDF 时使其可搜索的指南。但是,我目前正在努力弄清楚如何为多页 PDF 执行此操作。

我的代码采用多页 PDF,将每个页面转换为 JPG,在每个页面上运行 OCR,然后将其转换为 PDF。但是,只返回最后一页。

import pytesseract
from pdf2image import convert_from_path

pytesseract.pytesseract.tesseract_cmd = 'directory'
TESSDATA_PREFIX = 'directory'
tessdata_dir_config = '--tessdata-dir directory'

# Path of the pdf
PDF_file = r"pdf directory"
  
  
def pdf_text():
    
    # Store all the pages of the PDF in a variable
    pages = convert_from_path(PDF_file, 500)
  
    image_counter = 1

    for page in pages:

        # Declare file names
        filename = "page_"+str(image_counter)+".jpg"

        # Save the image of the page in system
        page.save(filename, 'JPEG')

        # Increment the counter to update filename
        image_counter = image_counter + 1

    # Variable to get count of total number of pages
    filelimit = image_counter-1

    outfile = "out_text.pdf"

    # Open the file in append mode so that all contents of all images are added to the same file
    
    f = open(outfile, "a")

    # Iterate from 1 to total number of pages
    for i in range(1, filelimit + 1):

        filename = "page_"+str(i)+".jpg"

        # Recognize the text as string in image using pytesseract
        result =  pytesseract.image_to_pdf_or_hocr(filename, lang="eng", config=tessdata_dir_config) 

            
        f = open(outfile, "w+b")
        f.write(bytearray(result))
        f.close()

pdf_text()

如何为所有页面运行此程序并输出一个合并的 PDF?

【问题讨论】:

  • 你为什么使用f = open(outfile, "w+b")?您已经在 for-loop 之前打开它以附加 f = open(outfile, "a") 并且您不应该一次又一次地打开它。你应该在for-loop 之后关闭它,而不是在里面

标签: python pdf ocr


【解决方案1】:

我无法运行它,但我认为所有问题都是因为你在循环中使用了open(..., 'w+b') - 这会删除以前的内容,最后你只写最后一页。

你应该使用已经打开的文件open(outfile, "a")并在循环后关闭它。

# --- before loop ---

f = open(outfile, "ab")

# --- loop ---

for i in range(1, filelimit+1):

    filename = f"page_{i}.jpg"

    result =  pytesseract.image_to_pdf_or_hocr(filename, lang="eng", config=tessdata_dir_config) 

    f.write(bytearray(result))

# --- after loop ---
        
f.close()

顺便说一句:

但还有其他问题 - image_to_pdf_or_hocr 创建完整的 PDF - 带有特殊的页眉和页脚 - 并且附加两个结果无法创建正确的 PDF。您必须使用特殊模块来合并 pdf。赞Merge PDF files

类似的东西

    # --- before loop ---
    
    from PyPDF2 import PdfFileMerger
    import io

    merger = PdfFileMerger()

    # --- loop ---
    
    for i in range(1, filelimit + 1):

        filename = "page_"+str(i)+".jpg"

        result =  pytesseract.image_to_pdf_or_hocr(filename, lang="eng", config=tessdata_dir_config)
        
        pdf_file_in_memory = io.BytesIO(result)        
        merger.append(pdf_file_in_memory)
        
    # --- after loop ---
    
    merger.write(outfile)
    merger.close()

【讨论】:

  • 使用PdfFileMerger 解决了我的问题:-) 谢谢!
【解决方案2】:

这里有许多潜在问题,如果无法调试,很难说根本原因是什么。

JPG 是否已成功创建,并且是预期的单独文件?

我怀疑pages = convert_from_path(PDF_file, 500) 没有按预期返回 - 您是否手动验证它们是按预期创建的?

【讨论】:

  • 是的,JPGS 是按预期创建的,每页一张图片。我怀疑 OCR 是图像并写入字节数组的最后一个循环,但是我还不能修复它。
  • 可能是f = open(outfile, "w+b")。这会以写入模式打开文件,但您可能需要一个 for append
猜你喜欢
  • 1970-01-01
  • 2017-06-28
  • 2012-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
相关资源
最近更新 更多