Password-protected PDFs: why extraction fails and what to do

Updated · Tested with pypdf 7.2.0, PyMuPDF 2.2, pdfplumber 0.11.11, qpdf 12.2.0

In short

A PDF that needs a password to open is encrypted, and no tool can read it without that password. With it, call decrypt() in pypdf before extracting, or write an unlocked copy with pypdf or qpdf. A PDF that opens freely but blocks copying is a different case: it carries a restriction that its author set.

A PDF can be locked in two ways that are often confused. The difference decides whether your code can read the file at all.

QuestionPassword to open ("user password")Restrictions ("owner password")
What you seeThe viewer asks for a passwordThe file opens; printing or copying is greyed out
ContentEncrypted: unreadable without the passwordReadable by any program that ignores the restriction
To extract textYou need the passwordYou need the right to do it

The sample for this guide, protected.pdf, needs a password to open. It is sands2026.

What extraction tools do with a locked file

Without the password, every library fails, each in its own words:

LibraryWhat you get
pypdfFileNotDecryptedError: File has not been decrypted
pdfplumberPDFPasswordIncorrect
pdfminer.sixPDFPasswordIncorrect
PyMuPDFOpens the file, then fails when a page is read

None of them returns partial text. If your pipeline shows empty results for some PDFs, check whether they are encrypted before blaming the extractor.

Check whether a file is locked

Python
from pypdf import PdfReader

reader = PdfReader("protected.pdf")
print(reader.is_encrypted)
Output
True

From the command line, qpdf says which kind of lock it is, once you give it the password:

Shell
qpdf --show-encryption --password=sands2026 protected.pdf
Output
R = 6
P = -4
User password = sands2026
Supplied password is user password

Read it when you have the password

Every library takes the password and then works as usual.

Python
from pypdf import PdfReader

reader = PdfReader("protected.pdf")
print(reader.decrypt("wrong"), reader.decrypt("sands2026"))
print(reader.pages[0].extract_text()[:28])
Output
0 1
COMPANY EXTRACT
Company name

decrypt returns 0 for a wrong password and does not raise an error, so check its result. With PyMuPDF the call is doc.authenticate("sands2026"), and with pdfplumber it is pdfplumber.open("protected.pdf", password="sands2026").

Make an unlocked copy

If a tool or a service does not accept passwords, decrypt the file once and pass on the copy.

Python
from pypdf import PdfReader, PdfWriter

reader = PdfReader("protected.pdf")
reader.decrypt("sands2026")
PdfWriter(clone_from=reader).write("unlocked.pdf")

print(PdfReader("unlocked.pdf").is_encrypted)
Output
False

Or in one command:

Shell
qpdf --password=sands2026 --decrypt protected.pdf unlocked.pdf

Treat the unlocked copy with the care the password was protecting. Write it to a temporary place and delete it when you are done.

When you do not have the password

Then you cannot read the file, and that is the point of it. Modern PDFs use AES encryption with 256-bit keys; tools that claim to "remove" an open password are guessing passwords, which works only for weak ones. Ask whoever sent the file for the password or for an unprotected copy.

Files that only carry restrictions are different in practice, since many libraries read them without asking. Whether you may is a question for the document's owner, not for the software.

Locked files and 100sands

100sands does not accept passwords. A file that needs one to open is refused with status 422 and a body of this shape, and it does not count toward your plan:

JSON
{ "error": "ENCRYPTED_PDF", "message": "This PDF is protected with a password, so it can't be read." }

Decrypt the file first with one of the two methods above, then send the unlocked copy. The password never leaves your machine that way.

A check to run before every extraction

Encrypted files are the most common reason a batch job "silently" skips documents. Put this at the top of the pipeline and log what it finds:

Python
from pypdf import PdfReader

def needs_password(path):
    reader = PdfReader(path)
    return reader.is_encrypted and reader.decrypt("") == 0

print(needs_password("protected.pdf"), needs_password("company-extract.pdf"))
Output
True False

Trying the empty password first matters: a file with restrictions but no open password decrypts with it, and can be read like any other.

Questions

How do I extract text from a password-protected PDF in Python?

Open it with pypdf's PdfReader, call reader.decrypt(password), check that it did not return 0, and then extract the text as usual.

How do I know if a PDF is encrypted?

In pypdf, PdfReader(path).is_encrypted is True. From the command line, qpdf --show-encryption prints the details.

Can I remove the password from a PDF?

If you know it, yes: qpdf --password=... --decrypt in.pdf out.pdf writes an unlocked copy. If you do not know it, no. Current PDFs use AES-256, and a tool can only guess weak passwords.

What is the difference between a user and an owner password?

The user password is needed to open the file and encrypts its content. The owner password only guards settings such as whether printing and copying are allowed.

Try it on your own documents

A free plan every month, no credit card. See the PDF to text API.

Start free