screenplays-3k / ocr_pdfs.py
aaronday3's picture
Upload 5 files
a04461a verified
raw
history blame
844 Bytes
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"])