### Install img2table with OCR Support Source: https://github.com/xavctn/img2table/blob/main/README.md Installs the img2table library with optional support for various OCR (Optical Character Recognition) engines. Choose the installation command based on the desired OCR engine for text extraction from images. ```bash pip install img2table pip install img2table[paddle] pip install img2table[easyocr] pip install img2table[surya] pip install img2table[gcp] pip install img2table[aws] pip install img2table[azure] ``` -------------------------------- ### Configure Tesseract OCR with Python Source: https://github.com/xavctn/img2table/blob/main/README.md Shows how to initialize the Tesseract OCR engine for text extraction. This includes setting the number of threads, language, page segmentation mode (PSM), and the directory for Tesseract's trained data files. Tesseract OCR requires prior installation. ```python from img2table.ocr import TesseractOCR ocr = TesseractOCR(n_threads=1, lang="eng", psm=11, tessdata_dir="...") ``` -------------------------------- ### Install img2table and PaddleOCR dependencies Source: https://github.com/xavctn/img2table/blob/main/README.md Commands to install the necessary packages for img2table and PaddleOCR, including GPU support via CUDA 11.8. ```bash pip install paddlepaddle-gpu==2.5.0rc1.post118 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html pip install paddleocr img2table ``` -------------------------------- ### Configure PaddleOCR with Python Source: https://github.com/xavctn/img2table/blob/main/README.md Illustrates the initialization of PaddleOCR for text recognition. Users can specify the language for text extraction and pass additional keyword arguments to the PaddleOCR constructor. Language models are downloaded on first use. For GPU usage, a specific CUDA version of paddlepaddle-gpu must be installed manually. ```python from img2table.ocr import PaddleOCR ocr = PaddleOCR(lang="en", kw={"kwarg": kw_value, ...}) ``` -------------------------------- ### Initialize docTR for img2table Source: https://github.com/xavctn/img2table/blob/main/README.md Initializes the docTR OCR engine. Requires prior installation of docTR and supports language detection toggling. ```python from img2table.ocr import DocTR ocr = DocTR(detect_language=False, kw={"kwarg": kw_value}) ``` -------------------------------- ### Implicit Row and Column Detection in img2table Source: https://context7.com/xavctn/img2table/llms.txt Shows how to enable implicit row and column detection to split cells containing multiple logical rows or columns based on text positioning. This example uses TesseractOCR to process an image and compares the results with and without implicit detection. ```python from img2table.document import Image from img2table.ocr import TesseractOCR ocr = TesseractOCR() image = Image("complex_table.png") # Without implicit detection tables_basic = image.extract_tables(ocr=ocr) print(f"Basic: {tables_basic[0].df.shape}") # With implicit row/column detection tables_implicit = image.extract_tables( ocr=ocr, implicit_rows=True, # Split cells with multiple text lines implicit_columns=True # Split cells with separated text blocks ) print(f"Implicit: {tables_implicit[0].df.shape}") ``` -------------------------------- ### Complete img2table Extraction Pipeline for PDFs Source: https://context7.com/xavctn/img2table/llms.txt A comprehensive example demonstrating the full extraction pipeline using img2table for PDF documents. It initializes TesseractOCR and a PDF document object, extracts all tables with advanced options, processes the results into a list of dictionaries, saves individual tables to CSV, and exports all tables to a single Excel file. ```python from img2table.document import PDF from img2table.ocr import TesseractOCR import pandas as pd # Initialize components ocr = TesseractOCR(n_threads=4, lang="eng") pdf = PDF( src="financial_report.pdf", pages=None, # Process all pages detect_rotation=True, pdf_text_extraction=True ) # Extract all tables all_tables = pdf.extract_tables( ocr=ocr, implicit_rows=True, implicit_columns=True, borderless_tables=True, min_confidence=60 ) # Process extracted tables results = [] for page_num, tables in all_tables.items(): for table_idx, table in enumerate(tables): df = table.df results.append({ "page": page_num + 1, "table": table_idx + 1, "title": table.title, "rows": df.shape[0], "columns": df.shape[1], "data": df }) # Save individual table df.to_csv(f"page{page_num + 1}_table{table_idx + 1}.csv", index=False) print(f"Extracted {len(results)} tables from {len(all_tables)} pages") # Export all to single Excel workbook pdf.to_xlsx(dest="all_tables.xlsx", ocr=ocr) ``` -------------------------------- ### docTR Integration Source: https://github.com/xavctn/img2table/blob/main/README.md Integrates docTR for OCR. Requires prior installation. Supports language detection and custom keyword arguments for the OCR predictor. ```APIDOC ## docTR Integration ### Description Integrates docTR for OCR. Requires prior installation. Supports language detection and custom keyword arguments for the OCR predictor. ### Method Instantiation of OCR object ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from img2table.ocr import DocTR ocr = DocTR(detect_language=False, kw={"kwarg": kw_value, ...}) ``` ### Response #### Success Response (200) N/A #### Response Example N/A ### Parameters - **detect_language** (bool, optional, default `False`) - Parameter indicating if language prediction is run on the document - **kw** (dict, optional, default `None`) - Dictionary containing additional keyword arguments passed to the docTR `ocr_predictor` method. ``` -------------------------------- ### Extract Tables with and without Implicit Rows Source: https://github.com/xavctn/img2table/blob/main/examples/Implicit.ipynb Extracts tables from the image using the img2table library. It demonstrates the difference in output when the `implicit_rows` parameter is set to `False` versus `True`. The `implicit_columns` parameter is kept `False` in this example. ```python # Extract tables without implicit rows extracted_tables = img.extract_tables(ocr=ocr, implicit_rows=False, implicit_columns=False) table = extracted_tables.pop() # Extract tables with implicit rows extracted_tables_implicit = img.extract_tables(ocr=ocr, implicit_rows=True, implicit_columns=False) table_implicit_rows = extracted_tables_implicit.pop() ``` -------------------------------- ### Configure EasyOCR Backend Source: https://context7.com/xavctn/img2table/llms.txt Initializes the EasyOCR engine, which supports over 80 languages and provides simple configuration for GPU usage and verbosity. ```python from img2table.ocr import EasyOCR # With custom options ocr = EasyOCR( lang=["en", "ch_sim"], kw={ "gpu": True, "verbose": False } ) ``` -------------------------------- ### Instantiate PDF Document with Python Source: https://github.com/xavctn/img2table/blob/main/README.md Demonstrates how to create a PDF document object from a source file. It allows specifying pages to process, whether to detect rotation, and if text extraction should be performed for native PDFs. The PDF is converted to images at 200 DPI for table identification. ```python from img2table.document import PDF pdf = PDF(src, pages=[0, 2], detect_rotation=False, pdf_text_extraction=True) ``` -------------------------------- ### Load and Process Image Files with Image Class Source: https://context7.com/xavctn/img2table/llms.txt Demonstrates how to initialize the Image class using various sources such as file paths, byte streams, and BytesIO buffers. Includes options for automatic rotation detection to handle skewed documents. ```python from img2table.document import Image import io # Load image from file path image = Image(src="path/to/document.png") # Load image from bytes with open("document.jpg", "rb") as f: image = Image(src=f.read()) # Load with rotation detection enabled image = Image(src="skewed_document.png", detect_rotation=True) # Load from BytesIO buffer buffer = io.BytesIO(image_bytes) image = Image(src=buffer) ``` -------------------------------- ### Configure OCR Backends Source: https://context7.com/xavctn/img2table/llms.txt Initialize various OCR engines including Surya, Google Cloud Vision, AWS Textract, and Azure Cognitive Services. These engines are required to process document text before table extraction. ```python from img2table.ocr import SuryaOCR, VisionOCR, TextractOCR, AzureOCR import os # Surya OCR ocr_surya = SuryaOCR(langs=["en", "fr", "de", "es"]) # Google Vision ocr_vision = VisionOCR(api_key="your-key", timeout=15) # AWS Textract ocr_textract = TextractOCR(aws_access_key_id="key", aws_secret_access_key="secret", region="us-east-1") # Azure OCR ocr_azure = AzureOCR(endpoint="https://...", subscription_key="key") ``` -------------------------------- ### POST /document/initialize Source: https://github.com/xavctn/img2table/blob/main/examples/Basic_usage.ipynb Initializes a document source from a file path, bytes, or file-like object for processing. ```APIDOC ## POST /document/initialize ### Description Creates a document object (Image or PDF) from a source to prepare it for table extraction. ### Method POST ### Endpoint /document/initialize ### Parameters #### Request Body - **type** (string) - Required - The document type ('image' or 'pdf'). - **src** (string/bytes) - Required - The file path, raw bytes, or file-like object. - **pages** (list) - Optional - List of page indices to process (for PDF only). ### Request Example { "type": "pdf", "src": "data/tables.pdf", "pages": [0, 1] } ### Response #### Success Response (200) - **document_id** (string) - Unique identifier for the initialized document object. ``` -------------------------------- ### Configure TesseractOCR Backend Source: https://context7.com/xavctn/img2table/llms.txt Initializes the Tesseract OCR engine with custom configuration parameters such as thread count, language settings, and page segmentation modes. ```python from img2table.ocr import TesseractOCR # Basic initialization ocr = TesseractOCR() # Full configuration ocr = TesseractOCR( n_threads=4, lang="eng+fra", psm=11, tessdata_dir="/usr/share/tesseract-ocr/4.00/tessdata" ) ``` -------------------------------- ### Configure SuryaOCR Backend Source: https://context7.com/xavctn/img2table/llms.txt Initializes the SuryaOCR engine for modern multilingual text recognition. ```python from img2table.ocr import SuryaOCR # English OCR ocr = SuryaOCR(langs=["en"]) ``` -------------------------------- ### Configure DocTR Backend Source: https://context7.com/xavctn/img2table/llms.txt Initializes the DocTR library for document text recognition, allowing for custom detection and recognition architecture selection. ```python from img2table.ocr import DocTR # With custom model options ocr = DocTR( detect_language=False, kw={ "pretrained": True, "det_arch": "db_resnet50", "reco_arch": "crnn_vgg16_bn" } ) ``` -------------------------------- ### Configure PaddleOCR Backend Source: https://context7.com/xavctn/img2table/llms.txt Initializes the PaddleOCR engine for deep learning-based text recognition, including support for GPU acceleration and language selection. ```python from img2table.ocr import PaddleOCR # Basic English OCR ocr = PaddleOCR(lang="en") # With additional PaddleOCR options ocr = PaddleOCR( lang="en", kw={ "use_angle_cls": True, "use_gpu": True, "show_log": False } ) ``` -------------------------------- ### Initialize EasyOCR for img2table Source: https://github.com/xavctn/img2table/blob/main/README.md Initializes the EasyOCR engine. It accepts a list of languages and optional keyword arguments for the Reader constructor. ```python from img2table.ocr import EasyOCR ocr = EasyOCR(lang=["en"], kw={"kwarg": kw_value}) ``` -------------------------------- ### Configure OCR Instances for Table Extraction with img2table Source: https://github.com/xavctn/img2table/blob/main/examples/Basic_usage.ipynb Shows how to initialize and configure various OCR (Optical Character Recognition) tools supported by img2table. These OCR instances are crucial for inferring cell content during table extraction. ```python # Tesseract OCR from img2table.ocr import TesseractOCR tesseract_ocr = TesseractOCR(n_threads=1, lang="eng") # PaddleOCR from img2table.ocr import PaddleOCR paddle_ocr = PaddleOCR(lang="en", kw={"use_dilation": True}) # EasyOCR from img2table.ocr import EasyOCR easyocr = EasyOCR(lang=["en"], kw={"gpu": False}) # docTR from img2table.ocr import DocTR doctr = DocTR(detect_language=True, kw={"detect_orientation": True}) # Google Vision OCR from img2table.ocr import VisionOCR vision_ocr = VisionOCR(api_key="***") # AWS Textract OCR from img2table.ocr import TextractOCR textract_ocr = TextractOCR(aws_access_key_id="***", aws_secret_access_key="***", aws_session_token="***", region="eu-west-1") # Azure Cognitive Services OCR from img2table.ocr import AzureOCR azure_ocr = AzureOCR(endpoint="abc.azure.com", subscription_key="***") ``` -------------------------------- ### Initialize Azure Cognitive Services OCR for img2table Source: https://github.com/xavctn/img2table/blob/main/README.md Initializes the Azure Cognitive Services OCR engine using an endpoint and subscription key. ```python from img2table.ocr import AzureOCR ocr = AzureOCR(endpoint="abc.azure.com", subscription_key="***") ``` -------------------------------- ### Initialize Surya OCR for img2table Source: https://github.com/xavctn/img2table/blob/main/README.md Initializes the Surya OCR engine, which requires Python 3.10 or higher. It automatically downloads models on the first use. ```python from img2table.ocr import SuryaOCR ocr = SuryaOCR(langs=["en"]) ``` -------------------------------- ### PDF Document Initialization Source: https://github.com/xavctn/img2table/blob/main/README.md Instantiates a PDF document object for table extraction, allowing configuration of page selection and text extraction settings. ```APIDOC ## PDF Document Initialization ### Description Initializes a PDF document for processing. Pages are converted to images at 200 DPI for table identification. ### Parameters - **src** (str/Path/bytes) - Required - The PDF source file. - **pages** (list) - Optional - List of page indexes to process. Defaults to all pages. - **detect_rotation** (bool) - Optional - Whether to detect and correct skew/rotation. Defaults to False. - **pdf_text_extraction** (bool) - Optional - Whether to extract text directly from native PDFs. Defaults to True. ### Request Example ```python from img2table.document import PDF pdf = PDF(src='doc.pdf', pages=[0, 2], detect_rotation=False, pdf_text_extraction=True) ``` ``` -------------------------------- ### Initialize AWS Textract for img2table Source: https://github.com/xavctn/img2table/blob/main/README.md Initializes the AWS Textract OCR engine. Credentials can be passed directly or managed via AWS environment variables. ```python from img2table.ocr import TextractOCR ocr = TextractOCR(aws_access_key_id="***", aws_secret_access_key="***", aws_session_token="***", region="eu-west-1") ``` -------------------------------- ### Initialize OCR and Image Objects Source: https://github.com/xavctn/img2table/blob/main/examples/Implicit.ipynb Initializes the Tesseract OCR engine and creates an Image object from a source file. Tesseract OCR is required for text recognition within the image to identify table elements. ```python # Define OCR instance, requires prior installation of Tesseract-OCR ocr = TesseractOCR() # Define image img = Image(src="data/implicit.png") ``` -------------------------------- ### Initialize Google Vision OCR for img2table Source: https://github.com/xavctn/img2table/blob/main/README.md Initializes the Google Vision OCR engine. Authentication is handled via environment variables or an explicit API key. ```python from img2table.ocr import VisionOCR ocr = VisionOCR(api_key="api_key", timeout=15) ``` -------------------------------- ### Load and Display Image Source: https://github.com/xavctn/img2table/blob/main/examples/Implicit.ipynb Loads an image file using Pillow (PIL) and displays it. This step is typically for previewing the image before table extraction. ```python PILImage.open("data/implicit.png") ``` -------------------------------- ### Initialize Image Document in Python Source: https://github.com/xavctn/img2table/blob/main/README.md Initializes an Image document object from a source. The 'src' parameter can be a file path, bytes, or a BytesIO object. The 'detect_rotation' parameter, when set to True, attempts to correct skew and rotation, but may alter image coordinates. ```python from img2table.document import Image image = Image(src, detect_rotation=False) ``` -------------------------------- ### POST /document/image Source: https://github.com/xavctn/img2table/blob/main/README.md Instantiates an image document for table identification and extraction processing. ```APIDOC ## POST /document/image ### Description Instantiates an image object to be used for table detection and extraction. This is the entry point for processing image-based documents. ### Method POST ### Endpoint /document/image ### Parameters #### Request Body - **src** (str, pathlib.Path, bytes, io.BytesIO) - Required - The source of the image file. - **detect_rotation** (bool) - Optional - If True, detects and corrects skew/rotation of the image up to 45 degrees. Defaults to False. ### Request Example { "src": "path/to/image.png", "detect_rotation": true } ### Response #### Success Response (200) - **image_object** (object) - The initialized image document object ready for extraction methods. #### Response Example { "status": "success", "message": "Image instantiated successfully" } ``` -------------------------------- ### Load Documents from Various Sources with img2table Source: https://github.com/xavctn/img2table/blob/main/examples/Basic_usage.ipynb Demonstrates how to load image and PDF documents into img2table from different sources like file paths, byte streams, and file-like objects. This is the first step in processing documents with the library. ```python from img2table.document import Image img_path = "data/tables.png" # Definition of image from path img_from_path = Image(src=img_path) # Definition of image from bytes with open(img_path, 'rb') as f: img_bytes = f.read() img_from_bytes = Image(src=img_bytes) # Definition of image from file-like object from io import BytesIO img_from_file_like = Image(src=BytesIO(img_bytes)) ``` ```python from img2table.document import PDF pdf_path = "data/tables.pdf" # Definition of PDF from path # The optional pages argument enables the extraction of table on specific pages of the PDF pdf_from_path = PDF(src=pdf_path, pages=[0, 1]) # Definition of PDF from bytes with open(pdf_path, 'rb') as f: pdf_bytes = f.read() pdf_from_bytes = PDF(src=pdf_bytes) # Definition of PDF from file-like object from io import BytesIO pdf_from_file_like = PDF(src=BytesIO(pdf_bytes)) ``` -------------------------------- ### Extract Borderless Tables with PaddleOCR Source: https://context7.com/xavctn/img2table/llms.txt Demonstrates how to extract tables without visible borders from an image using img2table and PaddleOCR. This feature relies on text alignment and whitespace analysis and requires at least 3 columns. It processes an image file and returns extracted tables. ```python from img2table.document import Image from img2table.ocr import PaddleOCR ocr = PaddleOCR(lang="en") image = Image("spreadsheet_screenshot.png") # Enable borderless table detection tables = image.extract_tables( ocr=ocr, borderless_tables=True, # Required for borderless detection min_confidence=50 ) # Borderless tables will be included alongside bordered tables for table in tables: print(f"Table with {len(table.content)} rows") print(table.df) ``` -------------------------------- ### Iterate and Display Borderless Tables from Multiple Images Source: https://github.com/xavctn/img2table/blob/main/examples/borderless.ipynb This code iterates through all image files in a specified directory ('data/borderless') and extracts borderless tables from each. It uses the TesseractOCR and a helper function `display_borderless_tables` to visualize the extracted tables as PIL Images, with each table separated by an HTML horizontal rule. ```python for img_path in os.listdir("data/borderless"): img = Image(f"data/borderless/{img_path}") display_img = display_borderless_tables(img=img, ocr=tesseract) display(PILImage.fromarray(display_img)) display_html("