Simple PDF text extraction
import pdftotext
# Load your PDF
with open("lorem_ipsum.pdf", "rb") as f:
pdf = pdftotext.PDF(f)
# If it's password-protected
with open("secure.pdf", "rb") as f:
pdf = pdftotext.PDF(f, "secret")
# How many pages?
print(len(pdf))
# Iterate over all the pages
for page in pdf:
print(page)
# Read some individual pages
print(pdf[0])
print(pdf[1])
# Read all the text into one string
print("\n\n".join(pdf))
These instructions assume you're using Python 3 on a recent OS. Package names may differ for Python 2 or for an older OS.
sudo apt install build-essential libpoppler-cpp-dev pkg-config python3-dev
sudo yum install gcc-c++ pkgconfig poppler-cpp-devel python3-devel
brew install pkg-config poppler python
Currently tested only when using conda:
- Install the Microsoft Visual C++ Build Tools
- Install poppler through conda:
conda install -c conda-forge poppler
pip install pdftotext