tabula-py

repository·master·Indexed 25 days ago

https://github.com/chezou/tabula-py

A Python wrapper for tabula-java used to extract tables from text-based PDF files. It allows users to read PDF tables into pandas DataFrames using `read_pdf()` or convert them directly into CSV, TSV, and JSON formats via `convert_into()` and `convert_into_by_batch()`. The library supports fine-tuning extraction through options like `area`, `lattice`, `stream`, and `pages`, and can leverage `jpype` for faster execution.

Tokens
2.5K
Snippets
12
Records
22
Agent score
80%

What's inside tabula-py

  1. Use high-level interfaces in tabula-py

    master
    For most common tasks like reading PDFs into pandas DataFrames or converting PDFs to CSVs, use the high-level interfaces provided in tabula.io and tabula.util. These modules abstract away the underlying complexity of interacting with the Tabula Java engine.
  2. Extract tables from a specific area using `area` and `spreadsheet`

    master

    To ignore useless areas and focus on a specific part of the PDF, use the area and spreadsheet options.

    Calculating Area Coordinates: When using coordinates from tools like macOS Preview, calculate the area tuple as follows:

    • y1 = top
    • x1 = left
    • y2 = top + height
    • x2 = left + width

    Note: For many PDFs, the spreadsheet=True option is required for the area to work properly.

    tabula.read_pdf('./table.pdf', spreadsheet=True, area=(337.29, 226.49, 472.85, 384.91))
  3. Configure Java options for headless mode or encoding

    master

    You can pass configuration flags to the JVM using the java_options argument.

    • Prevent focus stealing on macOS: Use java_options=["-Djava.awt.headless=true"].
    • Fix encoding issues on Windows: If you see ? characters in UTF-8 PDFs, run chcp 65001 in your terminal and pass java_options="-Dfile.encoding=UTF8" to read_pdf.

    Important: Because jpype is used, java_options cannot be changed once the JVM has started. If you need to change these options, you must restart the Python process.

  4. Install tabula-py

    master

    Install tabula-py using pip. To enable faster execution using jpype, install the jpype extra.

    Requirements:

    • Java: Java 8 or higher must be installed on your environment.
    • Python: Python 3.9 or higher.

    Note: It is recommended to use pip rather than conda-forge to ensure you are using the latest version.

  5. Extract multiple tables from a single document

    master

    To avoid ParserError: Error tokenizing data. C error (which occurs when pandas tries to merge tables with different column counts) and to extract all tables in a file, set multiple_tables=True. This will return a list of DataFrames instead of a single DataFrame.

    df = read_pdf(file_path, multiple_tables=True)
  6. Install tabula-py and verify environment

    master

    tabula-py requires a Java environment to function. Before installing, ensure Java is available on your machine by running java -version.

    Install the package using pip. It is recommended to use {sys.executable} -m pip install tabula-py to ensure it is installed in the correct Python environment.

    After installation, you can verify your environment (Python version, Java version, and OS) using tabula.environment_info().

    # Check Java
    !java -version
    
    # Install tabula-py
    !pip install -q tabula-py
    
    # Verify environment
    import tabula
    tabula.environment_info()
  7. Troubleshoot tabula-py installation and namespace conflicts

    master

    If you encounter issues where tabula-py does not work, ensure Java is installed and the java command is available in your terminal. You can verify if tabula-py can access Java from your Python process using tabula.environment_info().

    Namespace Conflict: If you have a package named tabula installed, it will conflict with tabula-py. To fix this, uninstall tabula and install tabula-py instead.

    pip uninstall tabula
    pip install tabula-py
  8. Troubleshoot Java PATH on Windows 10

    master

    If tabula.read_pdf() raises a FileNotFoundError and running java in your command line returns 'java' is not recognized as an internal or external command, you must add the Java bin directory to your system PATH.

    1. Find your Java installation folder (e.g., C:\Program Files\Java\jdk... or jre...).
    2. Open Control Panel -> System and Security -> System -> Advanced System Settings -> Environment Variables.
    3. Select PATH from the list and click Edit.
    4. Add the path to the bin folder (e.g., C:\Program Files\Java\jre1.8.0_144\bin).
    5. Verify by running java in a new command line window; it should print a list of options.