#!/usr/bin/env python3 import os import sys import argparse import urllib.request def install_and_import(package): import importlib try: importlib.import_module(package) except ImportError: import subprocess print(f"[{package}] is not installed. Installing dynamically...", file=sys.stderr) subprocess.check_call([sys.executable, "-m", "pip", "install", "--quiet", package]) finally: globals()[package] = importlib.import_module(package) # Ensure pypdf is installed and imported install_and_import('pypdf') def download_file(url, dest_path): print(f"Downloading PDF from {url}...", file=sys.stderr) headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'} req = urllib.request.Request(url, headers=headers) with urllib.request.urlopen(req) as response: with open(dest_path, 'wb') as out_file: out_file.write(response.read()) def extract_text_from_pdf(pdf_path): print(f"Parsing text from {pdf_path}...", file=sys.stderr) reader = pypdf.PdfReader(pdf_path) text_content = [] # Extract metadata if available meta = reader.metadata if meta: text_content.append("---") text_content.append(f"title: {meta.title or 'Unknown'}") text_content.append(f"author: {meta.author or 'Unknown'}") text_content.append(f"subject: {meta.subject or 'Unknown'}") text_content.append("---") text_content.append("") for page_num, page in enumerate(reader.pages): page_text = page.extract_text() if page_text: text_content.append(f"## Page {page_num + 1}") text_content.append(page_text) text_content.append("") return "\n".join(text_content) def main(): parser = argparse.ArgumentParser(description="Download and parse PDF files to clean text.") parser.add_argument("--url", help="URL of the PDF file to download and parse.") parser.add_argument("--file", help="Path to local PDF file to parse.") parser.add_argument("--output", required=True, help="Output path to save the extracted text (Markdown format).") args = parser.parse_args() # Create output directory if it doesn't exist out_dir = os.path.dirname(args.output) if out_dir and not os.path.exists(out_dir): os.makedirs(out_dir, exist_ok=True) pdf_temp_path = "scratch/temp_download.pdf" os.makedirs("scratch", exist_ok=True) try: if args.url: download_file(args.url, pdf_temp_path) target_pdf = pdf_temp_path elif args.file: target_pdf = args.file else: print("Error: You must provide either --url or --file", file=sys.stderr) sys.exit(1) extracted_text = extract_text_from_pdf(target_pdf) with open(args.output, "w", encoding="utf-8") as f: f.write(extracted_text) print(f"Successfully extracted text to: {args.output}", file=sys.stderr) # Clean up temp file if os.path.exists(pdf_temp_path) and args.url: os.remove(pdf_temp_path) except Exception as e: print(f"Error occurred during parsing: {str(e)}", file=sys.stderr) sys.exit(1) if __name__ == "__main__": main()