|
| 1 | +# PDF Table Extraction: Docling vs LlamaParse |
| 2 | + |
| 3 | +This folder contains the code examples for the Real Python tutorial [PDF Table Extraction: Docling vs LlamaParse](https://realpython.com/pdf-table-extraction-docling-vs-llamaparse/). |
| 4 | + |
| 5 | +The scripts parse `sample_report.pdf`, a short financial report with tables, and compare two approaches: |
| 6 | + |
| 7 | +- **[Docling](https://github.com/docling-project/docling)** runs locally and exports structured document data, including tables as pandas DataFrames. |
| 8 | +- **[LlamaParse](https://docs.cloud.llamaindex.ai/llamaparse/getting_started)** uses the Llama Cloud API for parsing and schema-driven extraction. |
| 9 | + |
| 10 | +## Files |
| 11 | + |
| 12 | +| File | Description | |
| 13 | +|------|-------------| |
| 14 | +| `sample_report.pdf` | Sample PDF used by all scripts | |
| 15 | +| `docling_extraction.py` | Parse the PDF with Docling and print Markdown output | |
| 16 | +| `docling_tables.py` | Inspect detected tables and print selected DataFrames | |
| 17 | +| `docling_formats.py` | Export Docling results to Markdown, JSON, HTML, and DataFrames | |
| 18 | +| `llamaparse_extraction.py` | Parse the PDF with LlamaParse and print Markdown output | |
| 19 | +| `llamaparse_formats.py` | Export LlamaParse results to Markdown, plain text, and JSON | |
| 20 | +| `requirements.txt` | Pinned dependencies for this folder | |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +Create and activate a [virtual environment](https://realpython.com/python-virtual-environments-a-primer/), then install the dependencies: |
| 25 | + |
| 26 | +```shell |
| 27 | +$ python3 -m venv venv/ |
| 28 | +$ source venv/bin/activate |
| 29 | +(venv) $ python -m pip install -r requirements.txt |
| 30 | +``` |
| 31 | + |
| 32 | +Run the scripts from this folder so the relative path to `sample_report.pdf` resolves correctly. |
| 33 | + |
| 34 | +## Docling examples |
| 35 | + |
| 36 | +Docling runs on your machine and does not require an API key. |
| 37 | + |
| 38 | +```shell |
| 39 | +(venv) $ python docling_extraction.py |
| 40 | +(venv) $ python docling_tables.py |
| 41 | +(venv) $ python docling_formats.py |
| 42 | +``` |
| 43 | + |
| 44 | +`docling_formats.py` writes `output_docling.md`, `output_docling.json`, and `output_docling.html` in the current directory. |
| 45 | + |
| 46 | +## LlamaParse examples |
| 47 | + |
| 48 | +The LlamaParse scripts require a [Llama Cloud API key](https://cloud.llamaindex.ai/). Export it before running: |
| 49 | + |
| 50 | +```shell |
| 51 | +(venv) $ export LLAMA_CLOUD_API_KEY="your-api-key" |
| 52 | +(venv) $ python llamaparse_extraction.py |
| 53 | +(venv) $ python llamaparse_formats.py |
| 54 | +``` |
| 55 | + |
| 56 | +`llamaparse_formats.py` writes `output_llamaparse.md`, `output_llamaparse.text`, and `output_llamaparse.json` in the current directory. |
0 commit comments