import os import subprocess import sys def setup_env(): print("Setting up local virtual environment for parsing...") if not os.path.exists(".venv"): subprocess.check_call([sys.executable, "-m", "venv", ".venv"]) pip_exe = os.path.join(".venv", "Scripts", "pip.exe") if os.name == "nt" else os.path.join(".venv", "bin", "pip") subprocess.check_call([pip_exe, "install", "pypdf", "python-docx"]) return os.path.join(".venv", "Scripts", "python.exe") if os.name == "nt" else os.path.join(".venv", "bin", "python") SCRIPT_CODE = """ import os import glob from pypdf import PdfReader from docx import Document os.makedirs('extracted_text', exist_ok=True) for filepath in glob.glob('source_Data/*.*'): filename = os.path.basename(filepath) output_path = f'extracted_text/{filename}.txt' print(f'Extracting {filename}...') try: if filename.endswith('.pdf'): reader = PdfReader(filepath) text = '' for page in reader.pages: text += page.extract_text() + '\\n' with open(output_path, 'w', encoding='utf-8') as f: f.write(text) elif filename.endswith('.docx'): doc = Document(filepath) text = '\\n'.join([p.text for p in doc.paragraphs]) with open(output_path, 'w', encoding='utf-8') as f: f.write(text) except Exception as e: print(f"Error parsing {filename}: {e}") print("Done extracting!") """ if __name__ == "__main__": python_exe = setup_env() script_path = "do_extract.py" with open(script_path, "w", encoding="utf-8") as f: f.write(SCRIPT_CODE) print("Running extraction script...") subprocess.check_call([python_exe, script_path])