Skip to content

Read and compare PDFs in Python

Python functions that read a PDF as Markdown, HTML, JSON or text, list its pages and fields, outline it and compare two versions.

These functions read a PDF and return what they find; none of them writes a file.

FunctionWhat it doesReturns
export(path, format="markdown", pages=, tables=, ocr=True)Markdown, HTML or JSON text: headings, tables, charts as data, OCR for scansstr
export_json(path, …)The structure as a dict: title, pages with blocks, sectionsdict
export_pages(path, …)Markdown per page: [{"page": 1, "markdown": …}]list[dict]
info(path)Page count, page sizes and rotation, form fields, propertiesPdfInfo
list_files(folder=".")The PDFs in a folder, with sizes and datesFolder
read_text(path, pages=, max_chars=)The text layer, page by page (no OCR)Text
extract_structure(path, pages=, format=, max_chars=)Markdown, HTML or JSON plus the outline, capped at max_charsStructure
outline(path, format="markdown"|"json")The outline, with the pages each section coversOutline
compare(before, after, max_chars=20000)The words that changed, page by page, marked [-old-] and {+new+}Comparison

Examples

Python
import superpdf

markdown = superpdf.export("report.pdf", tables="html")   # merged header cells kept
info = superpdf.info("report.pdf")
print(info["pages"], [field["name"] for field in info.get("forms", [])])

print(superpdf.outline("report.pdf")["content"])          # - Summary (pp. 1–2) …

changes = superpdf.compare("contract-v1.pdf", "contract-v2.pdf")
print(changes["summary"])  # 1 of 4 pages changed: page 3.
for page in changes["pages"]:
    if page["status"] != "same":
        print(page["page"], page["changes"])

All functions are listed on All functions.