Install & Compatibility
Where this runs
tested against v20191125 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.144s · 36MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 18.9s · import 0.141s · 37MB
34MB installed
● package 34MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
PDFResourceManager
✓ from pdfminer.pdfinterp import PDFResourceManager
PDFPageInterpreter
✓ from pdfminer.pdfinterp import PDFPageInterpreter
PDFPage
✓ from pdfminer.pdfpage import PDFPage
PDFParser
✓ from pdfminer.pdfparser import PDFParser
PDFDocument
✓ from pdfminer.pdfdocument import PDFDocument
TextConverter
✓ from pdfminer.converter import TextConverter
LAParams
✓ from pdfminer.layout import LAParams
This quickstart demonstrates how to extract text from a PDF file using PDFMiner's core components. It initializes a resource manager, a text converter, and a page interpreter to process the PDF document page by page. A dummy `dummy.pdf` file is created if not found, allowing the code to be runnable for demonstration purposes. This reflects the more verbose API usage typical of the original PDFMiner, as opposed to the simplified `high_level` API found in `pdfminer.six`.
import os
from io import StringIO
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfparser import PDFParser
def extract_text_from_pdf(pdf_path):
# Ensure a dummy PDF exists for demonstration, or replace with a real path
if not os.path.exists(pdf_path):
print(f"Error: PDF file not found at {pdf_path}. Creating a dummy PDF for demonstration.")
# In a real scenario, you'd handle the missing file appropriately.
# For a runnable example, we'll create a simple dummy file.
try:
from reportlab.pdfgen import canvas
c = canvas.Canvas(pdf_path)
c.drawString(100, 750, "Hello, PDFMiner!")
c.drawString(100, 730, "This is a dummy PDF for testing.")
c.save()
print(f"Dummy PDF created at {pdf_path}")
except ImportError:
print("Please install reportlab (`pip install reportlab`) to create dummy PDF, or provide a real PDF.")
return ""
rsrcmgr = PDFResourceManager()
retstr = StringIO()
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, laparams=laparams)
with open(pdf_path, 'rb') as fp:
parser = PDFParser(fp)
document = PDFDocument(parser)
interpreter = PDFPageInterpreter(rsrcmgr, device)
for page in PDFPage.create_pages(document):
interpreter.process_page(page)
text = retstr.getvalue()
device.close()
retstr.close()
return text
if __name__ == '__main__':
pdf_file = 'dummy.pdf'
extracted_content = extract_text_from_pdf(pdf_file)
print("\n--- Extracted Text ---")
print(extracted_content)
pdf2txt.py --version
Errors
Common errors & fixes
AttributeError: '_io.BytesIO' object has no attribute 'catalog'
This error typically occurs when a file-like object (like `io.BytesIO`) is passed directly to `PDFPage.create_pages()` or similar functions, but the API expects a `PDFDocument` object that has already been parsed by a `PDFParser`. This indicates incorrect API usage.
fixEnsure you correctly parse the file first using `PDFParser` to create a `PDFDocument` instance, and then pass the `PDFDocument` object to `PDFPage.create_pages()`. Refer to the quickstart example for correct API flow.
ModuleNotFoundError: No module named 'pdfminer.six'
This usually means you have installed the original `pdfminer` package but are attempting to import modules or use `high_level` functions specific to the `pdfminer.six` fork. Or, `pdfminer.six` was not installed at all.
fixIf you intend to use `pdfminer.six` (recommended), ensure you install it with `pip install pdfminer.six`. If you're sticking to the original `pdfminer`, use its specific import paths and API patterns. The original `pdfminer` does not expose a `pdfminer.high_level` module.
UnicodeEncodeError: 'charmap' codec can't encode character...
Encoding issues are common when handling diverse text content in PDFs, especially across different operating systems or locales, or when writing to files without specifying the correct encoding.
fixAlways specify `encoding='utf-8'` when creating output files or `StringIO` objects if you expect Unicode characters. For `TextConverter`, ensure the `outfp` (output file pointer) is opened with `encoding='utf-8'` or handle character sets explicitly.
Upgrade
Version history
20191125latest on PyPI · released Nov 25, 2019
Audit
Dependencies
pycryptodomerequiredRequired for handling encrypted PDF documents.
Resources
No resource links recorded.