Add password support for PDF conversion#1629
Open
mvanhorn wants to merge 1 commit intomicrosoft:mainfrom
Open
Conversation
Pass an optional password parameter through to pdfminer and pdfplumber when converting encrypted PDFs. Raise a clear FileConversionException when the password is missing or incorrect. Add a --password CLI flag. Fixes microsoft#1585
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MarkItDown fails with unhelpful errors when encountering password-protected PDF files. There is no way to supply a decryption password through the Python API or CLI.
Reported in #1585.
Solution
Added an optional
passwordparameter that flows through the existing**kwargspipeline to the PDF converter:Python API:
CLI:
Changes
_pdf_converter.py: Extractpasswordfrom kwargs and pass it to bothpdfplumber.open()andpdfminer.high_level.extract_text(). CatchPDFPasswordIncorrectand raise a clearFileConversionExceptionwith a helpful message.__main__.py: Add--passwordCLI argument and pass it through to the convert calls.test_pdf_password.pywith 5 tests covering correct password, missing password, wrong password, non-encrypted PDF regression, and CLI flag presence.test_password.pdf(encrypted with password "testpassword").Scope
This PR covers PDF password support only. DOCX and XLSX password support (also mentioned in #1585) can follow in separate PRs to keep this one focused and reviewable.
No new dependencies
Both
pdfminer.sixandpdfplumberalready support thepasswordparameter natively.This contribution was developed with AI assistance (Claude Code).