How to Extract Text from PDF using Python (Code Example Tutorial)

Tutorials | Python Fri. 29 Mar. 2024

In today's digital age, PDF files are widely used for storing information, encompassing everything from academic papers to business documents. Simultaneously, extracting text from PDF documents has become increasingly important for text analysis tasks such as sentiment analysis, keyword extraction, and natural language processing.

 

However, extracting text from PDF files programmatically can sometimes pose challenges. Fortunately, there are various powerful Python PDF libraries that make this task much simpler, with ComPDFKit Python PDF library being one of them. In this tutorial, we'll delve into extracting text from PDF files using Python and extracting text from a specific page in PDF using Python, offering you a step-by-step guide and code examples with ComPDFKit.

 

How to Extract Text from PDF using Python

1. Python PDF Text Extraction Library

2. Create a Python Project using PyCharm

3. Install ComPDFKit Python PDF Library 

4. Apply the License Key

5. Extract Text using Python PDF Library

 

Extract Text from PDFs using Python (Code Example Tutorial)

 

1. Python PDF Text Extraction Library

 

Python offers several well-integrated PDF libraries designed to handle unstructured data sources like PDF files effectively. Popular choices include PyPDF2, PyMuPDF, and ReportLab. One standout option for text extraction in Python is the ComPDFKit Python PDF library.

 

ComPDFKit Python library is a feature-rich and user-friendly library that empowers Python applications to perform OCR, extract, and convert PDF documents effortlessly. With this library, you can convert PDF files to various formats such as Word, Excel, PPT, HTML, CSV, images, RTF, and TXT. Additionally, its OCR functionality enables the transformation of images into searchable and editable PDFs, along with the extraction of data from PDFs, including text, tables, and images.

 

Accessing the SDK is straightforward—simply contact us to start leveraging its capabilities seamlessly.

 

Prerequisites

 

Before diving into text extraction from PDF in Python with ComPDFKit, it's crucial to set up the development environment and ensure the following prerequisites are in place:

Python Installation: Ensure that Python is installed on your system. ComPDFKit is compatible with Python 3.x versions (>= 3.6, < 3.11), so make sure you have a compatible Python installation.

. System Requirements: ComPDFKit supports the Windows platform, including Windows Vista, 7, 8, and 10 (both 32-bit and 64-bit), as well as Windows Server 2003, 2008, and 2012 (both 32-bit and 64-bit).

. Integrated Development Environment (IDE): While not mandatory, using an IDE can significantly enhance your development experience. IDEs offer features like code completion, debugging tools, and a streamlined workflow. PyCharm is a popular choice for Python development. You can download and install PyCharm from the JetBrains website to get started.

 

2. Create a Python Project using PyCharm

 

To begin development with the ComPDFKit Python library in PyCharm, follow these steps to create a new Python project:

1. Launch PyCharm: Open PyCharm from your system's application launcher or desktop shortcut.

2. Create a new project: Upon opening PyCharm, either click "Create New Project" or select an existing Python project if applicable.

 

Create a new project on PyCharm

Create a new project on PyCharm

 

3. Configure Project Settings: Give your project a name and specify a location to create the project directory. Choose a Python interpreter for your project, or create a new virtual environment if needed. Then, click "Create" to proceed.

 

Configure Project Settings on PyCharm

Configure Project Settings on PyCharm

 

4. Create Source Files: PyCharm will generate the project structure, including a main Python file and a directory for additional source files. Begin writing your code within the appropriate files. To execute the script, click the "Run" button in the toolbar or press Shift+F10.

 

By following these steps, you'll have set up a PyCharm Python project ready for development with the ComPDFKit Python library. Start coding and harness the power of ComPDFKit for PDF text extraction tasks within your project.

 

3. Install ComPDFKit Python PDF Library 

 

In the provided SDK package directory, enter the terminal (CMD/Powershell), enter the following command and press Enter:

 

python setup.py install

 

