Convert PDF to Excel in Python

Convert PDF to Excel (Spreadsheet 2003 XML)

Aspose.PDF for Python via .NET support the feature of converting PDF files to Excel, and CSV formats.

Aspose.PDF for Python via .NET is a PDF manipulation component, we have introduced a feature that renders PDF file to Excel workbook (XLSX files). During this conversion, the individual pages of the PDF file are converted to Excel worksheets.

Use this page when you need to extract table-oriented or report-style PDF content into spreadsheet formats for sorting, filtering, or downstream analysis.

The following code snippet shows the process for converting PDF file into XLS or XLSX format with Aspose.PDF for Python via .NET.

Steps: Convert a PDF file to an Excel (XML Spreadsheet 2003) format

  1. Load the PDF document.
  2. Set up Excel save options using ExcelSaveOptions.
  3. Save the converted file.
from os import path
import aspose.pdf as ap
import sys

def convert_pdf_to_excel_spread_sheet2003(infile, outfile):
    document = ap.Document(infile)
    save_options = ap.ExcelSaveOptions()
    save_options.format = ap.ExcelSaveOptions.ExcelFormat.XML_SPREAD_SHEET2003
    document.save(outfile, save_options)

    print(infile + " converted into " + outfile)

Convert PDF to Excel 2007+ (XLSX)

Steps: Convert a PDF file to an XLSX format (Excel 2007+)

  1. Load the PDF document.
  2. Set up Excel save options using ExcelSaveOptions.
  3. Save the converted file.
from os import path
import aspose.pdf as ap
import sys

def convert_pdf_to_excel_2007(infile, outfile):
    document = ap.Document(infile)
    save_options = ap.ExcelSaveOptions()
    save_options.format = ap.ExcelSaveOptions.ExcelFormat.XLSX
    document.save(outfile, save_options)

    print(infile + " converted into " + outfile)

Convert PDF to XLS with control Column

When converting a PDF to XLS format, a blank column is added to the output file as first column. The in ‘ExcelSaveOptions class’ ‘insert_blank_column_at_first’ option is used to control this column. Its default value is true.

from os import path
import aspose.pdf as ap
import sys

def convert_pdf_to_excel_2007_control_column(infile, outfile):
    document = ap.Document(infile)
    save_options = ap.ExcelSaveOptions()
    save_options.format = ap.ExcelSaveOptions.ExcelFormat.XLSX
    save_options.insert_blank_column_at_first = True
    document.save(outfile, save_options)

    print(infile + " converted into " + outfile)

Convert PDF to Single Excel Worksheet

Aspose.PDF for Python via .NET shows how to convert a PDF to an Excel (.xlsx) file, with the ‘minimize_the_number_of_worksheets’ option enabled.

Steps: Convert PDF to XLS or XLSX Single Worksheet in Python

  1. Load the PDF document.
  2. Set up Excel save options using ExcelSaveOptions.
  3. The ‘minimize_the_number_of_worksheets’ option reduces the number of Excel sheets by combining PDF pages into fewer worksheets (e.g., one worksheet for the entire document if possible).
  4. Save the converted file.
from os import path
import aspose.pdf as ap
import sys

def convert_pdf_to_excel_2007_single_excel_worksheet(infile, outfile):
    document = ap.Document(infile)
    save_options = ap.ExcelSaveOptions()
    save_options.format = ap.ExcelSaveOptions.ExcelFormat.XLSX
    save_options.minimize_the_number_of_worksheets = True
    document.save(outfile, save_options)

    print(infile + " converted into " + outfile)

Convert PDF to Excel 2007 Macro-Enabled (XLSM)

This Python example shows how to convert a PDF file into an Excel file in XLSM format (Excel Macro-Enabled Workbook).

from os import path
import aspose.pdf as ap
import sys

def convert_pdf_to_excel_2007_macro(infile, outfile):
    document = ap.Document(infile)
    save_options = ap.ExcelSaveOptions()
    save_options.format = ap.ExcelSaveOptions.ExcelFormat.XLSM
    document.save(outfile, save_options)

    print(infile + " converted into " + outfile)

Convert to other spreadsheet formats

Convert PDF to CSV

The ‘convert_pdf_to_excel_2007_csv’ function performs the same operation as before, but this time the target format is CSV (Comma-Separated Values) instead of XLSM.

Steps: Convert PDF to CSV in Python

  1. Create an instance of Document object with the source PDF document.
  2. Create an instance of ExcelSaveOptions with ExcelSaveOptions.ExcelFormat.CSV
  3. Save it to CSV format by calling save()* method and passing it ExcelSaveOptions.
from os import path
import aspose.pdf as ap
import sys

def convert_pdf_to_excel_2007_csv(infile, outfile):
    document = ap.Document(infile)
    save_options = ap.ExcelSaveOptions()
    save_options.format = ap.ExcelSaveOptions.ExcelFormat.CSV
    document.save(outfile, save_options)

    print(infile + " converted into " + outfile)

Convert PDF to ODS

Steps: Convert PDF to ODS in Python

  1. Create an instance of Document object with the source PDF document.
  2. Create an instance of ExcelSaveOptions with ExcelSaveOptions.ExcelFormat.ODS
  3. Save it to ODS format by calling save() method and passing it ExcelSaveOptions.

Conversion to ODS format performs in the same way as all other formats.

from os import path
import aspose.pdf as ap
import sys

def convert_pdf_to_ods(infile, outfile):
    document = ap.Document(infile)
    save_options = ap.ExcelSaveOptions()
    save_options.format = ap.ExcelSaveOptions.ExcelFormat.ODS
    document.save(outfile, save_options)

    print(infile + " converted into " + outfile)