Python:遍历目录并将结果写入单独的txt文件

问题描述

我正在尝试遍历 pdf 文件目录。我首先将所有 PDF 转换为 jpeg,最后转换为 txt。我已经能够遍历 PDF 目录并将每个 jpeg 文件写入单个 txt 文件,但我真正需要的是每个 PDF 的单独 txt 文件。我了解 pdf 的每一页都被转换为 JPEG 然后写入文本文件的问题。如果有 2 个 PDF,我想要 2 个 txt 文件。以下是我到目前为止的代码。谢谢你的帮助。 from PIL 导入图片

import PyTesseract 
import sys 
from pdf2image import convert_from_path 
import os 
import cv2
import glob

for filepath in glob.iglob("path/*.pdf"):
    PDF_file = filepath
  
    pages = convert_from_path(PDF_file,500) 
  
    image_counter = 1
  
    for page in pages: 
  
        filename = "page_"+str(image_counter)+".jpg"
      
        page.save(filename,'JPEG') 
  
        image_counter = image_counter + 1
  
    filelimit = image_counter-1
  
    outfile = "out_text.txt"
  
    f = open(outfile,"a") 
  
    for i in range(1,filelimit + 1): 
  
        filename = "page_"+str(i)+".jpg"
          
        text = str(((PyTesseract.image_to_string(Image.open(filename))))) 
  
        text = text.replace('-\n','')     
  
        f.write(text) 
  
    f.close() 

解决方法

如果您想在不同的 pdf 页面的单独文本文件中输出。然后,您应该为每个 pdf 的页面以不同的名称打开文件。像这样:

for i in range(1,filelimit + 1): 
    outfile = "out_text_"+ str(i) +".txt"
    f = open(outfile,"a") 
    filename = "page_"+str(i)+".jpg"
    text = str(((pytesseract.image_to_string(Image.open(filename))))) 
    text = text.replace('-\n','')     
    f.write(text) 
    f.close()