如何使用Python将.png图像转换为可搜索的PDF /单词

时间:2019-10-30 14:09:43

标签: python-3.x python-tesseract pytesser img2pdf

最近,我参加了一个项目。使用Python tesseract将扫描的PDF转换为可搜索的PDF /单词。

经过几次尝试,我能够将扫描的PDF转换为PNG图像文件,然后我感到震惊,有人可以帮助我将PNG文件转换为可搜索的Word / PDF。my piece of code attached

请找到所附图片以供参考。

Import os
Import sys
from PIL import image
Import pytesseract
from pytesseract import image_to_string

 Libpath =r'_______' #site-package
 Pop_path=r'_______' #poppler dlls
 Sys.path.insert(0,LibPath)

  from pdf2image import convert_from_path

     Pdfpath=r'_______' # PDF file directory
     imgpath=r'_______' #image output path

     images= convert_from_path(pdf_path = pdfpath, 
         dpi=500, poppler_path= pop_path)
      for idx, of in enumerate (images):
                 pg.save(imgPath+'PDF_Page_'+'.png',"PNG")
                 print('{} page converted'.format(str(idx)))

       try:
          from PIL import image
       except ImportError:
                 import image
         import pytesseract

     def ocr-core(images):
              Text = 
       pytesseract.image_to_string(image.open(images))
       return text
  print(ocr_core("image path/imagename))

就是这样,我已经写了.....然后我得到了多个“ .PNG”图像...现在我只能将一个PNG图像转换为文本。

如何转换所有图像并将其保存为CSV / word?

1 个答案:

答案 0 :(得分:0)

我已经回答了我的问题

      from PIL import image
      from pdf2image import convert_from_path
      import pytesseract
      import OS
      import sys

     Pdf_file_path = '_______' #your file path

    Images = convert_from_path(Pdf_file_path, dpi=500)

    Counter=1
    for page in Images:
           idx= "image_"+str(Counter)+".jpg" ##or ".png"
           page.save(idx, 'JPEG')
           Counter = Counter+1

     file=Counter-1
      Output= '_____' #where you want to save and file name
     f=open(output, "w")
     for i in range(1,file+1):
              idx= "image_"+str(Counter)+".jpg" ##or ".png"         
  text=str(pytesseract.image_to_string(Image.open(idx)))
         f.write(text)
         f.close()