Extract tables from PDF in Python

How to Extract tables from PDF using Python

Aspose.PDF for Python for .NET Logo

How to extract tables from PDF using Aspose.PDF for Python for .NET Tool

Do you need to extract tables from PDF? Programmatic modification of PDF documents is an essential part of modern digital workflows. With Python libraries like Aspose.PDF, developers can extract tables from PDF. These libraries are stand-alone solutions that don’t rely on other software and are ready for commercial use. They cover all possible needs of professional Python developers.

  • Extract text from PDF
  • Extract Images from PDF
  • Extract Fonts from PDF
  • Extract Data from the Form
  • Extract Text From Stamps
  • Extract Data from Table

In order to extract tables from PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for python-net platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Console

pip install aspose-pdf

Extract tables from PDF in Python


To try the code in your environment, you need Aspose.PDF for Python.

  1. Open the PDF Document.
  2. Iterate through all pages in the document.
  3. Create a table absorber for the page.
  4. Assemble row text output.

Extract tables from PDF with Python

This sample code shows how to extract tables from PDF documents

Input file:

File not added

Output format:

PDF

Output file:


import aspose.pdf as ap
from os import path

from config import set_license, initialize_data_dir

def extract_tables_from_pdf(infile):
    # Open PDF document
    document = ap.Document(infile)

    # Iterate through each page in the document
    for page in document.pages:
        absorber = ap.text.TableAbsorber()
        absorber.visit(page)

        for table in absorber.table_list:
            print("Table")
            for row in table.row_list:
                row_text = []
                for cell in row.cell_list:
                    cell_text = []
                    for fragment in cell.text_fragments:
                        cell_text.append("".join(seg.text for seg in fragment.segments))
                    row_text.append("|".join(cell_text))
                print("|".join(row_text))

About Aspose.PDF for Python for .NET API

Aspose.PDF for Python via .NET API supports most established PDF standards and PDF specifications. It allows developers to insert tables, graphs, images, hyperlinks, custom fonts - and more - into PDF documents. Moreover, it is also possible to compress PDF documents. Aspose.PDF for Python via .NET provides excellent security features to develop secure PDF documents. Some of the critical features of Aspose.PDF for Python via .NET API include:

  • Ability to read & export PDF in multiple image formats including BMP, GIF, JPEG & PNG.
  • Set basic information (e.g. author, creator) of the PDF document.
  • Conversion Features: Convert PDF to Word, Excel, and PowerPoint. Convert PDF to Image formats. Convert PDF files to HTML format and vice versa. Convert PDF to EPUB, Text, XPS, etc.

On API use, you can find more information about Aspose.PDF for Python via .NET API on our documentation.