### Specify Page Numbers using Camelot CLI Source: https://github.com/atlanhq/camelot/blob/master/docs/user/quickstart.rst This command-line example demonstrates how to specify which pages of a PDF to extract tables from using the `--pages` argument. ```bash $ camelot --pages 1,2,3 lattice your.pdf ``` -------------------------------- ### Export Tables using Camelot Command-Line Interface Source: https://github.com/atlanhq/camelot/blob/master/docs/user/quickstart.rst This example demonstrates how to use the Camelot command-line interface to extract tables from a PDF and export them as CSV files. It showcases options for specifying the output format, output file, extraction method (lattice), and input PDF file. ```bash $ camelot --format csv --output foo.csv lattice foo.pdf ``` -------------------------------- ### Read Encrypted PDF with Password using Camelot CLI Source: https://github.com/atlanhq/camelot/blob/master/docs/user/quickstart.rst This command-line example demonstrates how to extract tables from an encrypted PDF by providing the password using the `--password` argument. ```bash $ camelot --password userpass lattice foo.pdf ``` -------------------------------- ### Install Camelot from Source using Pip Source: https://github.com/atlanhq/camelot/blob/master/docs/user/install.rst Installs Camelot from its source code. This involves cloning the GitHub repository and then using pip to install the package, optionally including the `[cv]` dependencies. ```bash git clone https://www.github.com/camelot-dev/camelot cd camelot pip install ".[cv]" ``` -------------------------------- ### Install Camelot with Plotting Support Source: https://github.com/atlanhq/camelot/blob/master/HISTORY.md This command installs Camelot along with optional plotting dependencies. It's useful for users who need to visualize extracted tables or perform other plotting-related operations. ```bash pip install camelot-py[plot] ``` -------------------------------- ### Check ghostscript installation on Windows Source: https://github.com/atlanhq/camelot/blob/master/docs/user/install-deps.rst Verifies the ghostscript installation on Windows by checking its version using the appropriate executable. This command is for 64-bit systems; a separate command exists for 32-bit. ```cmd C:\> gswin64c.exe -version ``` ```cmd C:\> gswin32c.exe -version ``` -------------------------------- ### Install Camelot using Pip Source: https://github.com/atlanhq/camelot/blob/master/docs/user/install.rst Installs Camelot using pip after ensuring dependencies like Tkinter and ghostscript are installed. The `[cv]` option installs additional dependencies for computer vision capabilities. ```bash pip install camelot-py[cv] ``` -------------------------------- ### Install Tkinter and ghostscript on Ubuntu Source: https://github.com/atlanhq/camelot/blob/master/docs/user/install-deps.rst Installs the Tkinter library and ghostscript on Ubuntu systems. Tkinter is used for GUI development in Python, and ghostscript is a PostScript and PDF interpreter. These commands use the apt package manager. ```bash $ apt install python-tk ghostscript ``` ```bash $ apt install python3-tk ghostscript ``` -------------------------------- ### Check ghostscript installation on Ubuntu/macOS Source: https://github.com/atlanhq/camelot/blob/master/docs/user/install-deps.rst Checks the ghostscript installation by querying its version. This command is applicable to Ubuntu and macOS systems. A successful execution will display the ghostscript version information. ```bash $ gs -version ``` -------------------------------- ### Run Camelot Tests Source: https://github.com/atlanhq/camelot/blob/master/README.md Executes the tests for the Camelot project using the setup.py script. This is a crucial step for verifying the installation and development environment. ```bash python setup.py test ``` -------------------------------- ### Import Camelot and Read PDF Tables (Python) Source: https://github.com/atlanhq/camelot/blob/master/docs/user/quickstart.rst This snippet shows the basic steps to import the Camelot library and read tables from a PDF file. It uses the default 'lattice' method for table extraction and demonstrates how to access the resulting TableList object. ```python >>> import camelot >>> tables = camelot.read_pdf('foo.pdf') >>> tables ``` -------------------------------- ### Read Encrypted PDF with Password (Python) Source: https://github.com/atlanhq/camelot/blob/master/docs/user/quickstart.rst This code shows how to extract tables from an encrypted PDF by providing the password using the `password` argument in the `read_pdf()` function. It notes that Camelot supports ASCII passwords and specific encryption algorithms. ```python >>> tables = camelot.read_pdf('foo.pdf', password='userpass') >>> tables ``` -------------------------------- ### Export All Tables from PDF using Camelot (Python) Source: https://github.com/atlanhq/camelot/blob/master/docs/user/quickstart.rst This code shows how to export all tables found in a PDF at once using the `export()` method of the TableList object. It allows specifying the output file name and format, with options for CSV, JSON, Excel, HTML, and SQLite. ```python >>> tables.export('foo.csv', f='csv') ``` -------------------------------- ### Adding Chardet Dependency to Camelot Source: https://github.com/atlanhq/camelot/blob/master/HISTORY.md This entry in `setup.py` or `pyproject.toml` (conceptual) shows how to add `chardet` to the `install_requires` list. This ensures that `chardet` is installed automatically when Camelot is installed, resolving issues related to character encoding detection. ```python install_requires=[ ... 'chardet', ... ] ``` -------------------------------- ### Access and Inspect Extracted Table Data (Python) Source: https://github.com/atlanhq/camelot/blob/master/docs/user/quickstart.rst This code demonstrates how to access individual tables from a Camelot TableList object using indexing. It shows how to retrieve the shape of a table and print its parsing report, which includes accuracy and whitespace information. ```python >>> tables[0] >>> print tables[0].parsing_report { 'accuracy': 99.02, 'whitespace': 12.24, 'order': 1, 'page': 1 } ``` -------------------------------- ### Specify Page Numbers for Table Extraction (Python) Source: https://github.com/atlanhq/camelot/blob/master/docs/user/quickstart.rst This snippet shows how to extract tables from specific pages of a PDF by using the `pages` keyword argument in the `read_pdf()` function. It accepts a comma-separated string of page numbers or ranges. ```python >>> camelot.read_pdf('your.pdf', pages='1,2,3') ``` -------------------------------- ### Install Camelot with CV Dependencies Source: https://github.com/atlanhq/camelot/blob/master/README.md Installs the Camelot library along with its computer vision dependencies using pip. This is typically done after cloning the repository. ```bash cd camelot pip install ".[cv]" ``` -------------------------------- ### Install Camelot Development Dependencies Source: https://github.com/atlanhq/camelot/blob/master/README.md Installs the development dependencies for Camelot using pip. This is necessary for contributing to the project or running tests. ```bash pip install camelot-py[dev] ``` -------------------------------- ### Lattice Flavor Options (CLI) Source: https://context7.com/atlanhq/camelot/llms.txt Provides an extensive example of using various command-line options for the 'lattice' flavor in Camelot, covering background processing, scaling, text handling, and tolerance settings. ```bash # Lattice with options camelot lattice \ -back \ -scale 40 \ -copy h -copy v \ -shift l -shift t \ -split \ -flag \ -strip '\n' \ -l 2 \ -j 2 \ -block 15 \ -const -2 \ -I 0 \ -res 300 \ -T 72,730,580,460 \ -R 72,730,580,460 \ -o output.csv -f csv document.pdf ``` -------------------------------- ### Check Tkinter installation Source: https://github.com/atlanhq/camelot/blob/master/docs/user/install-deps.rst Verifies if the Tkinter library is installed and accessible in Python. Importing Tkinter (or tkinter in Python 3) without an ImportError indicates a successful installation. ```python >>> import Tkinter ``` ```python >>> import tkinter ``` -------------------------------- ### Export Table to CSV using Camelot (Python) Source: https://github.com/atlanhq/camelot/blob/master/docs/user/quickstart.rst This snippet illustrates how to export a single extracted table to a CSV file using the `to_csv()` method. It also mentions alternative export methods like `to_json()`, `to_excel()`, `to_html()`, and `to_sqlite()`. ```python >>> tables[0].to_csv('foo.csv') ``` -------------------------------- ### Install Tkinter and ghostscript on macOS Source: https://github.com/atlanhq/camelot/blob/master/docs/user/install-deps.rst Installs Tkinter (via tcl-tk) and ghostscript on macOS using the Homebrew package manager. Tkinter provides Python with GUI capabilities, and ghostscript is necessary for processing PostScript and PDF files. ```bash $ brew install tcl-tk ghostscript ``` -------------------------------- ### Install Camelot using Conda Source: https://github.com/atlanhq/camelot/blob/master/docs/user/install.rst Installs Camelot using the conda package manager from the conda-forge channel. This is the recommended method for users with Anaconda installed. It ensures compatibility with Python 2.7, 3.5, and 3.6 on various operating systems. ```bash conda install -c conda-forge camelot-py ``` -------------------------------- ### Adjust PDFMiner Layout Parameters with layout_kwargs in Camelot Source: https://github.com/atlanhq/camelot/blob/master/docs/user/advanced.rst This example demonstrates how to pass keyword arguments to PDFMiner's LAParams to fine-tune layout generation when reading a PDF with Camelot. This is useful for cases where PDFMiner incorrectly groups characters into sentences. The `detect_vertical` parameter is shown as an example, but other LAParams can also be adjusted. ```python import camelot tables = camelot.read_pdf('foo.pdf', layout_kwargs={'detect_vertical': False}) ``` -------------------------------- ### Stream Flavor Extraction (CLI) Source: https://context7.com/atlanhq/camelot/llms.txt Shows a basic example of extracting tables from a PDF using the 'stream' flavor via the Camelot command-line interface. ```bash # Stream flavor extraction camelot stream -o output.csv -f csv document.pdf ``` -------------------------------- ### Get Camelot and Dependency Versions Source: https://github.com/atlanhq/camelot/blob/master/docs/dev/contributing.rst This Python code snippet retrieves and prints the operating system type, Python version, and the versions of key libraries including NumPy, OpenCV, and Camelot. This information is crucial for bug reports. ```python import platform; print(platform.platform()) import sys; print('Python', sys.version) import numpy; print('NumPy', numpy.__version__) import cv2; print('OpenCV', cv2.__version__) import camelot; print('Camelot', camelot.__version__) ``` -------------------------------- ### Clone Camelot Repository Source: https://github.com/atlanhq/camelot/blob/master/README.md Clones the Camelot project repository from GitHub. This is the first step for both installation and development. ```bash git clone https://www.github.com/camelot-dev/camelot ``` -------------------------------- ### Stream Parser Initialization and Extraction (Python) Source: https://context7.com/atlanhq/camelot/llms.txt Provides an example of initializing and using the Stream parser in Camelot via Python. The Stream parser is ideal for tables lacking clear borders, relying on whitespace analysis. It offers numerous parameters to customize whitespace and text-based detection. ```python from camelot.parsers import Stream # Initialize parser with options parser = Stream( table_regions=None, table_areas=None, columns=None, split_text=False, flag_size=False, strip_text='', edge_tol=50, row_tol=2, column_tol=0 ) # Extract tables from a single-page PDF tables = parser.extract_tables( filename='page-1.pdf', suppress_stdout=False, layout_kwargs={} ) for table in tables: print(f"Table shape: {table.shape}") print(f"Accuracy: {table.accuracy}%") print(table.df) ``` -------------------------------- ### Detect Short Lines with line_scale in Camelot (Lattice) Source: https://github.com/atlanhq/camelot/blob/master/docs/user/advanced.rst Utilizes the 'line_scale' parameter with the Lattice method to detect smaller lines that might otherwise be missed. A larger 'line_scale' detects smaller lines. This example sets 'line_scale' to 40. ```python import camelot import matplotlib.pyplot as plt tables = camelot.read_pdf('short_lines.pdf', line_scale=40) camelot.plot(tables[0], kind='grid') plt.show() print(tables[0].df) ``` -------------------------------- ### Extract Tables from PDF using Camelot Source: https://github.com/atlanhq/camelot/blob/master/README.md This Python code snippet demonstrates how to use the Camelot library to read tables from a PDF file. It shows how to access the extracted tables, view parsing reports, and export them to various formats including CSV, JSON, Excel, HTML, and SQLite. It also shows how to get a pandas DataFrame representation of a table. ```python import camelot tables = camelot.read_pdf('foo.pdf') print(tables) tables.export('foo.csv', f='csv', compress=True) # json, excel, html, sqlite print(tables[0]) print(tables[0].parsing_report) tables[0].to_csv('foo.csv') # to_json, to_excel, to_html, to_sqlite df = tables[0].df # get a pandas DataFrame! ``` -------------------------------- ### Running Camelot CLI with Python Module Source: https://github.com/atlanhq/camelot/blob/master/HISTORY.md This command demonstrates how to run the Camelot command-line interface (CLI) using the `python -m` syntax. This is an alternative way to access Camelot's CLI functionalities, such as displaying help information. ```bash python -m camelot --help ``` -------------------------------- ### Camelot Python Module: Help and Basic Extraction Source: https://context7.com/atlanhq/camelot/llms.txt Shows how to invoke Camelot's help command and perform basic table extraction using the Python module. This is useful for understanding the available options and performing simple extractions programmatically. ```bash python -m camelot --help python -m camelot lattice -o output.csv -f csv document.pdf ``` -------------------------------- ### Lattice Parser Initialization and Extraction (Python) Source: https://context7.com/atlanhq/camelot/llms.txt Demonstrates the initialization and usage of the Lattice parser in Camelot using Python. The Lattice parser is suitable for tables with clear borders and gridlines, utilizing image processing techniques for detection. It allows extensive configuration for fine-tuning the extraction process. ```python from camelot.parsers import Lattice # Initialize parser with options parser = Lattice( table_regions=None, table_areas=None, process_background=False, line_scale=15, copy_text=None, shift_text=['l', 't'], split_text=False, flag_size=False, strip_text='', line_tol=2, joint_tol=2, threshold_blocksize=15, threshold_constant=-2, iterations=0, resolution=300 ) # Extract tables from a single-page PDF tables = parser.extract_tables( filename='page-1.pdf', suppress_stdout=False, layout_kwargs={} ) for table in tables: print(f"Table shape: {table.shape}") print(f"Accuracy: {table.accuracy}%") print(table.df) ``` -------------------------------- ### Group Rows Closer with row_tol in Camelot Source: https://github.com/atlanhq/camelot/blob/master/docs/user/advanced.rst Adjusts the 'row_tol' parameter to group rows closer together. This is useful for refining the structure of extracted tables. The example sets 'row_tol' to 10. ```python import camelot tables = camelot.read_pdf('group_rows.pdf', flavor='stream', row_tol=10) print(tables[0].df) ``` -------------------------------- ### Initial Table Extraction with Default line_scale (Lattice) Source: https://github.com/atlanhq/camelot/blob/master/docs/user/advanced.rst Illustrates the default behavior of Camelot's Lattice method when detecting lines in a PDF, showing that smaller lines might not be detected with the default 'line_scale' of 15. ```python import camelot import matplotlib.pyplot as plt tables = camelot.read_pdf('short_lines.pdf') camelot.plot(tables[0], kind='grid') plt.show() ``` -------------------------------- ### Camelot CLI: Visual Debugging with Lattice Parser Source: https://context7.com/atlanhq/camelot/llms.txt Illustrates how to use the Camelot command-line interface with the 'lattice' parser for visual debugging. This allows users to visualize different aspects of the table detection process, such as text, grid, contour, joint, and line detection, by opening a matplotlib window. ```bash camelot lattice -plot text document.pdf camelot lattice -plot grid document.pdf camelot lattice -plot contour document.pdf camelot lattice -plot joint document.pdf camelot lattice -plot line document.pdf ``` -------------------------------- ### Detect Short Lines with line_scale via CLI (Lattice) Source: https://github.com/atlanhq/camelot/blob/master/docs/user/advanced.rst Demonstrates adjusting the 'line_scale' parameter via the command-line interface for the Lattice method to detect short lines. The '-scale' flag is used. ```bash camelot lattice -scale 40 -plot grid short_lines.pdf ``` -------------------------------- ### Improve Table Area Detection with edge_tol in Camelot Source: https://github.com/atlanhq/camelot/blob/master/docs/user/advanced.rst Increases the 'edge_tol' parameter to improve the detection of table areas, especially when text is vertically spaced. Larger values lead to longer text edges being detected. This example uses a value of 500. ```python import camelot import matplotlib.pyplot as plt tables = camelot.read_pdf('edge_tol.pdf', flavor='stream', edge_tol=500) camelot.plot(tables[0], kind='contour') plt.show() ``` -------------------------------- ### Create ZIP Archive (CLI) Source: https://context7.com/atlanhq/camelot/llms.txt Demonstrates creating a ZIP archive of the output files during table extraction using the -z flag in the Camelot CLI. ```bash # Create ZIP archive of output camelot -z lattice -o output.csv -f csv document.pdf ``` -------------------------------- ### Extract Tables from PDF using Camelot Source: https://github.com/atlanhq/camelot/blob/master/docs/index.rst Demonstrates how to use the Camelot library to read tables from a PDF file and export them. It shows basic table extraction, listing tables, exporting to CSV, and accessing individual table data and parsing reports. Requires the camelot-py library. ```python >>> import camelot >>> tables = camelot.read_pdf('foo.pdf') >>> tables >>> tables.export('foo.csv', f='csv', compress=True) # json, excel, html >>> tables[0]
>>> tables[0].parsing_report { 'accuracy': 99.02, 'whitespace': 12.24, 'order': 1, 'page': 1 } >>> tables[0].to_csv('foo.csv') # to_json, to_excel, to_html >>> tables[0].df # get a pandas DataFrame! ``` -------------------------------- ### PDFHandler Initialization and Parsing (Python) Source: https://context7.com/atlanhq/camelot/llms.txt Illustrates how to initialize and use the PDFHandler class from the Camelot library in Python. This handler is designed for low-level PDF operations, including page splitting and orchestrating table parsing with custom options for both local files and URLs. ```python from camelot.handlers import PDFHandler # Initialize handler handler = PDFHandler( filepath='document.pdf', pages='1,2,3', password=None ) # Handler also accepts URLs handler = PDFHandler( filepath='https://example.com/document.pdf', pages='all' ) # Access parsed page numbers print(f"Pages to process: {handler.pages}") # Parse tables with custom options tables = handler.parse( flavor='lattice', suppress_stdout=False, layout_kwargs={'char_margin': 1.0}, process_background=True, line_scale=15 ) # Parse with stream flavor tables = handler.parse( flavor='stream', suppress_stdout=True, edge_tol=50, row_tol=2 ) ``` -------------------------------- ### Extract from Multiple Pages (CLI) Source: https://context7.com/atlanhq/camelot/llms.txt Demonstrates how to extract tables from specific pages or all pages of a PDF using the Camelot CLI. Supports page ranges and the 'all' keyword. ```bash # Extract from multiple pages camelot -p 1,2,3 lattice -o output.csv -f csv document.pdf camelot -p all lattice -o output.csv -f csv document.pdf camelot -p 1,4-end lattice -o output.csv -f csv document.pdf ``` -------------------------------- ### Export to Different Formats (CLI) Source: https://context7.com/atlanhq/camelot/llms.txt Illustrates exporting extracted tables to various formats including JSON, Excel, HTML, and SQLite using the Camelot CLI. ```bash # Export to different formats camelot lattice -o output.json -f json document.pdf camelot lattice -o output.xlsx -f excel document.pdf camelot lattice -o output.html -f html document.pdf camelot lattice -o output.db -f sqlite document.pdf ``` -------------------------------- ### Camelot CLI: Visual Debugging with Stream Parser Source: https://context7.com/atlanhq/camelot/llms.txt Shows how to use the Camelot command-line interface with the 'stream' parser for visual debugging, specifically focusing on visualizing text edges. This helps in understanding how the stream parser identifies table boundaries based on text elements. ```bash camelot stream -plot textedge document.pdf ``` -------------------------------- ### Camelot CLI: Stream Table Extraction with Options Source: https://context7.com/atlanhq/camelot/llms.txt Demonstrates how to use the Camelot command-line interface to extract tables using the 'stream' flavor. It showcases various options for controlling edge tolerance, row tolerance, column tolerance, column coordinates, table area, splitting text, flagging, stripping characters, and output format. ```bash camelot stream \ -e 50 \ -r 2 \ -c 0 \ -C 72,95,209,327 \ -T 72,730,580,460 \ -R 72,730,580,460 \ -split \ -flag \ -strip '\n' \ -o output.csv -f csv document.pdf ``` -------------------------------- ### camelot.parsers.Lattice Source: https://github.com/atlanhq/camelot/blob/master/docs/api.rst A parser for extracting tables from PDF files using a lattice-based approach, suitable for tables with explicit lines. ```APIDOC ## camelot.parsers.Lattice ### Description Implements the lattice parsing logic for table extraction. ### Method Class (Conceptual API) ### Endpoint N/A (Library Class) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```python from camelot.parsers import Lattice parser = Lattice(filepath='example.pdf', page='1') tables = parser.parse() ``` ### Response #### Success Response - **tables** (TableList) - A TableList object containing the extracted tables. #### Response Example ```json { "tables": [ { "parsing_report": { "accuracy": 99, "whitespace": 10.0, "order": 1, "page": 1 }, "_bbox": [100, 100, 700, 700], "_data": [ ["ID", "Name"], ["1", "Alice"] ] } ] } ``` ``` -------------------------------- ### camelot.handlers.PDFHandler Source: https://github.com/atlanhq/camelot/blob/master/docs/api.rst Handles the low-level operations for interacting with PDF files, including parsing and data extraction. ```APIDOC ## camelot.handlers.PDFHandler ### Description Provides methods for low-level PDF file handling and parsing. ### Method Class (Conceptual API) ### Endpoint N/A (Library Class) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```python from camelot.handlers import PDFHandler handler = PDFHandler('example.pdf') # Further operations using the handler instance ``` ### Response #### Success Response - **handler** (PDFHandler) - An instance of the PDFHandler class. #### Response Example N/A (Class instantiation) ``` -------------------------------- ### Shift Text in PDF Tables (CLI) Source: https://github.com/atlanhq/camelot/blob/master/docs/user/advanced.rst Shows how to achieve text shifting using the Camelot command-line interface. The `-shift` option can be used multiple times to specify the order of shifts. ```bash $ camelot lattice -scale 40 -shift r -shift b short_lines.pdf ``` -------------------------------- ### camelot.core.TableList Source: https://github.com/atlanhq/camelot/blob/master/docs/api.rst Represents a list of extracted tables from a PDF. Provides methods for accessing and manipulating the tables. ```APIDOC ## camelot.core.TableList ### Description Manages a collection of `Table` objects extracted from a PDF. ### Method Class (Conceptual API) ### Endpoint N/A (Library Class) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```python from camelot import read_pdf tables = read_pdf('example.pdf') print(f"Found {len(tables)} tables.") first_table = tables[0] ``` ### Response #### Success Response - **tables** (TableList) - An instance of the TableList class. #### Response Example N/A (Class instantiation) ``` -------------------------------- ### Password-Protected PDF Extraction (CLI) Source: https://context7.com/atlanhq/camelot/llms.txt Shows how to extract tables from a password-protected PDF using the Camelot CLI by providing the password with the -pw option. ```bash # Password-protected PDF camelot -pw mysecret lattice -o output.csv -f csv document.pdf ``` -------------------------------- ### camelot.parsers.Stream Source: https://github.com/atlanhq/camelot/blob/master/docs/api.rst A parser for extracting tables from PDF files using a stream-based approach, suitable for tables with clear spacing. ```APIDOC ## camelot.parsers.Stream ### Description Implements the stream parsing logic for table extraction. ### Method Class (Conceptual API) ### Endpoint N/A (Library Class) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```python from camelot.parsers import Stream parser = Stream(filepath='example.pdf', page='1') tables = parser.parse() ``` ### Response #### Success Response - **tables** (TableList) - A TableList object containing the extracted tables. #### Response Example ```json { "tables": [ { "parsing_report": { "accuracy": 98, "whitespace": 15.0, "order": 2, "page": 1 }, "_bbox": [100, 100, 700, 700], "_data": [ ["Column A", "Column B"], ["Data 1A", "Data 1B"] ] } ] } ``` ``` -------------------------------- ### Basic Table Extraction (CLI) Source: https://context7.com/atlanhq/camelot/llms.txt Performs basic table extraction from a PDF using the Camelot command-line interface with the 'lattice' flavor. Output is saved to a CSV file. ```bash # Basic extraction with lattice (default) camelot lattice -o output.csv -f csv document.pdf ``` -------------------------------- ### Camelot CLI: Quiet Mode for Suppressing Warnings Source: https://context7.com/atlanhq/camelot/llms.txt Demonstrates how to run Camelot commands in quiet mode using the '-q' flag. This suppresses warnings and informational messages during table extraction, making the output cleaner, especially when running automated scripts. ```bash camelot -q lattice -o output.csv -f csv document.pdf ``` -------------------------------- ### camelot.read_pdf Source: https://github.com/atlanhq/camelot/blob/master/docs/api.rst The main interface for reading tables from PDF files. It provides a high-level function to extract tabular data. ```APIDOC ## camelot.read_pdf ### Description Reads tables from a PDF file. ### Method Function Call (Conceptual API) ### Endpoint N/A (Library Function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```python import camelot tables = camelot.read_pdf('example.pdf', pages='1', flavor='lattice') ``` ### Response #### Success Response - **tables** (TableList) - A TableList object containing the extracted tables. #### Response Example ```json { "tables": [ { "parsing_report": { "accuracy": 99, "whitespace": 12.5, "order": 1, "page": 1 }, "_bbox": [100, 100, 700, 700], "_data": [ ["Header 1", "Header 2"], ["Row 1, Col 1", "Row 1, Col 2"] ] } ] } ``` ``` -------------------------------- ### camelot.core.Table Source: https://github.com/atlanhq/camelot/blob/master/docs/api.rst Represents a single table extracted from a PDF. Contains the table data and parsing information. ```APIDOC ## camelot.core.Table ### Description Represents a single table with its data and metadata. ### Method Class (Conceptual API) ### Endpoint N/A (Library Class) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```python from camelot import read_pdf tables = read_pdf('example.pdf') if tables: first_table = tables[0] print(first_table.df) # Accessing the pandas DataFrame print(first_table.parsing_report) ``` ### Response #### Success Response - **table** (Table) - An instance of the Table class. #### Response Example N/A (Class instantiation) ``` -------------------------------- ### Camelot and Pandas Integration for Data Analysis (Python) Source: https://context7.com/atlanhq/camelot/llms.txt Demonstrates how to integrate Camelot table extraction results with the pandas library in Python. This snippet shows how to read a PDF, filter extracted tables based on parsing accuracy and whitespace metrics, and prepare them for further data analysis, cleaning, and transformation. ```python import camelot import pandas as pd # Extract tables tables = camelot.read_pdf('financial_report.pdf', pages='all') # Filter tables by quality quality_tables = [t for t in tables if t.parsing_report['accuracy'] > 80 and t.parsing_report['whitespace'] < 50] ``` -------------------------------- ### Improve Table Area Detection with edge_tol via CLI Source: https://github.com/atlanhq/camelot/blob/master/docs/user/advanced.rst Demonstrates how to adjust the 'edge_tol' parameter using the Camelot command-line interface to improve table area detection. The '-e' flag corresponds to 'edge_tol'. ```bash camelot stream -e 500 -plot contour edge.pdf ``` -------------------------------- ### Accessing Table Bounding Box in Camelot Source: https://github.com/atlanhq/camelot/blob/master/HISTORY.md This Python code snippet illustrates how to access the bounding box coordinates of a detected table using the `_bbox` attribute. This attribute provides the positional information of the table within the PDF page. ```python import camelot tables = camelot.read_pdf('file.pdf') # Assuming at least one table is detected table = tables[0] print(table._bbox) ``` -------------------------------- ### Load Tables into SQLite Database (Python) Source: https://context7.com/atlanhq/camelot/llms.txt Connects to an SQLite database and loads multiple pandas DataFrames into separate tables. Each DataFrame is saved to a table named 'table_i' where 'i' is its index in the 'tables' list. 'if_exists='replace'' ensures that existing tables are overwritten. The connection is closed after all tables are written. ```python import sqlite3 conn = sqlite3.connect('analysis.db') for i, table in enumerate(tables): table.df.to_sql(f'table_{i}', conn, if_exists='replace', index=False) conn.close() ``` -------------------------------- ### Process Background Lines in PDF Tables with Camelot Source: https://github.com/atlanhq/camelot/blob/master/docs/user/advanced.rst Enables the detection of table lines that are in the background of a PDF. This is achieved by setting the `process_background` parameter to `True` when reading the PDF. The output is a `TableList` object containing the detected tables. ```python >>> tables = camelot.read_pdf('background_lines.pdf', process_background=True) >>> tables[1].df ``` -------------------------------- ### Responsive Design Adjustments Source: https://github.com/atlanhq/camelot/blob/master/docs/_templates/hacks.html Implements responsive design rules for screens with a maximum width of 1008px. It hides the sidebar and adjusts the document width to 100%, allowing code blocks to escape the right margin. ```css @media screen and (max-width: 1008px) { div.sphinxsidebar { display: none; } div.document { width: 100%!important; } div.highlight pre { margin-right: -30px; } } ``` -------------------------------- ### Decrypting Encrypted PDFs with Camelot Source: https://github.com/atlanhq/camelot/blob/master/HISTORY.md This Python code snippet demonstrates how to read an encrypted PDF file using Camelot by providing a password. It utilizes the `password` keyword argument in the `read_pdf` function. Note that support for encryption algorithms is limited by PyPDF2. ```python import camelot tables = camelot.read_pdf('encrypted.pdf', password='') print(tables) ``` -------------------------------- ### Set Document Width Source: https://github.com/atlanhq/camelot/blob/master/docs/_templates/hacks.html Sets a fixed width for the main document container. This is intended to prevent code from being cut off on wider screens. ```css div.document { width: 1008px; } ``` -------------------------------- ### Shift Text in PDF Tables (Python) Source: https://github.com/atlanhq/camelot/blob/master/docs/user/advanced.rst Demonstrates how to use the `shift_text` parameter in Camelot's `read_pdf` function to adjust text positioning. This parameter accepts a list of characters ('l', 'r', 't', 'b') to apply shifts in order. The default is `['l', 't']`. ```python >>> tables = camelot.read_pdf('short_lines.pdf', line_scale=40, shift_text=['']) >>> tables[0].df ``` ```python >>> tables = camelot.read_pdf('short_lines.pdf', line_scale=40, shift_text=['r', 'b']) >>> tables[0].df ``` -------------------------------- ### Fixing Ghostscript Subprocess Call on Windows Source: https://github.com/atlanhq/camelot/blob/master/HISTORY.md This code change addresses an issue with how Ghostscript subprocesses were called on Windows. It ensures that Camelot can correctly interact with Ghostscript on Windows systems for PDF processing. ```python # Conceptual fix for ghostscript subprocess call on Windows # Example: Adjusting path or arguments for subprocess.Popen ``` -------------------------------- ### Visual Debugging with plot() (Python) Source: https://context7.com/atlanhq/camelot/llms.txt Utilizes Camelot's plotting capabilities to visualize detected elements within PDF tables for debugging and parameter tuning. Requires matplotlib. Different plot kinds are available for 'lattice' and 'stream' flavors. ```python import camelot import matplotlib.pyplot as plt tables = camelot.read_pdf('document.pdf', flavor='lattice') table = tables[0] # Plot text elements on the page fig = camelot.plot(table, kind='text') plt.title('Text Elements') plt.show() # Plot detected table grid fig = camelot.plot(table, kind='grid') plt.title('Table Grid') plt.show() # Plot table contours/boundaries (lattice only) fig = camelot.plot(table, kind='contour') plt.title('Table Contours') plt.show() # Plot line intersections/joints (lattice only) fig = camelot.plot(table, kind='joint') plt.title('Line Joints') plt.show() # Plot detected line segments (lattice only) fig = camelot.plot(table, kind='line') plt.title('Line Segments') plt.show() # For stream flavor tables tables = camelot.read_pdf('document.pdf', flavor='stream') table = tables[0] # Plot text edges used for column detection (stream only) fig = camelot.plot(table, kind='textedge') plt.title('Text Edges') plt.show() # Save plots to file fig = camelot.plot(table, kind='text') fig.savefig('debug_plot.png', dpi=300, bbox_inches='tight') plt.close() ``` -------------------------------- ### Export Tables with Compression (ZIP) Source: https://context7.com/atlanhq/camelot/llms.txt Exports extracted tables to a CSV file and compresses them into a ZIP archive. This is useful for managing multiple output files efficiently. ```python tables.export('output.csv', f='csv', compress=True) ``` -------------------------------- ### Table Object Operations (Python) Source: https://context7.com/atlanhq/camelot/llms.txt Demonstrates how to access and manipulate individual Table objects obtained from PDF extraction using Camelot. It covers converting to pandas DataFrames, accessing metadata, retrieving raw data, and using quality metrics. ```python import camelot tables = camelot.read_pdf('document.pdf') table = tables[0] # Get table as pandas DataFrame df = table.df print(df.head()) # Table metadata print(f"Shape: {table.shape}") print(f"Page: {table.page}") print(f"Table order on page: {table.order}") # Get raw data as 2D list raw_data = table.data for row in raw_data: print(row) # Quality metrics via parsing report report = table.parsing_report print(f"Accuracy: {report['accuracy']}") # How well text was assigned to cells print(f"Whitespace: {report['whitespace']}") # Percentage of empty cells print(f"Order: {report['order']}") # Table number on page print(f"Page: {report['page']}") # Page number # Filter tables by quality good_tables = [t for t in tables if t.parsing_report['accuracy'] > 90] # Export individual table to various formats table.to_csv('table.csv') table.to_json('table.json') table.to_excel('table.xlsx') table.to_html('table.html') table.to_sqlite('tables.db') # Export with custom pandas options table.to_csv('table.csv', index=True, header=True) table.to_json('table.json', orient='records') table.to_excel('table.xlsx', sheet_name='MyTable') # Get table bounding box coordinates bbox = table._bbox print(f"Bounding box (x1, y1, x2, y2): {bbox}") ``` -------------------------------- ### Flag Superscripts and Subscripts with Camelot Source: https://github.com/atlanhq/camelot/blob/master/docs/user/advanced.rst Employ `flag_size=True` in `camelot.read_pdf` to mark superscripts and subscripts with `` tags. This helps differentiate them from regular text, preventing data analysis errors caused by incorrect decimal points or magnitude differences. The function processes a PDF file and outputs DataFrame objects. ```python import camelot tables = camelot.read_pdf('superscript.pdf', flavor='stream', flag_size=True) print(tables[0].df) ``` -------------------------------- ### Read PDF Tables with Camelot Source: https://context7.com/atlanhq/camelot/llms.txt The `read_pdf` function is the primary API for extracting tables from PDF files. It supports local files, URLs, multiple pages, password-protected PDFs, and both Lattice and Stream parsing flavors with numerous configuration options. ```python import camelot # Basic table extraction from first page tables = camelot.read_pdf('document.pdf') print(f"Found {tables.n} tables") # Extract from multiple pages tables = camelot.read_pdf('document.pdf', pages='1,3,4') tables = camelot.read_pdf('document.pdf', pages='1,4-end') tables = camelot.read_pdf('document.pdf', pages='all') # Extract from URL tables = camelot.read_pdf('https://example.com/document.pdf') # Password-protected PDF tables = camelot.read_pdf('encrypted.pdf', password='secretpassword') # Using lattice flavor (default) - for tables with visible lines tables = camelot.read_pdf( 'document.pdf', flavor='lattice', process_background=True, # Process background lines line_scale=15, # Line detection sensitivity copy_text=['h', 'v'], # Copy text in spanning cells shift_text=['l', 't'], # Text flow direction in spanning cells split_text=True, # Split text across multiple cells flag_size=True, # Detect super/subscripts with strip_text='\n', # Characters to strip from text line_tol=2, # Tolerance for merging lines joint_tol=2, # Tolerance for line intersections threshold_blocksize=15, # Adaptive threshold block size threshold_constant=-2, # Adaptive threshold constant iterations=0, # Erosion/dilation iterations resolution=300 # PDF to PNG conversion resolution ) # Using stream flavor - for tables with whitespace separation tables = camelot.read_pdf( 'document.pdf', flavor='stream', edge_tol=50, # Tolerance for extending text edges row_tol=2, # Tolerance for combining text into rows column_tol=0, # Tolerance for combining text into columns columns=['72,95,209,327,442,529,566,606'], # Manual column separators split_text=True, flag_size=True, strip_text='\n' ) # Specify table areas manually (coordinates: x1,y1,x2,y2) tables = camelot.read_pdf( 'document.pdf', table_areas=['72,730,580,460'] # Left-top to right-bottom ) # Specify regions to search for tables tables = camelot.read_pdf( 'document.pdf', table_regions=['72,730,580,460'] ) # Suppress stdout/warnings tables = camelot.read_pdf('document.pdf', suppress_stdout=True) # Custom PDFMiner layout parameters tables = camelot.read_pdf( 'document.pdf', layout_kwargs={ 'char_margin': 1.0, 'line_margin': 0.5, 'word_margin': 0.1 } ) ``` -------------------------------- ### Specify Column Separators for Extraction Source: https://github.com/atlanhq/camelot/blob/master/docs/user/advanced.rst Explicitly define the x-coordinates of column separators using the `columns` keyword argument in `read_pdf`. This corrects misinterpretations of column boundaries when text elements are very close. The `columns` list length must match the `table_areas` list length if both are provided. ```python import camelot tables = camelot.read_pdf('column_separators.pdf', flavor='stream', columns=['72,95,209,327,442,529,566,606,683']) print(tables[0].df) ```