pdf-invoice-extractor 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ from .client import PDFInvoiceExtractorClient, InvoiceExtractorError
2
+
3
+ __all__ = ["PDFInvoiceExtractorClient", "InvoiceExtractorError"]
@@ -0,0 +1,110 @@
1
+ import base64
2
+ import os
3
+ import requests
4
+ from typing import Any, Dict
5
+
6
+
7
+ class InvoiceExtractorError(Exception):
8
+ """Raised when the PDF Invoice & Receipt Data Extractor API returns an error."""
9
+ pass
10
+
11
+
12
+ class PDFInvoiceExtractorClient:
13
+ """Official Python wrapper for the PDF Invoice & Receipt Data Extractor API on RapidAPI."""
14
+
15
+ def __init__(
16
+ self,
17
+ api_key: str,
18
+ host: str = "://rapidapi.com",
19
+ timeout: int = 30,
20
+ ):
21
+ if not api_key:
22
+ raise ValueError("A valid RapidAPI key is required.")
23
+ self.base_url = f"https://{host}"
24
+ self.timeout = timeout
25
+ self.headers = {
26
+ "x-rapidapi-key": api_key,
27
+ "x-rapidapi-host": host,
28
+ }
29
+
30
+ def _handle_response(self, response: requests.Response) -> Dict[str, Any]:
31
+ try:
32
+ response.raise_for_status()
33
+ return response.json()
34
+ except requests.exceptions.HTTPError as e:
35
+ raise InvoiceExtractorError(
36
+ f"API HTTP Error ({response.status_code}): {response.text}"
37
+ ) from e
38
+ except ValueError as e:
39
+ raise InvoiceExtractorError(f"Invalid JSON response: {response.text}") from e
40
+
41
+ def parse_invoice_text(self, text: str) -> Dict[str, Any]:
42
+ """Extract structured invoice/receipt fields from raw OCR or plain text."""
43
+ if not text or not isinstance(text, str):
44
+ raise ValueError("A valid invoice text string is required.")
45
+
46
+ url = f"{self.base_url}/v1/parse-invoice-text"
47
+ headers = {**self.headers, "Content-Type": "application/json"}
48
+ try:
49
+ response = requests.post(
50
+ url=url,
51
+ headers=headers,
52
+ json={"text": text},
53
+ timeout=self.timeout,
54
+ )
55
+ return self._handle_response(response)
56
+ except requests.exceptions.RequestException as e:
57
+ raise InvoiceExtractorError(f"Network error connecting to RapidAPI: {e}") from e
58
+
59
+ def parse_pdf_url(self, pdf_url: str) -> Dict[str, Any]:
60
+ """Download and extract structured invoice/receipt fields from a public PDF URL."""
61
+ if not pdf_url or not isinstance(pdf_url, str):
62
+ raise ValueError("A valid PDF URL is required.")
63
+
64
+ url = f"{self.base_url}/v1/parse-pdf-url"
65
+ headers = {**self.headers, "Content-Type": "application/json"}
66
+ try:
67
+ response = requests.post(
68
+ url=url,
69
+ headers=headers,
70
+ json={"pdf_url": pdf_url},
71
+ timeout=self.timeout,
72
+ )
73
+ return self._handle_response(response)
74
+ except requests.exceptions.RequestException as e:
75
+ raise InvoiceExtractorError(f"Network error connecting to RapidAPI: {e}") from e
76
+
77
+ def parse_pdf_file(
78
+ self,
79
+ file_path: str,
80
+ as_multipart: bool = True,
81
+ base64_field: str = "file_base64",
82
+ ) -> Dict[str, Any]:
83
+ """Extract structured invoice/receipt data from a local PDF file."""
84
+ if not os.path.isfile(file_path):
85
+ raise FileNotFoundError(f"PDF file not found: {file_path}")
86
+
87
+ url = f"{self.base_url}/v1/parse-pdf-file"
88
+ try:
89
+ if as_multipart:
90
+ with open(file_path, "rb") as f:
91
+ files = {"file": (os.path.basename(file_path), f, "application/pdf")}
92
+ response = requests.post(
93
+ url=url,
94
+ headers=self.headers,
95
+ files=files,
96
+ timeout=self.timeout,
97
+ )
98
+ else:
99
+ with open(file_path, "rb") as f:
100
+ encoded = base64.b64encode(f.read()).decode("utf-8")
101
+ headers = {**self.headers, "Content-Type": "application/json"}
102
+ response = requests.post(
103
+ url=url,
104
+ headers=headers,
105
+ json={base64_field: encoded, "filename": os.path.basename(file_path)},
106
+ timeout=self.timeout,
107
+ )
108
+ return self._handle_response(response)
109
+ except requests.exceptions.RequestException as e:
110
+ raise InvoiceExtractorError(f"Network error connecting to RapidAPI: {e}") from e
@@ -0,0 +1,43 @@
1
+ Metadata-Version: 2.4
2
+ Name: pdf-invoice-extractor
3
+ Version: 0.1.0
4
+ Summary: Python SDK for extracting structured JSON data from PDF invoices, receipts, and URLs via RapidAPI.
5
+ License: MIT
6
+ Project-URL: Homepage, https://rapidapi.com
7
+ Keywords: pdf,invoice,receipt,ocr,parser,accounting,rapidapi
8
+ Requires-Python: >=3.8
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: requests>=2.28.0
11
+
12
+ # PDF Invoice & Receipt Data Extractor (Python SDK)
13
+
14
+ Extract vendor details, invoice numbers, dates, line items, taxes, and totals from PDF files, public PDF URLs, or raw receipt text using the [PDF Invoice & Receipt Data Extractor API](https://rapidapi.com).
15
+
16
+ ## Installation
17
+ ```bash
18
+ pip install pdf-invoice-extractor
19
+ ```
20
+
21
+ ## Quick Start
22
+ 1. Get a free API key from [RapidAPI](https://rapidapi.com).
23
+ 2. Extract structured JSON from text, URLs, or local PDFs:
24
+
25
+ ```python
26
+ from pdf_invoice_extractor import PDFInvoiceExtractorClient
27
+
28
+ client = PDFInvoiceExtractorClient(api_key="YOUR_RAPIDAPI_KEY")
29
+
30
+ # 1. Extract from raw invoice/receipt text
31
+ data = client.parse_invoice_text("""ACME Cloud Services Inc.
32
+ Invoice #INV-2026-8841
33
+ Total Due: \$295.00 USD""")
34
+ print(data)
35
+
36
+ # 2. Extract from a PDF URL
37
+ url_data = client.parse_pdf_url("https://example.com")
38
+ print(url_data)
39
+
40
+ # 3. Extract from a local PDF file
41
+ file_data = client.parse_pdf_file("invoice.pdf")
42
+ print(file_data)
43
+ ```
@@ -0,0 +1,6 @@
1
+ pdf_invoice_extractor/__init__.py,sha256=U6qmCzJKAf0KgCnOFge-mTzzth4o-sAB7QRFtkHmR0c,138
2
+ pdf_invoice_extractor/client.py,sha256=75C5WZm1fqDXskm2Aioidl40E5GAepa9TQ1dVu-SBg4,4424
3
+ pdf_invoice_extractor-0.1.0.dist-info/METADATA,sha256=_GPSxTNttheyFw40Ry_OPqRDapKHzYytMP8WaKiKB7c,1399
4
+ pdf_invoice_extractor-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
5
+ pdf_invoice_extractor-0.1.0.dist-info/top_level.txt,sha256=4z-iwb1Sbf9nJF63C_G7v_ktp6efRiDu2hZuYokPkJM,22
6
+ pdf_invoice_extractor-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ pdf_invoice_extractor