There are various Python packages to extract the text from a PDF with Python. You can see a speed/quality benchmark.
As the maintainer of pypdf
and PyPDF2
I am biased, but I would recommend pypdf
for people to start. It's pure-python and a BSD 3-clause license. That should work for most people. Also pypdf can do way more with PDF files (e.g. transformations).
If you feel comfortable with the C-dependency and don't want to modify the PDF, give pypdfium2 a shot. pypdfium2 is really fast and has an amazing extraction quality.
I previously recommended popplers pdftotext. Don't use that. It's quality is worse than PDFium/PyPDF2.
Tika and PyMuPDF work similarly well as PDFium, but they also have the non-python dependency. PyMuPDF might not work for you due to the commercial license.
I would NOT use pdfminer / pdfminer.six / pdfplumber/ pdftotext / borb / PyPDF2 / PyPDF3 / PyPDF4.
pypdf: Pure Python
Installation: pip install pypdf
(more instructions)
from pypdf import PdfReader
reader = PdfReader("example.pdf")
text = ""
for page in reader.pages:
text += page.extract_text() + "\n"
PDFium: High quality and very fast, but with C-dependency
Installation: pip install pypdfium2
import pypdfium2 as pdfium
text = ""
pdf = pdfium.PdfDocument(data)
for i in range(len(pdf)):
page = pdf.get_page(i)
textpage = page.get_textpage()
text += textpage.get_text()
text += "\n"
[g.close() for g in (textpage, page)]
pdf.close()