4. Apply the License Key

 

Before embarking on text extraction tasks with ComPDFKit Python library, it's essential to follow these steps:

. License Verification: Begin by calling LibraryManager.license_verify to validate your ComPDFKit Conversion SDK License. This step ensures that your license is valid and enables you to proceed with the extraction process.

. SDK Initialization: Once the license verification is successful, initialize the SDK using LibraryManager.initialize. This step prepares the SDK for text extraction operations.

 

It's important to note that ComPDFKit is available under a commercial license, but it also offers a free trial license. To obtain the free trial license, please reach out to our sales team for assistance.

 

Here's how you can apply the license key in your code:

 

# Verify the license.
error_code = LibraryManager.license_verify(
    license_key,
    is_file,
    device_id,
    package_id
)

if error_code == ErrorCode.Success:
    print("License verify success")

    # Initialize SDK.
    LibraryManager.initialize("path/to/resource")

 

By completing these steps, you can seamlessly integrate ComPDFKit into your Python project and commence text extraction tasks with confidence.

 

5. Extract Text using Python PDF Library

 

ComPDFKit provides versatile options for extracting text from PDF files, allowing you to either extract all text from the entire document or selectively extract text from specific pages. Below, you'll find detailed examples demonstrating how to accomplish each of these tasks:

 

Extract Text from an Entire PDF in Python

 

You can simply extract text from an entire PDF document by using the start_extract_pdf_text class to access the content streams from a PDF document.

 

Here is a simple example that shows how to extract text from an entire PDF document using Python and ComPDFKit for Python:

 

options = ConvertOptions()
error_code = PDFToOffice.start_extract_pdf_text("sample.pdf", "", "path/to/output", options, callback)

if error_code == ErrorCode.Success:
    print("Convert success")

 

It's important to note that disabling OCR (Optical Character Recognition) may lead to difficulties in extracting text from tables within images.

 

When utilizing the start_extract_pdf_text class to access content streams from a PDF document, it's common to encounter fragmented data. For instance, suppose we aim to extract the sentence "This is a sample sentence." from a PDF document. In some cases, the text may be retrieved as separate content streams such as "This" and "is a sample sentence.". This fragmentation occurs because text objects in PDFs aren't always neatly organized into words, sentences, or paragraphs.

 

With OCR disabled, the start_extract_pdf_text class will return Text objects exactly as they're defined in the PDF page content streams. This behavior underscores the importance of considering the structure and formatting of PDF documents when extracting text programmatically, especially when dealing with complex layouts or non-standard text arrangements.

 

Extract Text from a Specific Page in PDF in Python

 

We also support extracting text from specific pages or page ranges in a PDF file. Below is a sample code snippet demonstrating how to extract text from a specific page in a PDF file using Python:

 

options = ConvertOptions()
options.pages = "1,3-4"
error_code = PDFToOffice.start_extract_pdf_text("sample.pdf", "", "path/to/output", options, callback)

if error_code == ErrorCode.Success:
    print("Convert success")

 

This code snippet demonstrates how to use ComPDFKit to extract text from a specific page in a PDF file. Simply provide the path to your PDF file and specify the page number from which you want to extract text. Then, utilize the start_extract_pdf_text  method to retrieve the text content from the specified page. Finally, you can print or process the extracted text as needed for your application.

 

Conclusion

 

In this article, we've covered the process of extracting text from PDF files in Python, providing you with detailed steps and code samples. Whether you're extracting text from an entire PDF document or a specific page, the ComPDFKit Python PDF library offers a straightforward solution.

 

For deeper exploration, consider referring to the documentation to explore additional features of the ComPDFKit Python PDF library. This includes extracting data from PDFs, extracting tables, and extracting images from PDF files. Should you have any questions or require assistance, don't hesitate to reach out to our free technical support team for guidance and support. Happy extracting!

Ready to Get Started?

Download our all-in-one ComPDFKit for free and run it to your project within minutes!