A fast, helpful, and open-source document parser
python bindings
Logan Markewichd3df7db05664
diff --git a/.github/workflows/python-bindings.yml b/.github/workflows/python-bindings.ymlnew file mode 100644index 0000000..5828475--- /dev/null+++ b/.github/workflows/python-bindings.yml@@ -0,0 +1,278 @@+name: Python Bindings++on:+ push:+ branches: [main]+ tags: ['v*']+ pull_request:+ branches: [main]++env:+ CARGO_TERM_COLOR: always++jobs:+ build:+ strategy:+ fail-fast: false+ matrix:+ include:+ - os: macos-latest+ target: aarch64-apple-darwin++ - os: macos-13+ target: x86_64-apple-darwin++ - os: ubuntu-latest+ target: x86_64-unknown-linux-gnu++ - os: ubuntu-latest+ target: aarch64-unknown-linux-gnu++ - os: windows-latest+ target: x86_64-pc-windows-msvc++ runs-on: ${{ matrix.os }}+ name: Build ${{ matrix.target }}++ steps:+ - uses: actions/checkout@v4++ - uses: actions/setup-python@v5+ with:+ python-version: '3.12'++ - uses: dtolnay/rust-toolchain@stable+ with:+ targets: ${{ matrix.target }}++ - name: Install system dependencies (macOS)+ if: runner.os == 'macOS'+ run: brew install tesseract leptonica++ - uses: actions/cache@v4+ with:+ path: |+ ~/.cargo/registry+ ~/.cargo/git+ ~/.cache/pdfium-rs+ ~/Library/Caches/pdfium-rs+ target+ key: ${{ runner.os }}-${{ matrix.target }}-cargo-python-${{ hashFiles('**/Cargo.lock') }}++ - name: Build wheels+ uses: PyO3/maturin-action@v1+ with:+ target: ${{ matrix.target }}+ args: --release --out dist+ manylinux: auto+ working-directory: packages/python+ before-script-linux: |+ # Install build deps inside the manylinux container+ if command -v yum &> /dev/null; then+ yum install -y clang-devel tesseract-devel leptonica-devel || true+ elif command -v apt-get &> /dev/null; then+ apt-get update && apt-get install -y libclang-dev libtesseract-dev libleptonica-dev || true+ elif command -v apk &> /dev/null; then+ apk add clang-dev tesseract-ocr-dev leptonica-dev || true+ fi++ - name: Copy pdfium dylib into wheel (Unix)+ if: runner.os != 'Windows'+ working-directory: packages/python+ run: |+ bash scripts/copy-pdfium.sh+ for whl in dist/*.whl; do+ python -c "+ import zipfile, os, sys+ whl = sys.argv[1]+ if sys.platform == 'darwin':+ dylib = 'libpdfium.dylib'+ else:+ dylib = 'libpdfium.so'+ dylib_path = os.path.join('liteparse', dylib)+ if not os.path.exists(dylib_path):+ print(f'Warning: {dylib_path} not found, skipping')+ sys.exit(0)+ with zipfile.ZipFile(whl, 'a') as z:+ z.write(dylib_path, f'liteparse/{dylib}')+ print(f'Added {dylib} to {whl}')+ " "$whl"+ done++ - name: Copy pdfium dylib into wheel (Windows)+ if: runner.os == 'Windows'+ working-directory: packages/python+ shell: pwsh+ run: |+ # Find pdfium.dll in the build cache+ $cacheBase = "$env:LOCALAPPDATA\pdfium-rs"+ if (-not (Test-Path $cacheBase)) {+ $cacheBase = "$env:USERPROFILE\.cache\pdfium-rs"+ }+ $dll = Get-ChildItem -Path $cacheBase -Recurse -Filter "pdfium.dll" | Select-Object -First 1+ if (-not $dll) {+ $dll = Get-ChildItem -Path "..\..\target" -Recurse -Filter "pdfium.dll" | Select-Object -First 1+ }+ if ($dll) {+ Copy-Item $dll.FullName -Destination "liteparse\pdfium.dll"+ Write-Host "Copied $($dll.FullName) -> liteparse\pdfium.dll"+ foreach ($whl in Get-ChildItem dist\*.whl) {+ python -c @"+ import zipfile, sys, os+ whl = sys.argv[1]+ dll = os.path.join('liteparse', 'pdfium.dll')+ if os.path.exists(dll):+ with zipfile.ZipFile(whl, 'a') as z:+ z.write(dll, 'liteparse/pdfium.dll')+ print(f'Added pdfium.dll to {whl}')+ "@ $whl.FullName+ }+ } else {+ Write-Error "Could not find pdfium.dll"+ exit 1+ }++ - name: Upload wheels+ uses: actions/upload-artifact@v4+ with:+ name: wheels-${{ matrix.target }}+ path: packages/python/dist/*.whl+ if-no-files-found: error++ # Build source distribution+ sdist:+ runs-on: ubuntu-latest+ steps:+ - uses: actions/checkout@v4++ - name: Build sdist+ uses: PyO3/maturin-action@v1+ with:+ command: sdist+ args: --out dist+ working-directory: packages/python++ - name: Upload sdist+ uses: actions/upload-artifact@v4+ with:+ name: sdist+ path: packages/python/dist/*.tar.gz+ if-no-files-found: error++ # Test the built wheels on native platforms+ test:+ needs: build+ strategy:+ fail-fast: false+ matrix:+ include:+ - os: macos-latest+ target: aarch64-apple-darwin+ - os: macos-13+ target: x86_64-apple-darwin+ - os: ubuntu-latest+ target: x86_64-unknown-linux-gnu+ - os: windows-latest+ target: x86_64-pc-windows-msvc+ runs-on: ${{ matrix.os }}+ name: Test ${{ matrix.target }}++ steps:+ - uses: actions/checkout@v4++ - uses: actions/setup-python@v5+ with:+ python-version: '3.12'++ - name: Download wheels+ uses: actions/download-artifact@v4+ with:+ name: wheels-${{ matrix.target }}+ path: dist++ - name: Install wheel+ run: pip install --find-links dist liteparse++ - name: Smoke test+ run: |+ python -c "+ from liteparse import LiteParse, ParseResult, ParsedPage, TextItem, ScreenshotResult+ parser = LiteParse(ocr_enabled=False, quiet=True)+ print('LiteParse loaded successfully')+ print(repr(parser))+ "++ - name: Parse test+ run: |+ python -c "+ from liteparse import LiteParse+ parser = LiteParse(ocr_enabled=False, quiet=True, max_pages=2)+ result = parser.parse('demo/docs/apple-10q-q3-2025.pdf')+ assert result.num_pages == 2, f'Expected 2 pages, got {result.num_pages}'+ assert len(result.text) > 0, 'Expected non-empty text'+ page = result.get_page(1)+ assert page is not None, 'Expected page 1'+ assert len(page.text_items) > 0, 'Expected text items'+ print(f'Parsed {result.num_pages} pages, {len(result.text)} chars')+ print('Parse test passed')+ "++ - name: Bytes parse test+ run: |+ python -c "+ from liteparse import LiteParse+ parser = LiteParse(ocr_enabled=False, quiet=True, max_pages=1)+ with open('demo/docs/apple-10q-q3-2025.pdf', 'rb') as f:+ data = f.read()+ result = parser.parse(data)+ assert result.num_pages == 1+ assert len(result.text) > 0+ print('Bytes parse test passed')+ "++ - name: Screenshot test+ run: |+ python -c "+ from liteparse import LiteParse+ parser = LiteParse(ocr_enabled=False, quiet=True)+ screenshots = parser.screenshot('demo/docs/apple-10q-q3-2025.pdf', page_numbers=[1])+ assert len(screenshots) == 1+ assert screenshots[0].page_num == 1+ assert len(screenshots[0].image_bytes) > 0+ print(f'Screenshot: {screenshots[0].width}x{screenshots[0].height}, {len(screenshots[0].image_bytes)} bytes')+ print('Screenshot test passed')+ "++ - name: CLI test+ run: |+ lit --version+ lit parse demo/docs/apple-10q-q3-2025.pdf --no-ocr --max-pages 1 -q | head -5+ echo "CLI test passed"++ # Publish to PyPI on version tags+ publish:+ if: startsWith(github.ref, 'refs/tags/v')+ needs: test+ runs-on: ubuntu-latest+ name: Publish to PyPI+ permissions:+ id-token: write++ steps:+ - name: Download all wheels+ uses: actions/download-artifact@v4+ with:+ pattern: wheels-*+ path: dist+ merge-multiple: true++ - name: Download sdist+ uses: actions/download-artifact@v4+ with:+ name: sdist+ path: dist++ - name: Publish to PyPI+ uses: pypa/gh-action-pypi-publish@release/v1+ with:+ packages-dir: dist/diff --git a/Cargo.lock b/Cargo.lockindex 5189fd3..0640905 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1233,6 +1233,22 @@ dependencies = ["ureq",]+[[package]]+name = "liteparse-python"+version = "2.0.0"+dependencies = [+ "anyhow",+ "clap",+ "image",+ "liteparse",+ "liteparse-pdfium",+ "liteparse-pdfium-sys",+ "pyo3",+ "serde",+ "serde_json",+ "tokio",+]+[[package]]name = "lock_api"version = "0.4.14"@@ -1561,6 +1577,12 @@ dependencies = ["miniz_oxide",]+[[package]]+name = "portable-atomic"+version = "1.13.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"+[[package]]name = "potential_utf"version = "0.1.5"@@ -1610,6 +1632,64 @@ version = "0.1.29"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "e0c5ccf5294c6ccd63a74f1565028353830a9c2f5eb0c682c355c471726a6e3f"+[[package]]+name = "pyo3"+version = "0.28.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "91fd8e38a3b50ed1167fb981cd6fd60147e091784c427b8f7183a7ee32c31c12"+dependencies = [+ "libc",+ "once_cell",+ "portable-atomic",+ "pyo3-build-config",+ "pyo3-ffi",+ "pyo3-macros",+]++[[package]]+name = "pyo3-build-config"+version = "0.28.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e368e7ddfdeb98c9bca7f8383be1648fd84ab466bf2bc015e94008db6d35611e"+dependencies = [+ "target-lexicon",+]++[[package]]+name = "pyo3-ffi"+version = "0.28.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "7f29e10af80b1f7ccaf7f69eace800a03ecd13e883acfacc1e5d0988605f651e"+dependencies = [+ "libc",+ "pyo3-build-config",+]++[[package]]+name = "pyo3-macros"+version = "0.28.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "df6e520eff47c45997d2fc7dd8214b25dd1310918bbb2642156ef66a67f29813"+dependencies = [+ "proc-macro2",+ "pyo3-macros-backend",+ "quote",+ "syn",+]++[[package]]+name = "pyo3-macros-backend"+version = "0.28.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c4cdc218d835738f81c2338f822078af45b4afdf8b2e33cbb5916f108b813acb"+dependencies = [+ "heck",+ "proc-macro2",+ "pyo3-build-config",+ "quote",+ "syn",+]+[[package]]name = "quinn"version = "0.11.9"@@ -2240,6 +2320,12 @@ dependencies = ["xattr",]+[[package]]+name = "target-lexicon"+version = "0.13.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "adb6935a6f5c20170eeceb1a3835a49e12e19d792f6dd344ccc76a985ca5a6ca"+[[package]]name = "tempfile"version = "3.27.0"diff --git a/Cargo.toml b/Cargo.tomlindex 81323e0..8fd8f78 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -5,6 +5,7 @@ members = ["crates/pdfium","crates/liteparse","crates/liteparse-napi",+ "crates/liteparse-python",][workspace.package]diff --git a/crates/liteparse-python/Cargo.toml b/crates/liteparse-python/Cargo.tomlnew file mode 100644index 0000000..fe4ee62--- /dev/null+++ b/crates/liteparse-python/Cargo.toml@@ -0,0 +1,27 @@+[package]+name = "liteparse-python"+version = "2.0.0"+edition.workspace = true+license.workspace = true+repository.workspace = true+description = "Python bindings for LiteParse"++[lib]+name = "_liteparse"+crate-type = ["cdylib"]++[features]+default = ["tesseract"]+tesseract = ["liteparse/tesseract"]++[dependencies]+liteparse = { package = "liteparse", version = "2.0.0", path = "../liteparse", default-features = false }+pdfium-sys = { package = "liteparse-pdfium-sys", version = "1.0.0", path = "../pdfium-sys" }+pdfium = { package = "liteparse-pdfium", version = "1.0.0", path = "../pdfium" }+clap = { version = "4.5.55", features = ["derive"] }+pyo3 = { version = "0.28", features = ["extension-module"] }+anyhow = "1.0.102"+image = { version = "0.25", default-features = false, features = ["png"] }+serde = { version = "1", features = ["derive"] }+serde_json = "1"+tokio = { version = "1", features = ["full"] }diff --git a/crates/liteparse-python/build.rs b/crates/liteparse-python/build.rsnew file mode 100644index 0000000..ee9ce6e--- /dev/null+++ b/crates/liteparse-python/build.rs@@ -0,0 +1,23 @@+use std::env;++fn main() {+ // Get the pdfium lib path from the pdfium-sys link metadata+ let lib_path = env::var("DEP_PDFIUM_LIB_PATH").expect("DEP_PDFIUM_LIB_PATH not set (from pdfium-sys)");++ let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();++ // Tell the linker to set @loader_path as an rpath on macOS,+ // or $ORIGIN on Linux, so the .so can find libpdfium next to itself.+ match target_os.as_str() {+ "macos" => {+ println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path");+ }+ "linux" => {+ println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN");+ }+ _ => {}+ }++ // Emit the lib path so maturin/CI scripts can find and bundle libpdfium+ println!("cargo:rustc-env=PDFIUM_LIB_DIR={lib_path}");+}diff --git a/crates/liteparse-python/src/cli.rs b/crates/liteparse-python/src/cli.rsnew file mode 100644index 0000000..7ee37d7--- /dev/null+++ b/crates/liteparse-python/src/cli.rs@@ -0,0 +1,304 @@+use clap::{Args, Parser, Subcommand};+use liteparse::config::{LiteParseConfig, OutputFormat};+use liteparse::conversion;+use liteparse::extract;+use liteparse::parser::LiteParse;+use liteparse::render;++#[derive(Parser, Debug)]+#[command(+ name = "lit",+ version,+ about = "OSS document parsing tool (supports PDF, DOCX, XLSX, images, and more)"+)]+struct Cli {+ #[command(subcommand)]+ command: Commands,+}++#[derive(Subcommand, Debug)]+enum Commands {+ /// Parse a document file (PDF, DOCX, XLSX, PPTX, images, etc.)+ Parse(ParseCommand),+ /// Generate screenshots of PDF pages+ Screenshot(ScreenshotCommand),+ /// Parse multiple documents in batch mode+ BatchParse(BatchParseCommand),+ /// Extract raw text items from a PDF file (no grid projection) [dev tool]+ Extract(ExtractCommand),+ /// Extract embedded image bounding boxes from a page [dev tool]+ ImageBounds(ExtractCommand),+}++#[derive(Args, Debug)]+struct ParseCommand {+ file: String,+ #[arg(short, long)]+ output: Option<String>,+ #[arg(long, default_value = "text")]+ format: String,+ #[arg(long)]+ no_ocr: bool,+ #[arg(long, default_value = "eng")]+ ocr_language: String,+ #[arg(long, default_value = None)]+ ocr_server_url: Option<String>,+ #[arg(long)]+ tessdata_path: Option<String>,+ #[arg(long, default_value = "1000")]+ max_pages: usize,+ #[arg(long)]+ target_pages: Option<String>,+ #[arg(long, default_value = "150")]+ dpi: f32,+ #[arg(long)]+ preserve_small_text: bool,+ #[arg(long)]+ password: Option<String>,+ #[arg(short, long)]+ quiet: bool,+}++#[derive(Args, Debug)]+struct ScreenshotCommand {+ file: String,+ #[arg(short, long, default_value = "./screenshots")]+ output_dir: String,+ #[arg(long)]+ target_pages: Option<String>,+ #[arg(long, default_value = "150")]+ dpi: f32,+ #[arg(long)]+ password: Option<String>,+ #[arg(short, long)]+ quiet: bool,+}++#[derive(Args, Debug)]+struct BatchParseCommand {+ input_dir: String,+ output_dir: String,+ #[arg(long, default_value = "text")]+ format: String,+ #[arg(long)]+ no_ocr: bool,+ #[arg(long, default_value = "eng")]+ ocr_language: String,+ #[arg(long, default_value = None)]+ ocr_server_url: Option<String>,+ #[arg(long)]+ tessdata_path: Option<String>,+ #[arg(long, default_value = "1000")]+ max_pages: usize,+ #[arg(long, default_value = "150")]+ dpi: f32,+ #[arg(long)]+ recursive: bool,+ #[arg(long)]+ extension: Option<String>,+ #[arg(long)]+ password: Option<String>,+ #[arg(short, long)]+ quiet: bool,+}++#[derive(Args, Debug)]+struct ExtractCommand {+ #[arg(long)]+ pdf_path: String,+ #[arg(long)]+ page_num: Option<u32>,+}++fn parse_output_format(s: &str) -> Result<OutputFormat, String> {+ match s.to_lowercase().as_str() {+ "json" => Ok(OutputFormat::Json),+ "text" => Ok(OutputFormat::Text),+ _ => Err(format!("unknown format '{}', expected 'json' or 'text'", s)),+ }+}++/// Run the CLI with the given args (typically from sys.argv).+pub fn run_cli(args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {+ let cli = Cli::parse_from(args);+ let rt = tokio::runtime::Runtime::new()?;++ match cli.command {+ Commands::Parse(cmd) => {+ let format = parse_output_format(&cmd.format)?;+ let config = LiteParseConfig {+ ocr_language: cmd.ocr_language,+ ocr_enabled: !cmd.no_ocr,+ tessdata_path: cmd.tessdata_path,+ max_pages: cmd.max_pages,+ target_pages: cmd.target_pages,+ dpi: cmd.dpi,+ output_format: format,+ preserve_very_small_text: cmd.preserve_small_text,+ password: cmd.password,+ quiet: cmd.quiet,+ ocr_server_url: cmd.ocr_server_url,+ };+ let lp = LiteParse::new(config);+ let result = rt.block_on(lp.parse(&cmd.file))?;+ let formatted = lp.format(&result)?;+ match cmd.output {+ Some(path) => {+ std::fs::write(&path, &formatted)?;+ if !cmd.quiet {+ eprintln!("[liteparse] wrote output to {}", path);+ }+ }+ None => println!("{}", formatted),+ }+ }++ Commands::Screenshot(cmd) => {+ let target_pages = cmd+ .target_pages+ .as_ref()+ .map(|s| liteparse::config::parse_target_pages(s))+ .transpose()+ .map_err(|e| format!("invalid --target-pages: {}", e))?;++ std::fs::create_dir_all(&cmd.output_dir)?;++ let lib = pdfium::Library::init();+ let document = lib.load_document(&cmd.file, cmd.password.as_deref())?;+ let page_count = document.page_count();++ for page_index in 0..page_count {+ let page_number = page_index as u32 + 1;+ if let Some(ref targets) = target_pages+ && !targets.contains(&page_number)+ {+ continue;+ }+ let output_path = format!("{}/page_{}.png", cmd.output_dir, page_number);+ render::screenshot(&cmd.file, page_number, cmd.dpi, &output_path)?;+ if !cmd.quiet {+ eprintln!("[liteparse] screenshot page {} → {}", page_number, output_path);+ }+ }+ }++ Commands::BatchParse(cmd) => {+ let format = parse_output_format(&cmd.format)?;+ let ext_filter = cmd.extension.as_ref().map(|e| {+ let e = e.to_lowercase();+ if e.starts_with('.') { e } else { format!(".{}", e) }+ });++ let config = LiteParseConfig {+ ocr_language: cmd.ocr_language,+ ocr_enabled: !cmd.no_ocr,+ tessdata_path: cmd.tessdata_path,+ max_pages: cmd.max_pages,+ target_pages: None,+ dpi: cmd.dpi,+ output_format: format.clone(),+ preserve_very_small_text: false,+ password: cmd.password,+ quiet: cmd.quiet,+ ocr_server_url: cmd.ocr_server_url,+ };++ let lp = LiteParse::new(config);+ let out_ext = if format == OutputFormat::Json { "json" } else { "txt" };++ std::fs::create_dir_all(&cmd.output_dir)?;+ let files = collect_files(&cmd.input_dir, cmd.recursive, ext_filter.as_deref())?;++ if files.is_empty() {+ eprintln!("[liteparse] no matching files found in {}", cmd.input_dir);+ return Ok(());+ }+ if !cmd.quiet {+ eprintln!("[liteparse] found {} files to process", files.len());+ }++ let mut success = 0usize;+ let mut errors = 0usize;++ for file_path in &files {+ let t0 = std::time::Instant::now();+ let rel = file_path.strip_prefix(&cmd.input_dir).unwrap_or(file_path);+ let out_path = std::path::Path::new(&cmd.output_dir)+ .join(rel)+ .with_extension(out_ext);++ if let Some(parent) = out_path.parent() {+ std::fs::create_dir_all(parent)?;+ }++ match rt.block_on(lp.parse(file_path)) {+ Ok(result) => match lp.format(&result) {+ Ok(formatted) => {+ std::fs::write(&out_path, &formatted)?;+ success += 1;+ if !cmd.quiet {+ let elapsed = t0.elapsed().as_secs_f64() * 1000.0;+ eprintln!("[liteparse] {} → {} ({:.1}ms)", file_path, out_path.display(), elapsed);+ }+ }+ Err(e) => { eprintln!("[liteparse] error formatting {}: {}", file_path, e); errors += 1; }+ },+ Err(e) => { eprintln!("[liteparse] error parsing {}: {}", file_path, e); errors += 1; }+ }+ }++ eprintln!("[liteparse] batch complete: {} succeeded, {} failed", success, errors);+ if errors > 0 {+ std::process::exit(1);+ }+ }++ Commands::Extract(cmd) => {+ extract::extract(&cmd.pdf_path, cmd.page_num)?;+ }+ Commands::ImageBounds(cmd) => {+ render::image_bounds(&cmd.pdf_path, cmd.page_num)?;+ }+ }++ Ok(())+}++fn collect_files(+ dir: &str,+ recursive: bool,+ ext_filter: Option<&str>,+) -> Result<Vec<String>, Box<dyn std::error::Error>> {+ let mut files = Vec::new();+ collect_files_inner(std::path::Path::new(dir), recursive, ext_filter, &mut files)?;+ files.sort();+ Ok(files)+}++fn collect_files_inner(+ dir: &std::path::Path,+ recursive: bool,+ ext_filter: Option<&str>,+ files: &mut Vec<String>,+) -> Result<(), Box<dyn std::error::Error>> {+ for entry in std::fs::read_dir(dir)? {+ let entry = entry?;+ let path = entry.path();+ if path.is_dir() {+ if recursive {+ collect_files_inner(&path, recursive, ext_filter, files)?;+ }+ continue;+ }+ let path_str = path.to_string_lossy().to_string();+ if let Some(filter) = ext_filter {+ if !path_str.to_lowercase().ends_with(filter) {+ continue;+ }+ } else if !conversion::is_supported_extension(&path_str) {+ continue;+ }+ files.push(path_str);+ }+ Ok(())+}diff --git a/crates/liteparse-python/src/lib.rs b/crates/liteparse-python/src/lib.rsnew file mode 100644index 0000000..c633d7e--- /dev/null+++ b/crates/liteparse-python/src/lib.rs@@ -0,0 +1,406 @@+use pyo3::prelude::*;+use pyo3::types::PyBytes;++use liteparse::config::{LiteParseConfig, OutputFormat};+use liteparse::types::PdfInput;++mod cli;++// ---------------------------------------------------------------------------+// Python type wrappers+// ---------------------------------------------------------------------------++#[pyclass(frozen, from_py_object)]+#[derive(Clone)]+struct PyTextItem {+ #[pyo3(get)]+ text: String,+ #[pyo3(get)]+ x: f64,+ #[pyo3(get)]+ y: f64,+ #[pyo3(get)]+ width: f64,+ #[pyo3(get)]+ height: f64,+ #[pyo3(get)]+ font_name: Option<String>,+ #[pyo3(get)]+ font_size: Option<f64>,+ #[pyo3(get)]+ confidence: Option<f64>,+}++#[pymethods]+impl PyTextItem {+ fn __repr__(&self) -> String {+ format!(+ "TextItem(text={:?}, x={}, y={}, width={}, height={})",+ self.text, self.x, self.y, self.width, self.height+ )+ }+}++impl PyTextItem {+ fn from_rust(item: &liteparse::types::TextItem) -> Self {+ Self {+ text: item.text.clone(),+ x: item.x as f64,+ y: item.y as f64,+ width: item.width as f64,+ height: item.height as f64,+ font_name: item.font_name.clone(),+ font_size: item.font_size.map(|v| v as f64),+ confidence: item.confidence.map(|v| v as f64).or(Some(1.0)),+ }+ }+}++#[pyclass(frozen, from_py_object)]+#[derive(Clone)]+struct PyParsedPage {+ #[pyo3(get)]+ page_num: u32,+ #[pyo3(get)]+ width: f64,+ #[pyo3(get)]+ height: f64,+ #[pyo3(get)]+ text: String,+ #[pyo3(get)]+ text_items: Vec<PyTextItem>,+}++#[pymethods]+impl PyParsedPage {+ fn __repr__(&self) -> String {+ format!(+ "ParsedPage(page_num={}, width={}, height={}, text_items={})",+ self.page_num,+ self.width,+ self.height,+ self.text_items.len()+ )+ }+}++impl PyParsedPage {+ fn from_rust(page: &liteparse::types::ParsedPage) -> Self {+ Self {+ page_num: page.page_number as u32,+ width: page.page_width as f64,+ height: page.page_height as f64,+ text: page.text.clone(),+ text_items: page.text_items.iter().map(PyTextItem::from_rust).collect(),+ }+ }+}++#[pyclass(frozen, from_py_object)]+#[derive(Clone)]+struct PyParseResult {+ #[pyo3(get)]+ pages: Vec<PyParsedPage>,+ #[pyo3(get)]+ text: String,+}++#[pymethods]+impl PyParseResult {+ #[getter]+ fn num_pages(&self) -> usize {+ self.pages.len()+ }++ fn get_page(&self, page_num: u32) -> Option<PyParsedPage> {+ self.pages.iter().find(|p| p.page_num == page_num).cloned()+ }++ fn __repr__(&self) -> String {+ format!("ParseResult(pages={}, text_len={})", self.pages.len(), self.text.len())+ }+}++impl PyParseResult {+ fn from_rust(result: &liteparse::parser::ParseResult) -> Self {+ Self {+ pages: result.pages.iter().map(PyParsedPage::from_rust).collect(),+ text: result.text.clone(),+ }+ }++ fn to_rust(&self) -> liteparse::parser::ParseResult {+ liteparse::parser::ParseResult {+ pages: self+ .pages+ .iter()+ .map(|p| liteparse::types::ParsedPage {+ page_number: p.page_num as usize,+ page_width: p.width as f32,+ page_height: p.height as f32,+ text: p.text.clone(),+ text_items: p+ .text_items+ .iter()+ .map(|item| liteparse::types::TextItem {+ text: item.text.clone(),+ x: item.x as f32,+ y: item.y as f32,+ width: item.width as f32,+ height: item.height as f32,+ rotation: 0.0,+ font_name: item.font_name.clone(),+ font_size: item.font_size.map(|v| v as f32),+ font_height: None,+ font_ascent: None,+ font_descent: None,+ font_weight: None,+ font_flags: None,+ text_width: None,+ font_is_buggy: false,+ mcid: None,+ fill_color: None,+ stroke_color: None,+ confidence: item.confidence.map(|v| v as f32),+ })+ .collect(),+ })+ .collect(),+ text: self.text.clone(),+ }+ }+}++#[pyclass(frozen, from_py_object)]+#[derive(Clone)]+struct PyScreenshotResult {+ #[pyo3(get)]+ page_num: u32,+ #[pyo3(get)]+ width: u32,+ #[pyo3(get)]+ height: u32,+ image_buffer: Vec<u8>,+}++#[pymethods]+impl PyScreenshotResult {+ #[getter]+ fn image_bytes<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {+ PyBytes::new(py, &self.image_buffer)+ }++ fn __repr__(&self) -> String {+ format!(+ "ScreenshotResult(page_num={}, width={}, height={})",+ self.page_num, self.width, self.height+ )+ }+}++// ---------------------------------------------------------------------------+// Main LiteParse class+// ---------------------------------------------------------------------------++#[pyclass]+struct LiteParse {+ inner: liteparse::parser::LiteParse,+ config: LiteParseConfig,+ runtime: tokio::runtime::Runtime,+}++#[pymethods]+impl LiteParse {+ #[new]+ #[pyo3(signature = (+ *,+ ocr_language = None,+ ocr_enabled = None,+ ocr_server_url = None,+ tessdata_path = None,+ max_pages = None,+ target_pages = None,+ dpi = None,+ output_format = None,+ preserve_very_small_text = None,+ password = None,+ quiet = None,+ ))]+ fn new(+ ocr_language: Option<String>,+ ocr_enabled: Option<bool>,+ ocr_server_url: Option<String>,+ tessdata_path: Option<String>,+ max_pages: Option<usize>,+ target_pages: Option<String>,+ dpi: Option<f32>,+ output_format: Option<String>,+ preserve_very_small_text: Option<bool>,+ password: Option<String>,+ quiet: Option<bool>,+ ) -> PyResult<Self> {+ let mut cfg = LiteParseConfig::default();+ if let Some(v) = ocr_language {+ cfg.ocr_language = v;+ }+ if let Some(v) = ocr_enabled {+ cfg.ocr_enabled = v;+ }+ if let Some(v) = ocr_server_url {+ cfg.ocr_server_url = Some(v);+ }+ if let Some(v) = tessdata_path {+ cfg.tessdata_path = Some(v);+ }+ if let Some(v) = max_pages {+ cfg.max_pages = v;+ }+ if let Some(v) = target_pages {+ cfg.target_pages = Some(v);+ }+ if let Some(v) = dpi {+ cfg.dpi = v;+ }+ if let Some(v) = output_format {+ cfg.output_format = match v.as_str() {+ "text" => OutputFormat::Text,+ _ => OutputFormat::Json,+ };+ }+ if let Some(v) = preserve_very_small_text {+ cfg.preserve_very_small_text = v;+ }+ if let Some(v) = password {+ cfg.password = Some(v);+ }+ if let Some(v) = quiet {+ cfg.quiet = v;+ }++ let inner = liteparse::parser::LiteParse::new(cfg.clone());+ let runtime = tokio::runtime::Runtime::new()+ .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?;++ Ok(Self {+ inner,+ config: cfg,+ runtime,+ })+ }++ /// Parse a document from a file path.+ fn parse(&self, input: String) -> PyResult<PyParseResult> {+ let pdf_input = PdfInput::Path(input);+ let result = self+ .runtime+ .block_on(self.inner.parse_input(pdf_input))+ .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?;+ Ok(PyParseResult::from_rust(&result))+ }++ /// Parse a document from raw bytes.+ fn parse_bytes(&self, data: Vec<u8>) -> PyResult<PyParseResult> {+ let pdf_input = PdfInput::Bytes(data);+ let result = self+ .runtime+ .block_on(self.inner.parse_input(pdf_input))+ .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?;+ Ok(PyParseResult::from_rust(&result))+ }++ /// Take screenshots of document pages. Returns a list of ScreenshotResult.+ #[pyo3(signature = (input, page_numbers = None))]+ fn screenshot(+ &self,+ input: String,+ page_numbers: Option<Vec<u32>>,+ ) -> PyResult<Vec<PyScreenshotResult>> {+ let dpi = self.config.dpi;+ let lib = pdfium::Library::init();+ let document = lib+ .load_document(&input, self.config.password.as_deref())+ .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?;+ let page_count = document.page_count() as u32;++ let pages: Vec<u32> = match page_numbers {+ Some(nums) => nums,+ None => (1..=page_count).collect(),+ };++ let mut results = Vec::with_capacity(pages.len());+ for page_num in pages {+ if page_num < 1 || page_num > page_count {+ return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(+ "page {page_num} out of range (document has {page_count} pages)"+ )));+ }+ let page = document.page((page_num - 1) as i32).map_err(|e| {+ PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string())+ })?;+ let bitmap = page.render(dpi).map_err(|e| {+ PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string())+ })?;++ let width = bitmap.width() as u32;+ let height = bitmap.height() as u32;+ let rgba = bitmap.to_rgba();++ let mut png_buf: Vec<u8> = Vec::new();+ let encoder = image::codecs::png::PngEncoder::new(&mut png_buf);+ use image::ImageEncoder;+ encoder+ .write_image(&rgba, width, height, image::ColorType::Rgba8.into())+ .map_err(|e| {+ PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(+ "PNG encode failed: {e}"+ ))+ })?;++ results.push(PyScreenshotResult {+ page_num,+ width,+ height,+ image_buffer: png_buf,+ });+ }++ Ok(results)+ }++ /// Format a parse result according to the configured output format.+ fn format(&self, result: &PyParseResult) -> PyResult<String> {+ let rust_result = result.to_rust();+ self.inner+ .format(&rust_result)+ .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))+ }++ fn __repr__(&self) -> String {+ format!(+ "LiteParse(ocr_enabled={}, dpi={}, max_pages={})",+ self.config.ocr_enabled, self.config.dpi, self.config.max_pages+ )+ }+}++// ---------------------------------------------------------------------------+// Module+// ---------------------------------------------------------------------------++/// Run the `lit` CLI with the given arguments.+#[pyfunction]+fn run_cli(args: Vec<String>) -> PyResult<()> {+ cli::run_cli(args)+ .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))+}++#[pymodule]+fn _liteparse(m: &Bound<'_, PyModule>) -> PyResult<()> {+ m.add_class::<LiteParse>()?;+ m.add_class::<PyParseResult>()?;+ m.add_class::<PyParsedPage>()?;+ m.add_class::<PyTextItem>()?;+ m.add_class::<PyScreenshotResult>()?;+ m.add_function(wrap_pyfunction!(run_cli, m)?)?;+ Ok(())+}diff --git a/packages/python/.gitignore b/packages/python/.gitignorenew file mode 100644index 0000000..857b617--- /dev/null+++ b/packages/python/.gitignore@@ -0,0 +1,5 @@+# Native extension and bundled dylib (built by maturin)+*.so+*.pyd+*.dylib+*.dlldiff --git a/packages/python/liteparse/__init__.py b/packages/python/liteparse/__init__.pyindex d257ce9..79b502c 100644--- a/packages/python/liteparse/__init__.py+++ b/packages/python/liteparse/__init__.py@@ -1,37 +1,18 @@from .parser import LiteParsefrom .types import (- # Enums- OutputFormat,- ImageFormat,- # ResultsParseResult,- BatchResult,ParsedPage,TextItem,- BoundingBox,ScreenshotResult,- ScreenshotBatchResult,- # ErrorsParseError,- CLINotFoundError,)-__version__ = "1.2.1"+__version__ = "2.0.0"__all__ = [- # Main class"LiteParse",- # Enums- "OutputFormat",- "ImageFormat",- # Results"ParseResult",- "BatchResult","ParsedPage","TextItem",- "BoundingBox","ScreenshotResult",- "ScreenshotBatchResult",- # Errors"ParseError",- "CLINotFoundError",]diff --git a/packages/python/liteparse/cli.py b/packages/python/liteparse/cli.pynew file mode 100644index 0000000..d92e700--- /dev/null+++ b/packages/python/liteparse/cli.py@@ -0,0 +1,19 @@+"""CLI entry point for the `lit` command."""++import sys++from liteparse._liteparse import run_cli+++def main() -> None:+ try:+ run_cli(sys.argv)+ except SystemExit:+ raise+ except Exception as e:+ print(f"Error: {e}", file=sys.stderr)+ sys.exit(1)+++if __name__ == "__main__":+ main()diff --git a/packages/python/liteparse/parser.py b/packages/python/liteparse/parser.pyindex a74ade4..ca041bf 100644--- a/packages/python/liteparse/parser.py+++ b/packages/python/liteparse/parser.py@@ -1,235 +1,56 @@-"""LiteParse Python wrapper - wraps the Node.js CLI via subprocess."""+"""LiteParse Python wrapper - native Rust bindings via PyO3."""-import asyncio-import json-import os-import shutil-import subprocess-import tempfile-import warningsfrom pathlib import Path-from typing import Any, List, Literal, Optional, Union, cast+from typing import Any, List, Optional, Union++from liteparse._liteparse import LiteParse as _NativeLiteParsefrom .types import (- BatchResult,- BoundingBox,- CLINotFoundError,- ImageFormat,- OutputFormat,ParsedPage,ParseError,ParseResult,- ScreenshotBatchResult,ScreenshotResult,TextItem,)-def _find_cli(install_if_not_available: bool) -> str:- """Find the liteparse CLI executable."""- # Check if liteparse is in PATH- cli_path = shutil.which("liteparse")- if cli_path:- return cli_path-- # Check if npx is available- npx_path = shutil.which("npx")- if npx_path:- return "npx @llamaindex/liteparse"-- # Check common node_modules locations- possible_paths = [- "./node_modules/.bin/liteparse",- "../node_modules/.bin/liteparse",- "../../node_modules/.bin/liteparse",- ]-- for path in possible_paths:- if os.path.isfile(path):- return os.path.abspath(path)-- if install_if_not_available:- npm_path = shutil.which("npm")- if not npm_path:- raise CLINotFoundError(- "liteparse CLI not found and npm is not available to auto-install it.\n"- "Please install Node.js (>= 18) from https://nodejs.org/ and then run:\n"- " npm install -g @llamaindex/liteparse"- )- warnings.warn(- "liteparse CLI could not be found. Running `npm install -g @llamaindex/liteparse` to install it.",- UserWarning,- stacklevel=2,- )- result = subprocess.run(- ["npm", "install", "-g", "@llamaindex/liteparse"],- )- if result.returncode != 0:- raise subprocess.CalledProcessError(- result.returncode,- ["npm", "install", "-g", "@llamaindex/liteparse"],- result.stderr,- )- cli_path = shutil.which("liteparse")- if cli_path:- return cli_path-- raise CLINotFoundError(- "liteparse CLI not found. Please install Node.js (>= 18) and then run:\n"- " npm install -g @llamaindex/liteparse"- )---def _parse_json_result(json_data: dict) -> ParseResult:- """Parse JSON output from CLI into ParseResult."""+def _convert_native_result(native_result: Any) -> ParseResult:+ """Convert a native PyParseResult to our Python ParseResult."""pages: List[ParsedPage] = []-- for page_data in json_data.get("pages", []):- # Parse text items- text_items: List[TextItem] = []- for item in page_data.get("textItems", []):- text_items.append(- TextItem(- text=item.get("text", ""),- x=item.get("x", 0),- y=item.get("y", 0),- width=item.get("width", 0),- height=item.get("height", 0),- confidence=item.get("confidence"),- fontName=item.get("fontName"),- fontSize=item.get("fontSize"),- )- )-- # Parse bounding boxes- bounding_boxes: List[BoundingBox] = []- for bbox in page_data.get("boundingBoxes", []):- bounding_boxes.append(- BoundingBox(- x1=bbox.get("x1", 0),- y1=bbox.get("y1", 0),- x2=bbox.get("x2", 0),- y2=bbox.get("y2", 0),- )- )-+ for native_page in native_result.pages:+ text_items = [+ TextItem(+ text=item.text,+ x=item.x,+ y=item.y,+ width=item.width,+ height=item.height,+ font_name=item.font_name,+ font_size=item.font_size,+ confidence=item.confidence,+ )+ for item in native_page.text_items+ ]pages.append(ParsedPage(- pageNum=page_data.get("page", page_data.get("pageNum", 0)),- width=page_data.get("width", 0),- height=page_data.get("height", 0),- text=page_data.get("text", ""),- textItems=text_items,- boundingBoxes=bounding_boxes,+ page_num=native_page.page_num,+ width=native_page.width,+ height=native_page.height,+ text=native_page.text,+ text_items=text_items,))-- # Build full text from pages (JSON doesn't have top-level text field)- full_text = "\n\n".join(page.text for page in pages)-return ParseResult(pages=pages,- text=full_text,- json=json_data,+ text=native_result.text,)-def _build_parse_cli_args(- ocr_enabled: bool,- ocr_server_url: Optional[str],- ocr_language: str,- num_workers: Optional[int],- max_pages: int,- target_pages: Optional[str],- dpi: int,- precise_bounding_box: bool,- preserve_very_small_text: bool,- password: Optional[str],-) -> List[str]:- """Build CLI arguments for parse command."""- args: List[str] = ["--format", "json"]-- if not ocr_enabled:- args.append("--no-ocr")- elif ocr_server_url:- args.extend(["--ocr-server-url", ocr_server_url])-- args.extend(["--ocr-language", ocr_language])-- if num_workers is not None:- args.extend(["--num-workers", str(num_workers)])-- args.extend(["--max-pages", str(max_pages)])-- if target_pages:- args.extend(["--target-pages", target_pages])-- args.extend(["--dpi", str(dpi)])-- if not precise_bounding_box:- args.append("--no-precise-bbox")-- if preserve_very_small_text:- args.append("--preserve-small-text")-- if password:- args.extend(["--password", password])-- args.append("-q")- return args---def _build_batch_cli_args(- output_format: OutputFormat,- ocr_enabled: bool,- ocr_server_url: Optional[str],- ocr_language: str,- num_workers: Optional[int],- max_pages: int,- dpi: int,- precise_bounding_box: bool,- recursive: bool,- extension_filter: Optional[str],- password: Optional[str],-) -> List[str]:- """Build CLI arguments for batch-parse command."""- args: List[str] = ["--format", output_format.value]-- if not ocr_enabled:- args.append("--no-ocr")- elif ocr_server_url:- args.extend(["--ocr-server-url", ocr_server_url])-- args.extend(["--ocr-language", ocr_language])-- if num_workers is not None:- args.extend(["--num-workers", str(num_workers)])-- args.extend(["--max-pages", str(max_pages)])- args.extend(["--dpi", str(dpi)])-- if not precise_bounding_box:- args.append("--no-precise-bbox")-- if recursive:- args.append("--recursive")-- if extension_filter:- args.extend(["--extension", extension_filter])-- if password:- args.extend(["--password", password])-- args.append("-q")- return args--class LiteParse:"""Python wrapper for the LiteParse document parser.- This class wraps the LiteParse Node.js CLI, providing a Pythonic interface- for parsing PDFs and other documents.+ Uses native Rust bindings for fast, in-process PDF parsing.Example:>>> from liteparse import LiteParse@@ -239,623 +60,131 @@ class LiteParse:"""def __init__(- self, cli_path: Optional[str] = None, install_if_not_available: bool = True- ):- """- Initialize LiteParse parser.-- Args:- cli_path: Custom path to liteparse CLI (auto-detected if not provided)- install_if_not_available: Install the liteparse CLI from NPM if not available. Defaults to True.- """- self._cli_path = cli_path- self.install_if_not_available = install_if_not_available-- @property- def cli_path(self) -> str:- """Get the CLI path, finding it if not already set."""- if self._cli_path is None:- self._cli_path = _find_cli(self.install_if_not_available)- return self._cli_path-- def _prepare_command(self,- subcommand: Literal["parse", "batch-parse", "screenshot"],- *positional: Any,- **options: Any,- ) -> list[str]:- cmd_parts = self.cli_path.split()- cmd = cmd_parts + [subcommand, *positional]- if subcommand == "parse":- cmd.extend(_build_parse_cli_args(**options))- elif subcommand == "batch-parse":- cmd.extend(_build_batch_cli_args(**options))- return cmd-- @staticmethod- def _extract_path_and_bytes(- file_data: Union[str, Path, bytes],- ) -> tuple[str, Union[bytes, None]]:- if not isinstance(file_data, bytes):- file_path = Path(file_data)- if not file_path.exists():- raise FileNotFoundError(f"File not found: {file_path}")- file_path = str(file_path.absolute())- file_bytes = None- else:- file_path = "-"- file_bytes = file_data- return file_path, file_bytes-- @staticmethod- def _extract_batch_params(- input_dir: Union[str, Path],- output_dir: Union[str, Path],- output_format: Union[OutputFormat, str],- ) -> tuple[Path, Path, OutputFormat]:- indir = Path(input_dir)- outdir = Path(output_dir)-- if not indir.exists():- raise FileNotFoundError(f"Input directory not found: {input_dir}")-- if isinstance(output_format, str):- output_format = OutputFormat(output_format)-- return indir, outdir, output_format-- @staticmethod- def _extract_screenshot_params(- file_path: Union[str, Path],- image_format: Union[ImageFormat, str],- output_dir: Union[str, Path, None],- ) -> tuple[Path, ImageFormat, Path]:- file_path = Path(file_path)- if not file_path.exists():- raise FileNotFoundError(f"File not found: {file_path}")-- if isinstance(image_format, str):- image_format = ImageFormat(image_format)-- # Use temp dir if output_dir not provided- if output_dir is None:- output_dir = Path(tempfile.mkdtemp(prefix="liteparse_screenshots_"))- else:- output_dir = Path(output_dir)- output_dir.mkdir(parents=True, exist_ok=True)- return file_path, image_format, output_dir-- @staticmethod- def _get_parse_result(- returncode: int,- stdout: bytes,- stderr: bytes,- ) -> ParseResult:- if returncode != 0:- raise ParseError(- f"Parsing failed with exit code {returncode}",- stderr=stderr.decode("utf-8"),- )- try:- json_data = json.loads(stdout.decode("utf-8"))- return _parse_json_result(json_data)- except json.JSONDecodeError as e:- raise ParseError(- f"Failed to parse CLI output: {e}",- stderr=stderr.decode("utf-8"),- )-- @staticmethod- def _get_screenshot_result(- returncode: int,- stderr: str,- output_dir: Path,- image_format: ImageFormat,- load_bytes: bool,- ) -> ScreenshotBatchResult:- if returncode != 0:- raise ParseError(- f"Screenshot generation failed with exit code {returncode}",- stderr=stderr,- )- screenshots: List[ScreenshotResult] = []- ext = f".{image_format.value}"-- for img_file in sorted(output_dir.glob(f"*{ext}")):- # Parse page number from filename (page_N.png)- filename = img_file.stem- if filename.startswith("page_"):- try:- page_num = int(filename.replace("page_", ""))- except ValueError:- continue-- # Optionally load bytes- image_bytes = None- if load_bytes:- image_bytes = img_file.read_bytes()-- screenshots.append(- ScreenshotResult(- page_num=page_num,- image_path=str(img_file),- image_bytes=image_bytes,- )- )-- return ScreenshotBatchResult(- screenshots=screenshots,- output_dir=str(output_dir),- )-- def parse(- self,- file_data: Union[str, Path, bytes],*,- ocr_enabled: bool = True,+ ocr_enabled: Optional[bool] = None,ocr_server_url: Optional[str] = None,- ocr_language: str = "en",- num_workers: Optional[int] = None,- max_pages: int = 10000,+ ocr_language: Optional[str] = None,+ tessdata_path: Optional[str] = None,+ max_pages: Optional[int] = None,target_pages: Optional[str] = None,- dpi: int = 150,- precise_bounding_box: bool = True,- preserve_very_small_text: bool = False,+ dpi: Optional[float] = None,++ preserve_very_small_text: Optional[bool] = None,password: Optional[str] = None,- timeout: Optional[float] = None,- ) -> ParseResult:+ quiet: Optional[bool] = None,+ ):"""- Parse a document file.+ Initialize LiteParse parser.Args:- file_path: Path to the document file (PDF, DOCX, images, etc.)- file_bytes: Bytes content of the file- ocr_enabled: Whether to enable OCR for scanned documents+ ocr_enabled: Whether to enable OCR for scanned documents (default: True)ocr_server_url: URL of HTTP OCR server (uses Tesseract if not provided)- ocr_language: Language code for OCR (e.g., "en", "fr", "de")- num_workers: Number of pages to OCR in parallel (defaults to CPU cores - 1)+ ocr_language: Language code for OCR (e.g., "eng", "fra")+ tessdata_path: Path to tessdata directory for Tesseractmax_pages: Maximum number of pages to parsetarget_pages: Specific pages to parse (e.g., "1-5,10,15-20")dpi: DPI for rendering (affects OCR quality)- precise_bounding_box: Whether to compute precise bounding boxespreserve_very_small_text: Whether to preserve very small textpassword: Password for encrypted/protected documents- timeout: Timeout in seconds (None for no timeout)-- Returns:- ParseResult containing the parsed document data-- Raises:- ParseError: If parsing fails- FileNotFoundError: If the file doesn't exist- TimeoutError: If parsing times out+ quiet: Suppress progress output"""+ kwargs = {}+ if ocr_enabled is not None:+ kwargs["ocr_enabled"] = ocr_enabled+ if ocr_server_url is not None:+ kwargs["ocr_server_url"] = ocr_server_url+ if ocr_language is not None:+ kwargs["ocr_language"] = ocr_language+ if tessdata_path is not None:+ kwargs["tessdata_path"] = tessdata_path+ if max_pages is not None:+ kwargs["max_pages"] = max_pages+ if target_pages is not None:+ kwargs["target_pages"] = target_pages+ if dpi is not None:+ kwargs["dpi"] = dpi+ if preserve_very_small_text is not None:+ kwargs["preserve_very_small_text"] = preserve_very_small_text+ if password is not None:+ kwargs["password"] = password+ if quiet is not None:+ kwargs["quiet"] = quiet++ self._native = _NativeLiteParse(**kwargs)- file_path, file_bytes = self._extract_path_and_bytes(file_data)-- # Build command- cmd = self._prepare_command(- "parse",- file_path,- ocr_enabled=ocr_enabled,- ocr_server_url=ocr_server_url,- ocr_language=ocr_language,- num_workers=num_workers,- max_pages=max_pages,- target_pages=target_pages,- dpi=dpi,- precise_bounding_box=precise_bounding_box,- preserve_very_small_text=preserve_very_small_text,- password=password,- )-- try:- result = subprocess.run(- cmd,- capture_output=True,- timeout=timeout,- check=False,- input=file_bytes,- )-- return self._get_parse_result(- result.returncode, result.stdout, result.stderr- )-- except subprocess.TimeoutExpired:- raise TimeoutError(f"Parsing timed out after {timeout} seconds")-- async def parse_async(+ def parse(self,file_data: Union[str, Path, bytes],- *,- ocr_enabled: bool = True,- ocr_server_url: Optional[str] = None,- ocr_language: str = "en",- num_workers: Optional[int] = None,- max_pages: int = 10000,- target_pages: Optional[str] = None,- dpi: int = 150,- precise_bounding_box: bool = True,- preserve_very_small_text: bool = False,- password: Optional[str] = None,- timeout: Optional[float] = None,) -> ParseResult:"""- Parse a document file (asynchronously).-- Args:- file_path: Path to the document file (PDF, DOCX, images, etc.)- file_bytes: Bytes content of the file- ocr_enabled: Whether to enable OCR for scanned documents- ocr_server_url: URL of HTTP OCR server (uses Tesseract if not provided)- ocr_language: Language code for OCR (e.g., "en", "fr", "de")- num_workers: Number of pages to OCR in parallel (defaults to CPU cores - 1)- max_pages: Maximum number of pages to parse- target_pages: Specific pages to parse (e.g., "1-5,10,15-20")- dpi: DPI for rendering (affects OCR quality)- precise_bounding_box: Whether to compute precise bounding boxes- preserve_very_small_text: Whether to preserve very small text- password: Password for encrypted/protected documents- timeout: Timeout in seconds (None for no timeout)-- Returns:- ParseResult containing the parsed document data-- Raises:- ParseError: If parsing fails- FileNotFoundError: If the file doesn't exist- TimeoutError: If parsing times out- """- file_path, file_bytes = self._extract_path_and_bytes(file_data)-- # Build command- cmd = self._prepare_command(- "parse",- file_path,- ocr_enabled=ocr_enabled,- ocr_server_url=ocr_server_url,- ocr_language=ocr_language,- num_workers=num_workers,- max_pages=max_pages,- target_pages=target_pages,- dpi=dpi,- precise_bounding_box=precise_bounding_box,- preserve_very_small_text=preserve_very_small_text,- password=password,- )-- try:- process = await asyncio.subprocess.create_subprocess_exec(- cmd[0],- *cmd[1:],- stdin=asyncio.subprocess.PIPE,- stdout=asyncio.subprocess.PIPE,- stderr=asyncio.subprocess.PIPE,- )- stdout, stderr = await asyncio.wait_for(- process.communicate(input=file_bytes), timeout=timeout- )-- return self._get_parse_result(cast(int, process.returncode), stdout, stderr)-- except TimeoutError:- raise TimeoutError(f"Parsing timed out after {timeout} seconds")-- def batch_parse(- self,- input_dir: Union[str, Path],- output_dir: Union[str, Path],- *,- output_format: Union[OutputFormat, str] = OutputFormat.TEXT,- ocr_enabled: bool = True,- ocr_server_url: Optional[str] = None,- ocr_language: str = "en",- num_workers: Optional[int] = None,- max_pages: int = 10000,- dpi: int = 150,- precise_bounding_box: bool = True,- recursive: bool = False,- extension_filter: Optional[str] = None,- password: Optional[str] = None,- timeout: Optional[float] = None,- ) -> BatchResult:- """- Parse multiple documents in batch mode.-- This is more efficient than calling parse() multiple times because- it reuses the PDF engine across files, avoiding cold-start overhead.+ Parse a document file.Args:- input_dir: Directory containing documents to parse- output_dir: Directory to write output files- output_format: Output format ("json" or "text")- ocr_enabled: Whether to enable OCR for scanned documents- ocr_server_url: URL of HTTP OCR server (uses Tesseract if not provided)- ocr_language: Language code for OCR- num_workers: Number of pages to OCR in parallel (defaults to CPU cores - 1)- max_pages: Maximum number of pages to parse per file- dpi: DPI for rendering- precise_bounding_box: Whether to compute precise bounding boxes- recursive: Whether to recursively search subdirectories- extension_filter: Only process files with this extension (e.g., ".pdf")- password: Password for encrypted/protected documents (applied to all files)- timeout: Timeout in seconds for the entire batch+ file_data: Path to the document file, or raw PDF bytes.Returns:- BatchResult with output directory path+ ParseResult containing the parsed document data.Raises:- FileNotFoundError: If the input directory doesn't exist- TimeoutError: If the batch operation times out+ ParseError: If parsing fails.+ FileNotFoundError: If the file doesn't exist."""-- input_dir, output_dir, output_format = self._extract_batch_params(- input_dir, output_dir, output_format- )-- # Build command- cmd = self._prepare_command(- "batch-parse",- str(input_dir.absolute()),- str(output_dir.absolute()),- output_format=output_format,- ocr_enabled=ocr_enabled,- ocr_server_url=ocr_server_url,- ocr_language=ocr_language,- num_workers=num_workers,- max_pages=max_pages,- dpi=dpi,- precise_bounding_box=precise_bounding_box,- recursive=recursive,- extension_filter=extension_filter,- password=password,- )-try:- subprocess.run(- cmd,- capture_output=True,- text=True,- timeout=timeout,- check=False,- )-- return BatchResult(output_dir=str(output_dir))-- except subprocess.TimeoutExpired:- raise TimeoutError(f"Batch parsing timed out after {timeout} seconds")-- async def batch_parse_async(- self,- input_dir: Union[str, Path],- output_dir: Union[str, Path],- *,- output_format: Union[OutputFormat, str] = OutputFormat.TEXT,- ocr_enabled: bool = True,- ocr_server_url: Optional[str] = None,- ocr_language: str = "en",- num_workers: Optional[int] = None,- max_pages: int = 10000,- dpi: int = 150,- precise_bounding_box: bool = True,- recursive: bool = False,- extension_filter: Optional[str] = None,- password: Optional[str] = None,- timeout: Optional[float] = None,- ) -> BatchResult:- """- Parse multiple documents in batch mode (asynchronously).-- This is more efficient than calling parse() multiple times because- it reuses the PDF engine across files, avoiding cold-start overhead.-- Args:- input_dir: Directory containing documents to parse- output_dir: Directory to write output files- output_format: Output format ("json" or "text")- ocr_enabled: Whether to enable OCR for scanned documents- ocr_server_url: URL of HTTP OCR server (uses Tesseract if not provided)- ocr_language: Language code for OCR- num_workers: Number of pages to OCR in parallel (defaults to CPU cores - 1)- max_pages: Maximum number of pages to parse per file- dpi: DPI for rendering- precise_bounding_box: Whether to compute precise bounding boxes- recursive: Whether to recursively search subdirectories- extension_filter: Only process files with this extension (e.g., ".pdf")- password: Password for encrypted/protected documents (applied to all files)- timeout: Timeout in seconds for the entire batch-- Returns:- BatchResult with output directory path-- Raises:- FileNotFoundError: If the input directory doesn't exist- TimeoutError: If the batch operation times out- """- input_dir, output_dir, output_format = self._extract_batch_params(- input_dir, output_dir, output_format- )-- # Build command- cmd = self._prepare_command(- "batch-parse",- str(input_dir.absolute()),- str(output_dir.absolute()),- output_format=output_format,- ocr_enabled=ocr_enabled,- ocr_server_url=ocr_server_url,- ocr_language=ocr_language,- num_workers=num_workers,- max_pages=max_pages,- dpi=dpi,- precise_bounding_box=precise_bounding_box,- recursive=recursive,- extension_filter=extension_filter,- password=password,- )-- try:- process = await asyncio.subprocess.create_subprocess_exec(- cmd[0],- *cmd[1:],- )- await asyncio.wait_for(process.wait(), timeout=timeout)-- return BatchResult(output_dir=str(output_dir))-- except TimeoutError:- raise TimeoutError(f"Batch parsing timed out after {timeout} seconds")+ if isinstance(file_data, bytes):+ native_result = self._native.parse_bytes(file_data)+ else:+ file_path = Path(file_data)+ if not file_path.exists():+ raise FileNotFoundError(f"File not found: {file_path}")+ native_result = self._native.parse(str(file_path.absolute()))+ return _convert_native_result(native_result)+ except FileNotFoundError:+ raise+ except Exception as e:+ raise ParseError(str(e)) from edef screenshot(self,file_path: Union[str, Path],- output_dir: Optional[Union[str, Path]] = None,*,- target_pages: Optional[str] = None,- dpi: int = 150,- image_format: Union[ImageFormat, str] = ImageFormat.PNG,- password: Optional[str] = None,- load_bytes: bool = False,- timeout: Optional[float] = None,- ) -> ScreenshotBatchResult:+ page_numbers: Optional[List[int]] = None,+ ) -> List[ScreenshotResult]:"""Generate screenshots of document pages.Args:- file_path: Path to the document file- output_dir: Directory to save screenshots (uses temp dir if not provided)- target_pages: Specific pages to screenshot (e.g., "1,3,5" or "1-5")- dpi: DPI for rendering- image_format: Image format ("png" or "jpg")- password: Password for encrypted/protected documents- load_bytes: If True, load image bytes into ScreenshotResult objects- timeout: Timeout in seconds-- Returns:- ScreenshotBatchResult containing paths to generated screenshots-- Raises:- FileNotFoundError: If the file doesn't exist- TimeoutError: If the operation times out- """- file_path, image_format, output_dir = self._extract_screenshot_params(- file_path, image_format, output_dir- )-- # Build command- cmd = self._prepare_command(- "screenshot",- str(file_path.absolute()),- "-o",- str(output_dir.absolute()),- "--format",- image_format.value,- "--dpi",- str(dpi),- "-q",- )-- if target_pages:- cmd.extend(["--target-pages", target_pages])-- if password:- cmd.extend(["--password", password])-- try:- result = subprocess.run(- cmd,- capture_output=True,- text=True,- timeout=timeout,- check=False,- )-- return self._get_screenshot_result(- result.returncode,- result.stderr,- output_dir,- image_format,- load_bytes,- )-- except subprocess.TimeoutExpired:- raise TimeoutError(- f"Screenshot generation timed out after {timeout} seconds"- )-- async def screenshot_async(- self,- file_path: Union[str, Path],- output_dir: Optional[Union[str, Path]] = None,- *,- target_pages: Optional[str] = None,- dpi: int = 150,- image_format: Union[ImageFormat, str] = ImageFormat.PNG,- password: Optional[str] = None,- load_bytes: bool = False,- timeout: Optional[float] = None,- ) -> ScreenshotBatchResult:- """- Generate screenshots of document pages (asynchronously).-- Args:- file_path: Path to the document file- output_dir: Directory to save screenshots (uses temp dir if not provided)- target_pages: Specific pages to screenshot (e.g., "1,3,5" or "1-5")- dpi: DPI for rendering- image_format: Image format ("png" or "jpg")- password: Password for encrypted/protected documents- load_bytes: If True, load image bytes into ScreenshotResult objects- timeout: Timeout in seconds+ file_path: Path to the document file.+ page_numbers: Specific page numbers to screenshot (1-indexed).+ If None, screenshots all pages.Returns:- ScreenshotBatchResult containing paths to generated screenshots+ List of ScreenshotResult with PNG image bytes.Raises:- FileNotFoundError: If the file doesn't exist- TimeoutError: If the operation times out+ FileNotFoundError: If the file doesn't exist.+ ParseError: If screenshot generation fails."""- file_path, image_format, output_dir = self._extract_screenshot_params(- file_path, image_format, output_dir- )-- # Build command- cmd = self._prepare_command(- "screenshot",- str(file_path.absolute()),- "-o",- str(output_dir.absolute()),- "--format",- image_format.value,- "--dpi",- str(dpi),- "-q",- )-- if target_pages:- cmd.extend(["--target-pages", target_pages])-- if password:- cmd.extend(["--password", password])+ file_path = Path(file_path)+ if not file_path.exists():+ raise FileNotFoundError(f"File not found: {file_path}")try:- process = await asyncio.subprocess.create_subprocess_exec(cmd[0], *cmd[1:])-- _, stderr = await asyncio.wait_for(process.communicate(), timeout=timeout)-- return self._get_screenshot_result(- cast(int, process.returncode),- (stderr or b"").decode("utf-8"),- output_dir,- image_format,- load_bytes,- )-- except TimeoutError:- raise TimeoutError(- f"Screenshot generation timed out after {timeout} seconds"- )+ native_results = self._native.screenshot(+ str(file_path.absolute()),+ page_numbers,+ )+ return [+ ScreenshotResult(+ page_num=r.page_num,+ width=r.width,+ height=r.height,+ image_bytes=r.image_bytes,+ )+ for r in native_results+ ]+ except Exception as e:+ raise ParseError(str(e)) from edef __repr__(self) -> str:- return f"LiteParse(cli_path={self._cli_path!r})"+ return "LiteParse()"diff --git a/packages/python/liteparse/types.py b/packages/python/liteparse/types.pyindex 9952ade..064e6fb 100644--- a/packages/python/liteparse/types.py+++ b/packages/python/liteparse/types.py@@ -1,29 +1,9 @@+"""Python-friendly type wrappers around the native Rust bindings."""+from __future__ import annotationsfrom dataclasses import dataclass, field-from enum import Enum-from typing import List, Optional, Any, Dict, Iterator---class OutputFormat(str, Enum):- """Output format options."""- JSON = "json"- TEXT = "text"---class ImageFormat(str, Enum):- """Image format options for screenshots."""- PNG = "png"- JPG = "jpg"---@dataclass-class BoundingBox:- """Bounding box coordinates."""- x1: float- y1: float- x2: float- y2: float+from typing import Iterator, List, Optional@dataclass@@ -34,20 +14,19 @@ class TextItem:y: floatwidth: floatheight: float+ font_name: Optional[str] = None+ font_size: Optional[float] = Noneconfidence: Optional[float] = None- fontName: Optional[str] = None- fontSize: Optional[float] = None@dataclassclass ParsedPage:"""A parsed page from a document."""- pageNum: int+ page_num: intwidth: floatheight: floattext: str- textItems: List[TextItem] = field(default_factory=list)- boundingBoxes: List[BoundingBox] = field(default_factory=list)+ text_items: List[TextItem] = field(default_factory=list)@dataclass@@ -55,62 +34,28 @@ class ParseResult:"""Result of parsing a document."""pages: List[ParsedPage]text: str- json: Optional[Dict[str, Any]] = None@propertydef num_pages(self) -> int:- """Number of pages in the document."""return len(self.pages)def get_page(self, page_num: int) -> Optional[ParsedPage]:"""Get a specific page by number (1-indexed)."""for page in self.pages:- if page.pageNum == page_num:+ if page.page_num == page_num:return pagereturn None-@dataclass-class BatchResult:- """Result of batch parsing."""- output_dir: str--@dataclassclass ScreenshotResult:"""Result of a single page screenshot."""page_num: int- image_path: str- image_bytes: Optional[bytes] = None---@dataclass-class ScreenshotBatchResult:- """Result of screenshot operation."""- screenshots: List[ScreenshotResult]- output_dir: str-- def __len__(self) -> int:- return len(self.screenshots)-- def __iter__(self) -> Iterator[ScreenshotResult]:- return iter(self.screenshots)-- def get_page(self, page_num: int) -> Optional[ScreenshotResult]:- """Get screenshot for a specific page."""- for s in self.screenshots:- if s.page_num == page_num:- return s- return None+ width: int+ height: int+ image_bytes: bytesclass ParseError(Exception):"""Exception raised when parsing fails."""- def __init__(self, message: str, stderr: Optional[str] = None):- super().__init__(message)- self.stderr = stderr---class CLINotFoundError(Exception):- """Exception raised when the liteparse CLI is not found."""passdiff --git a/packages/python/pyproject.toml b/packages/python/pyproject.tomlindex 0c1c4c8..dafc031 100644--- a/packages/python/pyproject.toml+++ b/packages/python/pyproject.toml@@ -1,11 +1,11 @@[build-system]-requires = ["hatchling"]-build-backend = "hatchling.build"+requires = ["maturin>=1.0,<2.0"]+build-backend = "maturin"[project]name = "liteparse"-version = "1.2.1"-description = "Python wrapper for LiteParse - fast, lightweight PDF and document parsing"+version = "2.0.0"+description = "Python bindings for LiteParse - fast, lightweight PDF and document parsing"readme = "README.md"license = "MIT"requires-python = ">=3.10"@@ -23,6 +23,7 @@ classifiers = ["Programming Language :: Python :: 3.12","Programming Language :: Python :: 3.13","Programming Language :: Python :: 3.14",+ "Programming Language :: Rust","Topic :: Text Processing","Topic :: Scientific/Engineering :: Information Analysis","Typing :: Typed",@@ -33,6 +34,9 @@ Homepage = "https://github.com/run-llama/liteparse"Documentation = "https://github.com/run-llama/liteparse#readme"Repository = "https://github.com/run-llama/liteparse"+[project.scripts]+lit = "liteparse.cli:main"+[project.optional-dependencies]dev = ["pytest>=7.0",@@ -40,8 +44,16 @@ dev = ["mypy>=1.0",]-[tool.hatch.build.targets.wheel]-packages = ["liteparse"]+[tool.maturin]+manifest-path = "../../crates/liteparse-python/Cargo.toml"+python-source = "."+module-name = "liteparse._liteparse"+# Bundle libpdfium alongside the extension module+include = [+ { path = "liteparse/libpdfium.dylib", format = "wheel" },+ { path = "liteparse/libpdfium.so", format = "wheel" },+ { path = "liteparse/pdfium.dll", format = "wheel" },+][tool.mypy]python_version = "3.10"diff --git a/packages/python/scripts/copy-pdfium.sh b/packages/python/scripts/copy-pdfium.shnew file mode 100755index 0000000..4a94c87--- /dev/null+++ b/packages/python/scripts/copy-pdfium.sh@@ -0,0 +1,77 @@+#!/usr/bin/env bash+#+# Copy the pdfium shared library into the liteparse package directory+# so it can be found at runtime via @loader_path (macOS) or $ORIGIN (Linux).+#+# Usage: ./scripts/copy-pdfium.sh+#+# The script auto-detects the pdfium library location from:+# 1. PDFIUM_LIB_PATH env var (set by CI or user)+# 2. The pdfium-sys build cache (~/.cache/pdfium-rs or ~/Library/Caches/pdfium-rs)++set -euo pipefail++SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"+OUTPUT_DIR="${SCRIPT_DIR}/../liteparse"++# Determine OS and library filename+case "$(uname -s)" in+ Darwin*) DYLIB="libpdfium.dylib" ;;+ Linux*) DYLIB="libpdfium.so" ;;+ MINGW*|MSYS*|CYGWIN*) DYLIB="pdfium.dll" ;;+ *) echo "Unsupported OS: $(uname -s)" >&2; exit 1 ;;+esac++# Find the pdfium library+find_pdfium() {+ # 1. Explicit env var+ if [ -n "${PDFIUM_LIB_PATH:-}" ] && [ -f "${PDFIUM_LIB_PATH}/${DYLIB}" ]; then+ echo "${PDFIUM_LIB_PATH}/${DYLIB}"+ return+ fi++ # 2. Vendor directory+ local vendor="${SCRIPT_DIR}/../../vendor/pdfium/release/lib/${DYLIB}"+ if [ -f "$vendor" ]; then+ echo "$vendor"+ return+ fi++ # 3. Cargo build output (debug and release)+ local workspace_root="${SCRIPT_DIR}/../../../.."+ for profile in release debug; do+ local deps="${workspace_root}/target/${profile}/deps/${DYLIB}"+ if [ -f "$deps" ]; then+ echo "$deps"+ return+ fi+ done++ # 4. Build cache+ local cache_base+ case "$(uname -s)" in+ Darwin*) cache_base="$HOME/Library/Caches/pdfium-rs" ;;+ *) cache_base="${XDG_CACHE_HOME:-$HOME/.cache}/pdfium-rs" ;;+ esac++ if [ -d "$cache_base" ]; then+ local found+ found=$(find "$cache_base" -name "$DYLIB" -type f 2>/dev/null | head -1)+ if [ -n "$found" ]; then+ echo "$found"+ return+ fi+ fi++ echo ""+}++PDFIUM_PATH=$(find_pdfium)++if [ -z "$PDFIUM_PATH" ]; then+ echo "Error: Could not find ${DYLIB}. Set PDFIUM_LIB_PATH to the directory containing it." >&2+ exit 1+fi++cp "$PDFIUM_PATH" "${OUTPUT_DIR}/${DYLIB}"+echo "Copied ${PDFIUM_PATH} -> ${OUTPUT_DIR}/${DYLIB}"diff --git a/packages/python/uv.lock b/packages/python/uv.lockindex 730b3ad..4b1a41e 100644--- a/packages/python/uv.lock+++ b/packages/python/uv.lock@@ -128,7 +128,7 @@ wheels = [[[package]]name = "liteparse"-version = "1.2.1"+version = "2.0.0"source = { editable = "." }[package.optional-dependencies]