pdfminer.six
| Language | Python 3.10+ |
|---|---|
| License | MIT |
| Repository | github.com/pdfminer/pdfminer.six |
| Documentation | pdfminersix.readthedocs.io |
| Depends on | charset-normalizer, cryptography (for AES-encrypted files) |
| Read / write | yes / no |
pdfminer.six is the maintained, Python 3 fork of PDFMiner. It interprets page content streams and builds a layout tree: pages contain text boxes, which contain text lines, which contain characters (LTChar) with their font name, size and bounding box, plus images, lines and rectangles. Layout analysis parameters (LAParams) control how characters are grouped into words, lines and boxes. It’s pure Python, handles CMaps and CJK text well, and is the engine behind pdfplumber.
It’s slow on large documents compared with PyMuPDF or pdftotext, and it doesn’t render or modify PDFs.
Installation#
pip install pdfminer.six
The package also installs the pdf2txt.py and dumppdf.py command-line tools.
Basic usage: extract text from a PDF#
from pdfminer.high_level import extract_text
text = extract_text("in.pdf")
print(text)
Options: page_numbers=[0, 1] (0-based), password="...", laparams=LAParams(...).
How to get text boxes, lines and fonts#
from pdfminer.high_level import extract_pages
from pdfminer.layout import LTChar, LTTextContainer
for page_number, page_layout in enumerate(extract_pages("in.pdf"), start=1):
for element in page_layout:
if isinstance(element, LTTextContainer):
x0, y0, x1, y1 = element.bbox # points, origin bottom-left
print(f"page {page_number} box at ({x0:.0f}, {y0:.0f}): {element.get_text()!r}")
for line in element:
for char in line:
if isinstance(char, LTChar):
print(" first char font:", char.fontname, char.size)
break
break
How to tune layout analysis#
LAParams decides when characters form words, lines and boxes. Lower line_margin splits paragraphs more; boxes_flow=None disables the reading-order heuristic for multi-column text:
from pdfminer.high_level import extract_text
from pdfminer.layout import LAParams
text = extract_text("in.pdf", laparams=LAParams(line_margin=0.3, char_margin=2.0, boxes_flow=None))
How to convert a PDF to HTML#
from io import BytesIO
from pdfminer.high_level import extract_text_to_fp
from pdfminer.layout import LAParams
out = BytesIO()
with open("in.pdf", "rb") as f:
extract_text_to_fp(f, out, output_type="html", laparams=LAParams())
html = out.getvalue().decode("utf-8")
output_type can be text, html, xml or tag. Command line: pdf2txt.py -t html -o out.html in.pdf.
How to read PDF metadata#
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfparser import PDFParser
with open("in.pdf", "rb") as f:
doc = PDFDocument(PDFParser(f))
info = doc.info[0] if doc.info else {}
print(info.get("Title"), info.get("Author")) # raw bytes values
Tips#
- Extract text from encrypted files with
extract_text("in.pdf", password="user"). dumppdf.py -a in.pdfdumps all objects as XML, useful for debugging a file’s structure.- If you need tables or a simpler API on top of the same engine, use pdfplumber.