Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to Convert PDF to Excel in Python using REST API

Learn how to convert PDF to Excel document in Python using REST API:

The following topics shall be covered in this article:

  1. Python PDF to Excel Converter API – Installation
  2. How to Convert PDF to XLSX in Python using REST API
  3. Convert Range of Pages from PDF to Excel File in Python
  4. Convert Specific Pages of PDF to Excel format in Python
# Upload PDF file to the cloud storage
# Create an instance of the File API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("python-testing\sample-pdf-file.pdf", "H:\\groupdocs-cloud-data\\sample-pdf-file.pdf", your_storage)
# Upload sample pdf file
response = file_api.upload_file(request)
# API initialization to download converted file
import shutil
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Create download file request
request = groupdocs_conversion_cloud.DownloadFileRequest("python-testing\\sample-pdf-file.xlsx", your_storage)
# Download converted file
response = file_api.download_file(request)
# Move the downloaded file to your directory
shutil.move(response, "H:\\groupdocs-cloud-data\\")
# How to Python Convert PDF to Excel Online
try:
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.storage_name = your_storage
settings.file_path = "python-testing/sample-pdf-file.pdf"
settings.format = "xlsx"
loadOptions = groupdocs_conversion_cloud.PdfLoadOptions()
loadOptions.password = "password"
settings.load_options = loadOptions
settings.output_path = "python-testing"
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert .pdf file to .xlsx format
result = convert_api.convert_document(request)
print("pdf File converted to xlsx successfully: " + result[0].path)
except groupdocs_conversion_cloud.ApiException as e:
print("Exception when calling convert_document: {0}".format(e.message))
# How to Convert Range of Pages from PDF to Excel in Python
# Create an instance of API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare excel convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-pdf-file.pdf"
settings.format = "xlsx"
# PDF convert options: start page number and total pages to convert
convertOptions = groupdocs_conversion_cloud.XlsConvertOptions()
convertOptions.from_page = 2
convertOptions.pages_count = 3
settings.convert_options = convertOptions
settings.output_path = "python-testing"
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert pages of PDF to Excel file
result = convert_api.convert_document(request)
print("Converted range of pages from PDF to Excel file: " + result[0].path)
# How to Convert Specific Pages of PDF file to Excel format in Python
# Create an instance of API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-pdf-file.pdf"
settings.format = "xlsx"
# PDF convert options: page numbers to convert
convertOptions = groupdocs_conversion_cloud.XlsConvertOptions()
convertOptions.pages = [1, 3, 5]
settings.convert_options = convertOptions
settings.output_path = "python-testing"
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert PDF file to Excel file
result = convert_api.convert_document(request)
print("Successfully converted PDF file to XLSX document: " + result[0].path)
# Python library for PDF to Excel conversion in python application
import groupdocs_conversion_cloud
# Get client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
client_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Get different configurations
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
your_storage = "local-storage-name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment