我的代码应该从目录中获取每个pdf,对其进行OCR,然后为每个OCR的pdf返回一个.txt文件。 pdf和.txt文件的名称应相同,但.pdf更改为.txt。我被困在拆分输入pdf名称以为OCR文件生成扩展名为.txt的相同名称的过程中。目录中的示例文件如下所示:“ 000dbf9d-d53f-465f-a7ce-722722136fb7465.pdf”。我需要输出为“ 000dbf9d-d53f-465f-a7ce-722722136fb7465.txt”。另外,我的代码不会创建新的.txt文件,而是会为每次迭代覆盖一个文件。对于每个OCR的.pdf文件,我需要一个新的.txt文件。到目前为止的代码:
import io
import glob
from PIL import Image
import pytesseract
from wand.image import Image as wi
files = glob.glob(r"D:\files\**")
for file in files:
#print(file)
pdf = wi(filename = file, resolution = 300)
pdfImg = pdf.convert('jpeg')
imgBlobs = []
for img in pdfImg.sequence:
page = wi(image = img)
imgBlobs.append(page.make_blob('jpeg'))
extracted_texts = []
for imgBlob in imgBlobs:
im = Image.open(io.BytesIO(imgBlob))
text = pytesseract.image_to_string(im, lang = 'eng')
extracted_texts.append(text)
with open("D:\\extracted_text\\"+ "\\file1.txt", 'w') as f:
f.write(str(extracted_texts))
答案 0 :(得分:0)
You just need to keep track of your file name and re-use it in the last two lines:
# ...
import os
files = glob.glob(r"D:\files\**")
for file in files:
#print(file)
# Get the name of the file less any suffixes
name = os.path.basename(file).split('.')[0]
# ...
# Use `name` from above to name your text file
with open("D:\\extracted_text\\" + name + ".txt", 'w') as f:
f.write(str(extracted_texts))