The category page had no intro, giving readers no guidance for picking among the PDF, Office, Markdown, and config libraries listed. Co-Authored-By: Claude <noreply@anthropic.com>
7.3 KiB
PDF, Word, and Excel files open with pypdf, python-docx, and openpyxl, a Python file format library for each. MarkItDown turns all three into Markdown.
How to choose:
- Splitting, merging, and reading PDFs: pypdf
- Word documents: python-docx
- Excel: openpyxl to read or edit, XlsxWriter for new reports
- Documents to Markdown for an LLM: MarkItDown
- ELF binaries and DWARF debug info: pyelftools
- One table exported to CSV, JSON, Excel, and more: Tablib
- Scanned PDFs, tables, and complex layouts: Docling
- PowerPoint decks: python-pptx
- New PDFs drawn from Python code: ReportLab
- PDF text with its position and font: pdfminer.six
- PDFs from HTML and CSS: WeasyPrint
- Markdown to HTML: markdown-it-py for CommonMark, Python-Markdown for its extensions, Mistune for speed
- Config files: tomllib for TOML, PyYAML for YAML
pypdf works on PDFs that already exist: it splits, merges, crops, and transforms pages, adds passwords, and pulls out text and metadata. It's pure Python with no C dependency, and it doesn't create PDFs. pypdf isn't OCR software, so run scanned pages through OCR instead.
python-docx only edits existing documents: Document() opens a built-in template with no content. Start from your own .docx instead, so its styles, headers, and footers carry over.
openpyxl reads and writes Excel files. For big workbooks, open them with read_only=True or create them with write_only=True, which keep memory near constant. A cell with a formula loads the formula; pass data_only=True to get the value Excel last calculated.
XlsxWriter only writes new files and can't read or modify existing ones, but it supports more Excel features than the alternatives. Open the workbook in a with block so it gets closed and saved. For large files, turn on constant_memory and write the rows in order. From pandas, pass engine='xlsxwriter' to pd.ExcelWriter.
MarkItDown converts files to Markdown for LLMs and text analysis, not for high-fidelity conversions that people read. Install markitdown[all], or only the extras for your formats, like markitdown[pdf, docx, pptx].
Docling understands PDF layout: reading order, tables, and formulas. It also runs OCR on scanned pages. Its models run locally and send no data out unless you turn remote services on. Each file becomes a DoclingDocument, which you export to Markdown or split into chunks for an embedding model.
python-pptx builds decks from data, like a database query or analytics output, and doesn't need PowerPoint installed. It only edits existing presentations, so start from your own deck: its theme, slide master, and slide layouts set how the slides look. Add each slide from one of those layouts, picked by its index in your deck.
ReportLab draws new PDFs from Python code. Learn it on pdfgen, its lowest-level interface, then build multi-page documents with Platypus. Platypus lets you keep paragraph styles and page layouts in one shared file, so restyling takes a few lines.
pdfminer.six focuses on text: it gets each piece of text with its exact location, font, and color. Start with extract_text() from its high-level API. A PDF stores only characters and their positions, so pdfminer.six guesses words, lines, and paragraphs from the layout. Tune those guesses with LAParams.
WeasyPrint turns HTML and CSS into PDFs, like reports, invoices, and tickets. It runs no JavaScript. Set page size and margins with the CSS @page rule.
markdown-it-py follows the CommonMark spec and takes plugins for more syntax. For content your users submit, use the js-default preset, since the default settings aren't safe for it.
Python-Markdown isn't a CommonMark implementation: it follows the original Markdown syntax and has an extension API. It doesn't sanitize its HTML output, so sanitize it yourself when the input is untrusted.
Mistune is fast and has no dependencies. For untrusted input, build the parser with mistune.create_markdown(), which escapes HTML tags, since mistune.html() doesn't.
tomllib only reads TOML, from a file opened in binary mode.
Tablib holds one dataset and exports it to many formats; Excel, YAML, and pandas are optional extras, like tablib[xlsx].
pyelftools is pure Python with no dependencies; start from its ELFFile class and stay on the high-level API.
Treat every file you didn't create as untrusted. With PyYAML, call yaml.safe_load(), never yaml.load(), which can run any Python function. Install defusedxml next to openpyxl to guard against XML attacks like billion laughs. Catch pypdf's exceptions yourself, so a broken PDF can't crash your service. For MarkItDown, call convert_local() or convert_stream() instead of convert(), which also fetches remote URIs. Cap Docling's input with max_num_pages and max_file_size. Run WeasyPrint on untrusted HTML as a user with limited access, with a URL fetcher that blocks local files. With tomllib, limit the size of the data you parse.