|
| 1 | +from mindee import Client |
| 2 | +from mindee.extraction.pdf_extractor import PdfExtractor |
| 3 | +from mindee.input import PathInput |
| 4 | +from mindee.product import InvoiceSplitterV1, InvoiceV4 |
| 5 | + |
| 6 | +mindee_client = Client(api_key="my-api-key") |
| 7 | +# mindee_client = Client() # Optionally, set from env. |
| 8 | + |
| 9 | + |
| 10 | +def parse_invoice(file_path): |
| 11 | + input_source = PathInput(file_path) |
| 12 | + |
| 13 | + if input_source.is_pdf() and input_source.count_doc_pages() > 1: |
| 14 | + parse_multi_page(input_source) |
| 15 | + else: |
| 16 | + parse_single_page(input_source) |
| 17 | + |
| 18 | + |
| 19 | +def parse_single_page(input_source): |
| 20 | + invoice_result = mindee_client.parse(InvoiceV4, input_source) |
| 21 | + print(invoice_result.document) |
| 22 | + |
| 23 | + |
| 24 | +def parse_multi_page(input_source): |
| 25 | + pdf_extractor = PdfExtractor(input_source) |
| 26 | + invoice_splitter_response = mindee_client.enqueue_and_parse( |
| 27 | + InvoiceSplitterV1, input_source, close_file=False |
| 28 | + ) |
| 29 | + page_groups = ( |
| 30 | + invoice_splitter_response.document.inference.prediction.invoice_page_groups |
| 31 | + ) |
| 32 | + extracted_pdfs = pdf_extractor.extract_invoices(page_groups, strict=False) |
| 33 | + |
| 34 | + for extracted_pdf in extracted_pdfs: |
| 35 | + # Optional: Save the files locally |
| 36 | + # extracted_pdf.write_to_file("output/path") |
| 37 | + |
| 38 | + invoice_result = mindee_client.parse(InvoiceV4, extracted_pdf.as_input_source()) |
| 39 | + print(invoice_result.document) |
| 40 | + |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + parse_invoice("path/to/my/file.ext") |
0 commit comments