scripts/extract_marker.py
scripts/extract_marker.pyBrowse 21 files
798 tokens
3,458 bytes
Token encoding: o200k_base
Snapshot 24fd22b
← Back to SKILL.md
1#!/usr/bin/env python32"""Extract text from documents using marker-pdf. High-quality OCR + layout analysis.3 4Requires ~3-5GB disk (PyTorch + models downloaded on first use).5Supports: PDF, DOCX, PPTX, XLSX, HTML, EPUB, images.6 7Usage:8 python extract_marker.py document.pdf9 python extract_marker.py document.pdf --output_dir ./output10 python extract_marker.py presentation.pptx11 python extract_marker.py spreadsheet.xlsx12 python extract_marker.py scanned_doc.pdf # OCR works here13 python extract_marker.py document.pdf --json # Structured output14 python extract_marker.py document.pdf --use_llm # LLM-boosted accuracy15"""16import sys17import os18 19def convert(path, output_dir=None, output_format="markdown", use_llm=False):20 from marker.converters.pdf import PdfConverter21 from marker.models import create_model_dict22 from marker.config.parser import ConfigParser23 24 config_dict = {}25 if use_llm:26 config_dict["use_llm"] = True27 28 config_parser = ConfigParser(config_dict)29 models = create_model_dict()30 converter = PdfConverter(config=config_parser.generate_config_dict(), artifact_dict=models)31 rendered = converter(path)32 33 if output_format == "json":34 import json35 print(json.dumps({36 "markdown": rendered.markdown,37 "metadata": rendered.metadata if hasattr(rendered, "metadata") else {},38 }, indent=2, ensure_ascii=False))39 else:40 print(rendered.markdown)41 42 # Save images if output_dir specified43 if output_dir and hasattr(rendered, "images") and rendered.images:44 from pathlib import Path45 Path(output_dir).mkdir(parents=True, exist_ok=True)46 for name, img_data in rendered.images.items():47 img_path = os.path.join(output_dir, name)48 with open(img_path, "wb") as f:49 f.write(img_data)50 print(f"\nSaved {len(rendered.images)} image(s) to {output_dir}/", file=sys.stderr)51 52 53def check_requirements():54 """Check disk space before installing."""55 import shutil56 free_gb = shutil.disk_usage("/").free / (1024**3)57 if free_gb < 5:58 print(f"⚠️ Only {free_gb:.1f}GB free. marker-pdf needs ~5GB for PyTorch + models.")59 print("Use pymupdf instead (scripts/extract_pymupdf.py) or free up disk space.")60 sys.exit(1)61 print(f"✓ {free_gb:.1f}GB free — sufficient for marker-pdf")62 63 64if __name__ == "__main__":65 import argparse66 67 parser = argparse.ArgumentParser(68 description="Extract text from documents using marker-pdf (high-quality OCR + layout analysis)."69 )70 parser.add_argument("path", nargs="?", help="Document to convert (PDF, DOCX, PPTX, XLSX, HTML, EPUB, image)")71 parser.add_argument("--output_dir", help="Directory to save extracted images")72 parser.add_argument("--json", action="store_true", help="Structured JSON output instead of markdown")73 parser.add_argument("--use_llm", action="store_true", help="LLM-boosted accuracy")74 parser.add_argument("--check", action="store_true", help="Check disk space requirements and exit")75 args = parser.parse_args()76 77 if args.check:78 check_requirements()79 sys.exit(0)80 if not args.path:81 parser.error("path is required unless --check is given")82 83 convert(84 args.path,85 output_dir=args.output_dir,86 output_format="json" if args.json else "markdown",87 use_llm=args.use_llm,88 )89