Extract Text from PDF using Python

Extract Text from PDF using Python

You may need to read and extract text from PDF documents in your Python applications. So, as a Python developer, you can easily extract all the text from PDF documents programmatically on the cloud. This article will explain how to extract text from PDF documents using a REST API in Python.

The following topics shall be covered in this article:

Document Parser REST API and Python SDK

For extracting text from a PDF document, I will be using the Python SDK of GroupDocs.Parser Cloud API. It allows you to parse data from all popular document types. You can extract text, images, and parse data by a template by using the SDK. It also provides .NET, Java, PHP, Ruby, and Node.js SDKs as its document parser family members for the Cloud API.

You can install GroupDocs.Parser Cloud to your Python project with pip (package installer for python) using the following command in the console:

pip install groupdocs_parser_cloud

Please get your Client ID and Client Secret from the dashboard before you start following the steps and available code examples. Once you have your client ID and Secret, add in the code as shown below:

Extract Text from PDF using a REST API in Python

You can extract text from PDF documents by following the simple steps mentioned below:

Upload the Document

First of all, upload the PDF document to the Cloud using the code example given below:

As a result, the uploaded PDF file (sample.pdf) will be available in the files section of your dashboard on the cloud.

Extract Text from PDF Documents using Python

You can easily extract all the text from the PDF file programmatically by following the steps mentioned below.

  • Create an instance of ParseApi
  • Define TextOptions
  • Set path to the PDF file
  • Create TextRequest
  • Get results by calling the ParseApi.text() method

The following code sample shows how to extract all the text from a PDF document using a REST API.

Extract Text From The Whole Document
Extract Text From The Whole Document

Read Text by Page Numbers from PDF Documents using Python

You can easily extract the text from specific pages of a PDF file programmatically by following the steps mentioned below.

  • Create an instance of ParseApi
  • Define TextOptions
  • Provide the path to the PDF file
  • Set the start page number
  • set the count of pages to extract
  • Create TextRequest
  • Get results by calling the ParseApi.text() method

The following code sample shows how to extract the text by page numbers range from a PDF document using a REST API.

Extract Text by a Page Number Range
Extract Text by a Page Number Range

Get Text From Document Attached with PDF using Python

You can extract the text from a document inside a container, available as an attachment in a PDF file programmatically by following the steps mentioned below.

  • Create an instance of ParseApi
  • Define TextOptions
  • Set path to the PDF file
  • Define ContainerItemInfo
  • Provide the relative path of the inside document
  • Set the start page number
  • set the count of pages to extract
  • Create TextRequest
  • Get results by calling the ParseApi.text() method

The following code sample shows how to extract the text from a document inside a PDF document using a REST API.

Extract Text From a Document Inside a Container
Extract Text From a Document Inside a Container

Try Online

Please try the following free online PDF Parsing tool, which is developed using the above API. https://products.groupdocs.app/parser/pdf

Conclusion

In this article, you have learned how to extract text from PDF documents on the cloud. This article also explained how to programmatically upload a PDF file on the cloud. You can learn more about GroupDocs.Parser Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also