Files
fil/docs/snippets/python/cli/basic_cli.py
Henrik Jess Nielsen b4c07d3693
All checks were successful
Deploy fil (kreuzberg) / deploy (push) Successful in 49s
Nomad changes
2026-06-01 23:40:55 +02:00

29 lines
774 B
Python

```python title="basic_cli.py"
import subprocess
import json
import sys
from pathlib import Path
def extract_with_cli(file_path: str, output_format: str = "text") -> str:
result = subprocess.run(
["kreuzberg", "extract", file_path, "--format", output_format],
capture_output=True,
text=True,
)
if result.returncode != 0:
print(f"Error: {result.stderr}", file=sys.stderr)
sys.exit(1)
if output_format == "json":
return json.loads(result.stdout)
return result.stdout
document = "document.pdf"
text_output = extract_with_cli(document, "text")
print(f"Extracted: {len(text_output)} characters")
json_output = extract_with_cli(document, "json")
print(f"Format: {json_output.get('format', 'unknown')}")
```