feat: add Python bindings via PyO3
Expose the pdf-inspector Rust library as a Python package using PyO3 + maturin. Python users can now `pip install` and use `import pdf_inspector` for PDF classification, text extraction, and markdown conversion with native Rust speed. Adds: - src/python.rs: PyO3 bindings (process_pdf, detect_pdf, extract_text, etc.) - pyproject.toml: maturin build configuration - pdf_inspector.pyi: type stubs for IDE support - tests/test_python.py: 21 pytest tests covering all Python API functions - examples/basic_usage.py: example script demonstrating all features - Updated README with Python quick start and API reference Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
7a0e074fa2
commit
0bf5463a1e
@@ -0,0 +1,76 @@
|
||||
"""Basic usage examples for pdf-inspector Python library."""
|
||||
|
||||
import sys
|
||||
import pdf_inspector
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python basic_usage.py <path-to-pdf>")
|
||||
sys.exit(1)
|
||||
|
||||
path = sys.argv[1]
|
||||
|
||||
# 1. Full processing: detect + extract + markdown
|
||||
print("=" * 60)
|
||||
print("Full processing")
|
||||
print("=" * 60)
|
||||
result = pdf_inspector.process_pdf(path)
|
||||
print(f"Type: {result.pdf_type}")
|
||||
print(f"Pages: {result.page_count}")
|
||||
print(f"Confidence: {result.confidence:.0%}")
|
||||
print(f"Time: {result.processing_time_ms}ms")
|
||||
print(f"Title: {result.title}")
|
||||
print(f"Complex: {result.is_complex_layout}")
|
||||
print(f"Tables on: {result.pages_with_tables}")
|
||||
print(f"Columns on: {result.pages_with_columns}")
|
||||
print(f"Encoding: {'issues detected' if result.has_encoding_issues else 'ok'}")
|
||||
print(f"OCR needed: {result.pages_needing_ocr or 'none'}")
|
||||
if result.markdown:
|
||||
print(f"\n--- Markdown ({len(result.markdown)} chars) ---")
|
||||
print(result.markdown[:500])
|
||||
if len(result.markdown) > 500:
|
||||
print(f"\n... ({len(result.markdown) - 500} more chars)")
|
||||
|
||||
# 2. Fast detection only
|
||||
print("\n" + "=" * 60)
|
||||
print("Detection only")
|
||||
print("=" * 60)
|
||||
info = pdf_inspector.detect_pdf(path)
|
||||
print(f"Type: {info.pdf_type}")
|
||||
print(f"Confidence: {info.confidence:.0%}")
|
||||
print(f"Time: {info.processing_time_ms}ms")
|
||||
|
||||
# 3. From bytes
|
||||
print("\n" + "=" * 60)
|
||||
print("From bytes")
|
||||
print("=" * 60)
|
||||
with open(path, "rb") as f:
|
||||
data = f.read()
|
||||
result = pdf_inspector.process_pdf_bytes(data)
|
||||
print(f"Type: {result.pdf_type}, Pages: {result.page_count}")
|
||||
|
||||
# 4. Plain text
|
||||
print("\n" + "=" * 60)
|
||||
print("Plain text extraction")
|
||||
print("=" * 60)
|
||||
text = pdf_inspector.extract_text(path)
|
||||
print(text[:300])
|
||||
|
||||
# 5. Positioned items
|
||||
print("\n" + "=" * 60)
|
||||
print("Positioned text items (first 10)")
|
||||
print("=" * 60)
|
||||
items = pdf_inspector.extract_text_with_positions(path, pages=[1])
|
||||
for item in items[:10]:
|
||||
bold = " [B]" if item.is_bold else ""
|
||||
italic = " [I]" if item.is_italic else ""
|
||||
print(
|
||||
f" p{item.page} ({item.x:6.1f}, {item.y:6.1f}) "
|
||||
f"size={item.font_size:5.1f}{bold}{italic} "
|
||||
f"'{item.text}'"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user