import os import PyPDF2 def convert_pdfs_to_txt(directory): # Iterate over all files in the directory for filename in os.listdir(directory): if filename.endswith(".pdf"): pdf_path = os.path.join(directory, filename) txt_path = os.path.join(directory, os.path.splitext(filename)[0] + ".txt") # Open the PDF file with open(pdf_path, "rb") as pdf_file: reader = PyPDF2.PdfReader(pdf_file) # Create a new TXT file with open(txt_path, "w", encoding="utf-8") as txt_file: for page in reader.pages: # Extract text from each page and write it to the TXT file txt_file.write(page.extract_text()) print(f"Converted {pdf_path} to {txt_path}") # Provide the directory path where the PDF files are located directory_path = "/home/ok/Desktop/OneDrive/PDF/" # Call the function to convert the PDF files to TXT convert_pdfs_to_txt(directory_path)