6

Is there any Python module to convert PDF files into text? I tried one piece of code (mirror) found in Activestate which uses pypdf but the text generated had no space between and was of no use.


This question was originally posted on stack overflow by cnu but it got closed.

2

10 Answers 10

5

The python pdfminer2 or pdfminer3k/pdfminer.six for python 3 libraries can extract the text from pdf files that contain text, (note that scanned documents stored as pdf will contain no text or an attempt at OCR).

It includes the command line pdf2txt.py utility to allow convenient use in the terminal.

Update Nov 2020

However, you might find it worth looking at pdf-scraper-with-ocr which provides a GUI to allow you to select the regions of a scanned document to convert, pages to skip, etc. then outputs them as images an uses Tesseract to OCR the images into named fields for you.

Given that it is based on Python and Tesseract both of which are multi-platform this should work on just about any platform.

4

The easiest way to manage pdf in python is PyPDF2 also you can extract text and other information like pages, author, creator...

You said you tried pypdf but it is an outdated version and this issue could be solved. Maybe your problem is with an specific file. If PyPDF2 does not solve your trouble, Did you see unix command pdftotext?? Does it gives you a correct output?

1
  • 2023 update: PyPDF2 is deprecated. pypdf is the way to go. Mar 31, 2023 at 21:02
3

I have worked with two PDF modules: PyPDF2 and pdfminer.six

PDFminer.six is more reliable than PyPDF2 but 6 times slower (timed).

Here is a working code example for PDFminer.six, the documentation is a little sparse: https://stackoverflow.com/a/61857301/7483211

2
  • 1
    PyPDF2 got a big improvement today. You might want to try it again :-) Jun 6, 2022 at 21:09
  • 2023 update: PyPDF2 is deprecated. pypdf is the way to go. Mar 31, 2023 at 21:03
2

The only solution that worked for me for UTF-8 content: Apache Tika

from tika import parser
parsed = parser.from_file('/path/to/file.pdf')
print(parsed["content"])
0
2

pypdf is maintained again and received a massive text extraction improvement with pypdf>3.0.0.

PyPDF2==3.0.0 is the last version. I moved the project to pypdf :-)

Usage

Install it: pip install pypdf --upgrade

from pypdf import PdfReader

reader = PdfReader("example.pdf")

text = ""
for page in reader.pages:
    text += page.extract_text() + "\n"

print(text)

More in the docs

Benchmark

My benchmark shows that Tika / MuPDF / PDFium still have better extraction quality. However, there are two important things to consider:

  1. pypdf is a pure-python package without dependencies. This makes it way simpler to install.
  2. pypdf is free and open source software (using the BSD 3-clause license).
0
0

You can use GroupDocs.Conversion Cloud SDK for Python to convert PDF to Text. It can convert other common file formats to text as well. Its free package plan provides 50 free credits per month.

I'm developer evangelist at Aspose.

0

https://github.com/kermitt2/grobid + https://github.com/kermitt2/grobid-client-python:

  • extract information from scholarly documents
  • the two repos are Apache-2.0
  • Full text extraction and structuring from PDF articles, including a model for the overall document segmentation and models for the structuring of the text body (paragraph, section titles, reference callout, figure, table, etc.).

  • grobid is in Java and can be called as a service via Python with https://github.com/kermitt2/grobid-client-python
0

https://github.com/allenai/spv2:

  • no longer maintained
  • no license
  • focuses on academic articles
0

https://github.com/allenai/science-parse:

0

You can use Spire.PDF for Python to convert PDF to text. The spacing and positioning of the text will be retained during the text extraction.

from spire.pdf import *
from spire.pdf.common import *

# Create an instance of the PdfDocument class
pdf = PdfDocument()

# Load the PDF document
pdf.LoadFromFile("Sample.pdf")

# Create a TXT file to save the extracted text
extractedText = open("output/ExtractedText.txt", "w", encoding="utf-8")

# Iterate through the pages of the document
for i in range(pdf.Pages.Count):
    # Get the page
    page = pdf.Pages.get_Item(i)
    # Extract text from the page
    text = page.ExtractText()
    # Write the text to the text file
    extractedText.write(text + "\n")

extractedText.close()
pdf.Close()

In addition, it supports advanced PDF text extraction options, such as extracting text from a particular PDF page and extracting text from a specific PDF page area. More details can be found in this article: Python: Extract Text from a PDF Document

I work for the company that developed this library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.