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.
| Question | Password to open ("user password") | Restrictions ("owner password") |
|---|---|---|
| What you see | The viewer asks for a password | The file opens; printing or copying is greyed out |
| Content | Encrypted: unreadable without the password | Readable by any program that ignores the restriction |
| To extract text | You need the password | You 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:
| Library | What you get |
|---|---|
| pypdf | FileNotDecryptedError: File has not been decrypted |
| pdfplumber | PDFPasswordIncorrect |
| pdfminer.six | PDFPasswordIncorrect |
| PyMuPDF | Opens 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
from pypdf import PdfReader
reader = PdfReader("protected.pdf")
print(reader.is_encrypted)TrueFrom the command line, qpdf says which kind of lock it is, once you give it the password:
qpdf --show-encryption --password=sands2026 protected.pdfR = 6
P = -4
User password = sands2026
Supplied password is user passwordRead it when you have the password
Every library takes the password and then works as usual.
from pypdf import PdfReader
reader = PdfReader("protected.pdf")
print(reader.decrypt("wrong"), reader.decrypt("sands2026"))
print(reader.pages[0].extract_text()[:28])0 1
COMPANY EXTRACT
Company namedecrypt 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.
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)FalseOr in one command:
qpdf --password=sands2026 --decrypt protected.pdf unlocked.pdfTreat 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:
{ "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:
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"))True FalseTrying the empty password first matters: a file with restrictions but no open password decrypts with it, and can be read like any other.