File size: 844 Bytes
a04461a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
import subprocess
from tqdm import tqdm

# Define the directories
pdfs_folder = "pdfs"
texts_folder = "texts3"
ocr_pdfs_folder = "ocr_d_pdfs"

# Create the texts3 folder if it doesn't exist
if not os.path.exists(texts_folder):
    os.makedirs(texts_folder)

if not os.path.exists(ocr_pdfs_folder):
    os.makedirs(ocr_pdfs_folder)

# Iterate over all files in the pdfs folder
for filename in tqdm(os.listdir(pdfs_folder)):
    if filename.endswith(".pdf"):
        pdf_path = os.path.join(pdfs_folder, filename)
        text_filename = filename.replace(".pdf", ".txt")
        output_pdf = os.path.join(ocr_pdfs_folder, filename)
        text_path = os.path.join(texts_folder, text_filename)
        
        # Run the ocrmypdf command
        subprocess.run(["ocrmypdf", pdf_path, output_pdf, "--sidecar", text_path, "--force-ocr"])