markitdown-ocr 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- markitdown_ocr-0.1.0/.gitignore +168 -0
- markitdown_ocr-0.1.0/LICENSE +21 -0
- markitdown_ocr-0.1.0/PKG-INFO +233 -0
- markitdown_ocr-0.1.0/README.md +200 -0
- markitdown_ocr-0.1.0/pyproject.toml +57 -0
- markitdown_ocr-0.1.0/src/markitdown_ocr/__about__.py +4 -0
- markitdown_ocr-0.1.0/src/markitdown_ocr/__init__.py +31 -0
- markitdown_ocr-0.1.0/src/markitdown_ocr/_docx_converter_with_ocr.py +189 -0
- markitdown_ocr-0.1.0/src/markitdown_ocr/_ocr_service.py +110 -0
- markitdown_ocr-0.1.0/src/markitdown_ocr/_pdf_converter_with_ocr.py +422 -0
- markitdown_ocr-0.1.0/src/markitdown_ocr/_plugin.py +68 -0
- markitdown_ocr-0.1.0/src/markitdown_ocr/_pptx_converter_with_ocr.py +249 -0
- markitdown_ocr-0.1.0/src/markitdown_ocr/_xlsx_converter_with_ocr.py +225 -0
- markitdown_ocr-0.1.0/tests/__init__.py +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/docx_complex_layout.docx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/docx_image_end.docx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/docx_image_middle.docx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/docx_image_start.docx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/docx_multipage.docx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/docx_multiple_images.docx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pdf_complex_layout.pdf +79 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pdf_image_end.pdf +79 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pdf_image_middle.pdf +79 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pdf_image_start.pdf +79 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pdf_multipage.pdf +139 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pdf_multiple_images.pdf +88 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pdf_scanned_invoice.pdf +354 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pdf_scanned_meeting_minutes.pdf +363 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pdf_scanned_minimal.pdf +180 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pdf_scanned_report.pdf +1136 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pdf_scanned_sales_report.pdf +342 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pptx_complex_layout.pptx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pptx_image_end.pptx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pptx_image_middle.pptx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pptx_image_start.pptx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/pptx_multiple_images.pptx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/xlsx_complex_layout.xlsx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/xlsx_image_end.xlsx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/xlsx_image_middle.xlsx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/xlsx_image_start.xlsx +0 -0
- markitdown_ocr-0.1.0/tests/ocr_test_data/xlsx_multiple_images.xlsx +0 -0
- markitdown_ocr-0.1.0/tests/test_docx_converter.py +223 -0
- markitdown_ocr-0.1.0/tests/test_pdf_converter.py +234 -0
- markitdown_ocr-0.1.0/tests/test_pptx_converter.py +148 -0
- markitdown_ocr-0.1.0/tests/test_xlsx_converter.py +249 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
.vscode
|
|
2
|
+
|
|
3
|
+
# Byte-compiled / optimized / DLL files
|
|
4
|
+
__pycache__/
|
|
5
|
+
*.py[cod]
|
|
6
|
+
*$py.class
|
|
7
|
+
|
|
8
|
+
# C extensions
|
|
9
|
+
*.so
|
|
10
|
+
|
|
11
|
+
# Distribution / packaging
|
|
12
|
+
.Python
|
|
13
|
+
build/
|
|
14
|
+
develop-eggs/
|
|
15
|
+
dist/
|
|
16
|
+
downloads/
|
|
17
|
+
eggs/
|
|
18
|
+
.eggs/
|
|
19
|
+
lib/
|
|
20
|
+
lib64/
|
|
21
|
+
parts/
|
|
22
|
+
sdist/
|
|
23
|
+
var/
|
|
24
|
+
wheels/
|
|
25
|
+
share/python-wheels/
|
|
26
|
+
*.egg-info/
|
|
27
|
+
.installed.cfg
|
|
28
|
+
*.egg
|
|
29
|
+
MANIFEST
|
|
30
|
+
|
|
31
|
+
# PyInstaller
|
|
32
|
+
# Usually these files are written by a python script from a template
|
|
33
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
34
|
+
*.manifest
|
|
35
|
+
*.spec
|
|
36
|
+
|
|
37
|
+
# Installer logs
|
|
38
|
+
pip-log.txt
|
|
39
|
+
pip-delete-this-directory.txt
|
|
40
|
+
|
|
41
|
+
# Unit test / coverage reports
|
|
42
|
+
htmlcov/
|
|
43
|
+
.tox/
|
|
44
|
+
.nox/
|
|
45
|
+
.coverage
|
|
46
|
+
.coverage.*
|
|
47
|
+
.cache
|
|
48
|
+
nosetests.xml
|
|
49
|
+
coverage.xml
|
|
50
|
+
*.cover
|
|
51
|
+
*.py,cover
|
|
52
|
+
.hypothesis/
|
|
53
|
+
.pytest_cache/
|
|
54
|
+
cover/
|
|
55
|
+
.test-logs/
|
|
56
|
+
|
|
57
|
+
# Translations
|
|
58
|
+
*.mo
|
|
59
|
+
*.pot
|
|
60
|
+
|
|
61
|
+
# Django stuff:
|
|
62
|
+
*.log
|
|
63
|
+
local_settings.py
|
|
64
|
+
db.sqlite3
|
|
65
|
+
db.sqlite3-journal
|
|
66
|
+
|
|
67
|
+
# Flask stuff:
|
|
68
|
+
instance/
|
|
69
|
+
.webassets-cache
|
|
70
|
+
|
|
71
|
+
# Scrapy stuff:
|
|
72
|
+
.scrapy
|
|
73
|
+
|
|
74
|
+
# Sphinx documentation
|
|
75
|
+
docs/_build/
|
|
76
|
+
|
|
77
|
+
# PyBuilder
|
|
78
|
+
.pybuilder/
|
|
79
|
+
target/
|
|
80
|
+
|
|
81
|
+
# Jupyter Notebook
|
|
82
|
+
.ipynb_checkpoints
|
|
83
|
+
|
|
84
|
+
# IPython
|
|
85
|
+
profile_default/
|
|
86
|
+
ipython_config.py
|
|
87
|
+
|
|
88
|
+
# pyenv
|
|
89
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
90
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
91
|
+
# .python-version
|
|
92
|
+
|
|
93
|
+
# pipenv
|
|
94
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
95
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
96
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
97
|
+
# install all needed dependencies.
|
|
98
|
+
#Pipfile.lock
|
|
99
|
+
|
|
100
|
+
# poetry
|
|
101
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
102
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
103
|
+
# commonly ignored for libraries.
|
|
104
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
105
|
+
#poetry.lock
|
|
106
|
+
|
|
107
|
+
# pdm
|
|
108
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
109
|
+
#pdm.lock
|
|
110
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
111
|
+
# in version control.
|
|
112
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
113
|
+
.pdm.toml
|
|
114
|
+
.pdm-python
|
|
115
|
+
.pdm-build/
|
|
116
|
+
|
|
117
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
118
|
+
__pypackages__/
|
|
119
|
+
|
|
120
|
+
# Celery stuff
|
|
121
|
+
celerybeat-schedule
|
|
122
|
+
celerybeat.pid
|
|
123
|
+
|
|
124
|
+
# SageMath parsed files
|
|
125
|
+
*.sage.py
|
|
126
|
+
|
|
127
|
+
# Environments
|
|
128
|
+
.env
|
|
129
|
+
.venv
|
|
130
|
+
env/
|
|
131
|
+
venv/
|
|
132
|
+
ENV/
|
|
133
|
+
env.bak/
|
|
134
|
+
venv.bak/
|
|
135
|
+
|
|
136
|
+
# Spyder project settings
|
|
137
|
+
.spyderproject
|
|
138
|
+
.spyproject
|
|
139
|
+
|
|
140
|
+
# Rope project settings
|
|
141
|
+
.ropeproject
|
|
142
|
+
|
|
143
|
+
# mkdocs documentation
|
|
144
|
+
/site
|
|
145
|
+
|
|
146
|
+
# mypy
|
|
147
|
+
.mypy_cache/
|
|
148
|
+
.dmypy.json
|
|
149
|
+
dmypy.json
|
|
150
|
+
|
|
151
|
+
# Pyre type checker
|
|
152
|
+
.pyre/
|
|
153
|
+
|
|
154
|
+
# pytype static type analyzer
|
|
155
|
+
.pytype/
|
|
156
|
+
|
|
157
|
+
# Cython debug symbols
|
|
158
|
+
cython_debug/
|
|
159
|
+
|
|
160
|
+
# PyCharm
|
|
161
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
162
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
163
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
164
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
165
|
+
#.idea/
|
|
166
|
+
src/.DS_Store
|
|
167
|
+
.DS_Store
|
|
168
|
+
.cursorrules
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Microsoft Corporation.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: markitdown-ocr
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: OCR plugin for MarkItDown - Extracts text from images in PDF, DOCX, PPTX, and XLSX via LLM Vision
|
|
5
|
+
Project-URL: Documentation, https://github.com/microsoft/markitdown#readme
|
|
6
|
+
Project-URL: Issues, https://github.com/microsoft/markitdown/issues
|
|
7
|
+
Project-URL: Source, https://github.com/microsoft/markitdown
|
|
8
|
+
Author-email: Contributors <noreply@github.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: docx,llm,markitdown,ocr,pdf,pptx,vision,xlsx
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Programming Language :: Python
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Requires-Dist: mammoth~=1.11.0
|
|
21
|
+
Requires-Dist: markitdown>=0.1.0
|
|
22
|
+
Requires-Dist: openpyxl
|
|
23
|
+
Requires-Dist: pandas
|
|
24
|
+
Requires-Dist: pdfminer-six>=20251230
|
|
25
|
+
Requires-Dist: pdfplumber>=0.11.9
|
|
26
|
+
Requires-Dist: pillow>=9.0.0
|
|
27
|
+
Requires-Dist: pymupdf>=1.24.0
|
|
28
|
+
Requires-Dist: python-docx
|
|
29
|
+
Requires-Dist: python-pptx
|
|
30
|
+
Provides-Extra: llm
|
|
31
|
+
Requires-Dist: openai>=1.0.0; extra == 'llm'
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# MarkItDown OCR Plugin
|
|
35
|
+
|
|
36
|
+
LLM Vision plugin for MarkItDown that extracts text from images embedded in PDF, DOCX, PPTX, and XLSX files.
|
|
37
|
+
|
|
38
|
+
Uses the same `llm_client` / `llm_model` pattern that MarkItDown already supports for image descriptions — no new ML libraries or binary dependencies required.
|
|
39
|
+
|
|
40
|
+
## Features
|
|
41
|
+
|
|
42
|
+
- **Enhanced PDF Converter**: Extracts text from images within PDFs, with full-page OCR fallback for scanned documents
|
|
43
|
+
- **Enhanced DOCX Converter**: OCR for images in Word documents
|
|
44
|
+
- **Enhanced PPTX Converter**: OCR for images in PowerPoint presentations
|
|
45
|
+
- **Enhanced XLSX Converter**: OCR for images in Excel spreadsheets
|
|
46
|
+
- **Context Preservation**: Maintains document structure and flow when inserting extracted text
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install markitdown-ocr
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The plugin uses whatever OpenAI-compatible client you already have. Install one if you don't have it yet:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
pip install openai
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Usage
|
|
61
|
+
|
|
62
|
+
### Command Line
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
markitdown document.pdf --use-plugins --llm-client openai --llm-model gpt-4o
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Python API
|
|
69
|
+
|
|
70
|
+
Pass `llm_client` and `llm_model` to `MarkItDown()` exactly as you would for image descriptions:
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from markitdown import MarkItDown
|
|
74
|
+
from openai import OpenAI
|
|
75
|
+
|
|
76
|
+
md = MarkItDown(
|
|
77
|
+
enable_plugins=True,
|
|
78
|
+
llm_client=OpenAI(),
|
|
79
|
+
llm_model="gpt-4o",
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
result = md.convert("document_with_images.pdf")
|
|
83
|
+
print(result.text_content)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
If no `llm_client` is provided the plugin still loads, but OCR is silently skipped — falling back to the standard built-in converter.
|
|
87
|
+
|
|
88
|
+
### Custom Prompt
|
|
89
|
+
|
|
90
|
+
Override the default extraction prompt for specialized documents:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
md = MarkItDown(
|
|
94
|
+
enable_plugins=True,
|
|
95
|
+
llm_client=OpenAI(),
|
|
96
|
+
llm_model="gpt-4o",
|
|
97
|
+
llm_prompt="Extract all text from this image, preserving table structure.",
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Any OpenAI-Compatible Client
|
|
102
|
+
|
|
103
|
+
Works with any client that follows the OpenAI API:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
from openai import AzureOpenAI
|
|
107
|
+
|
|
108
|
+
md = MarkItDown(
|
|
109
|
+
enable_plugins=True,
|
|
110
|
+
llm_client=AzureOpenAI(
|
|
111
|
+
api_key="...",
|
|
112
|
+
azure_endpoint="https://your-resource.openai.azure.com/",
|
|
113
|
+
api_version="2024-02-01",
|
|
114
|
+
),
|
|
115
|
+
llm_model="gpt-4o",
|
|
116
|
+
)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## How It Works
|
|
120
|
+
|
|
121
|
+
When `MarkItDown(enable_plugins=True, llm_client=..., llm_model=...)` is called:
|
|
122
|
+
|
|
123
|
+
1. MarkItDown discovers the plugin via the `markitdown.plugin` entry point group
|
|
124
|
+
2. It calls `register_converters()`, forwarding all kwargs including `llm_client` and `llm_model`
|
|
125
|
+
3. The plugin creates an `LLMVisionOCRService` from those kwargs
|
|
126
|
+
4. Four OCR-enhanced converters are registered at **priority -1.0** — before the built-in converters at priority 0.0
|
|
127
|
+
|
|
128
|
+
When a file is converted:
|
|
129
|
+
|
|
130
|
+
1. The OCR converter accepts the file
|
|
131
|
+
2. It extracts embedded images from the document
|
|
132
|
+
3. Each image is sent to the LLM with an extraction prompt
|
|
133
|
+
4. The returned text is inserted inline, preserving document structure
|
|
134
|
+
5. If the LLM call fails, conversion continues without that image's text
|
|
135
|
+
|
|
136
|
+
## Supported File Formats
|
|
137
|
+
|
|
138
|
+
### PDF
|
|
139
|
+
|
|
140
|
+
- Embedded images are extracted by position (via `page.images` / page XObjects) and OCR'd inline, interleaved with the surrounding text in vertical reading order.
|
|
141
|
+
- **Scanned PDFs** (pages with no extractable text) are detected automatically: each page is rendered at 300 DPI and sent to the LLM as a full-page image.
|
|
142
|
+
- **Malformed PDFs** that pdfplumber/pdfminer cannot open (e.g. truncated EOF) are retried with PyMuPDF page rendering, so content is still recovered.
|
|
143
|
+
|
|
144
|
+
### DOCX
|
|
145
|
+
|
|
146
|
+
- Images are extracted via document part relationships (`doc.part.rels`).
|
|
147
|
+
- OCR is run before the DOCX→HTML→Markdown pipeline executes: placeholder tokens are injected into the HTML so that the markdown converter does not escape the OCR markers, and the final placeholders are replaced with the formatted `*[Image OCR]...[End OCR]*` blocks after conversion.
|
|
148
|
+
- Document flow (headings, paragraphs, tables) is fully preserved around the OCR blocks.
|
|
149
|
+
|
|
150
|
+
### PPTX
|
|
151
|
+
|
|
152
|
+
- Picture shapes, placeholder shapes with images, and images inside groups are all supported.
|
|
153
|
+
- Shapes are processed in top-to-left reading order per slide.
|
|
154
|
+
- If an `llm_client` is configured, the LLM is asked for a description first; OCR is used as the fallback when no description is returned.
|
|
155
|
+
|
|
156
|
+
### XLSX
|
|
157
|
+
|
|
158
|
+
- Images embedded in worksheets (`sheet._images`) are extracted per sheet.
|
|
159
|
+
- Cell position is calculated from the image anchor coordinates (column/row → Excel letter notation).
|
|
160
|
+
- Images are listed under a `### Images in this sheet:` section after the sheet's data table — they are not interleaved into the table rows.
|
|
161
|
+
|
|
162
|
+
### Output format
|
|
163
|
+
|
|
164
|
+
Every extracted OCR block is wrapped as:
|
|
165
|
+
|
|
166
|
+
```text
|
|
167
|
+
*[Image OCR]
|
|
168
|
+
<extracted text>
|
|
169
|
+
[End OCR]*
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Troubleshooting
|
|
173
|
+
|
|
174
|
+
### OCR text missing from output
|
|
175
|
+
|
|
176
|
+
The most likely cause is a missing `llm_client` or `llm_model`. Verify:
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
from openai import OpenAI
|
|
180
|
+
from markitdown import MarkItDown
|
|
181
|
+
|
|
182
|
+
md = MarkItDown(
|
|
183
|
+
enable_plugins=True,
|
|
184
|
+
llm_client=OpenAI(), # required
|
|
185
|
+
llm_model="gpt-4o", # required
|
|
186
|
+
)
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Plugin not loading
|
|
190
|
+
|
|
191
|
+
Confirm the plugin is installed and discovered:
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
markitdown --list-plugins # should show: ocr
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### API errors
|
|
198
|
+
|
|
199
|
+
The plugin propagates LLM API errors as warnings and continues conversion. Check your API key, quota, and that the chosen model supports vision inputs.
|
|
200
|
+
|
|
201
|
+
## Development
|
|
202
|
+
|
|
203
|
+
### Running Tests
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
cd packages/markitdown-ocr
|
|
207
|
+
pytest tests/ -v
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Building from Source
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
git clone https://github.com/microsoft/markitdown.git
|
|
214
|
+
cd markitdown/packages/markitdown-ocr
|
|
215
|
+
pip install -e .
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Contributing
|
|
219
|
+
|
|
220
|
+
Contributions are welcome! See the [MarkItDown repository](https://github.com/microsoft/markitdown) for guidelines.
|
|
221
|
+
|
|
222
|
+
## License
|
|
223
|
+
|
|
224
|
+
MIT — see [LICENSE](LICENSE).
|
|
225
|
+
|
|
226
|
+
## Changelog
|
|
227
|
+
|
|
228
|
+
### 0.1.0 (Initial Release)
|
|
229
|
+
|
|
230
|
+
- LLM Vision OCR for PDF, DOCX, PPTX, XLSX
|
|
231
|
+
- Full-page OCR fallback for scanned PDFs
|
|
232
|
+
- Context-aware inline text insertion
|
|
233
|
+
- Priority-based converter replacement (no code changes required)
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
# MarkItDown OCR Plugin
|
|
2
|
+
|
|
3
|
+
LLM Vision plugin for MarkItDown that extracts text from images embedded in PDF, DOCX, PPTX, and XLSX files.
|
|
4
|
+
|
|
5
|
+
Uses the same `llm_client` / `llm_model` pattern that MarkItDown already supports for image descriptions — no new ML libraries or binary dependencies required.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Enhanced PDF Converter**: Extracts text from images within PDFs, with full-page OCR fallback for scanned documents
|
|
10
|
+
- **Enhanced DOCX Converter**: OCR for images in Word documents
|
|
11
|
+
- **Enhanced PPTX Converter**: OCR for images in PowerPoint presentations
|
|
12
|
+
- **Enhanced XLSX Converter**: OCR for images in Excel spreadsheets
|
|
13
|
+
- **Context Preservation**: Maintains document structure and flow when inserting extracted text
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install markitdown-ocr
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The plugin uses whatever OpenAI-compatible client you already have. Install one if you don't have it yet:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install openai
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
### Command Line
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
markitdown document.pdf --use-plugins --llm-client openai --llm-model gpt-4o
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Python API
|
|
36
|
+
|
|
37
|
+
Pass `llm_client` and `llm_model` to `MarkItDown()` exactly as you would for image descriptions:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from markitdown import MarkItDown
|
|
41
|
+
from openai import OpenAI
|
|
42
|
+
|
|
43
|
+
md = MarkItDown(
|
|
44
|
+
enable_plugins=True,
|
|
45
|
+
llm_client=OpenAI(),
|
|
46
|
+
llm_model="gpt-4o",
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
result = md.convert("document_with_images.pdf")
|
|
50
|
+
print(result.text_content)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
If no `llm_client` is provided the plugin still loads, but OCR is silently skipped — falling back to the standard built-in converter.
|
|
54
|
+
|
|
55
|
+
### Custom Prompt
|
|
56
|
+
|
|
57
|
+
Override the default extraction prompt for specialized documents:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
md = MarkItDown(
|
|
61
|
+
enable_plugins=True,
|
|
62
|
+
llm_client=OpenAI(),
|
|
63
|
+
llm_model="gpt-4o",
|
|
64
|
+
llm_prompt="Extract all text from this image, preserving table structure.",
|
|
65
|
+
)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Any OpenAI-Compatible Client
|
|
69
|
+
|
|
70
|
+
Works with any client that follows the OpenAI API:
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from openai import AzureOpenAI
|
|
74
|
+
|
|
75
|
+
md = MarkItDown(
|
|
76
|
+
enable_plugins=True,
|
|
77
|
+
llm_client=AzureOpenAI(
|
|
78
|
+
api_key="...",
|
|
79
|
+
azure_endpoint="https://your-resource.openai.azure.com/",
|
|
80
|
+
api_version="2024-02-01",
|
|
81
|
+
),
|
|
82
|
+
llm_model="gpt-4o",
|
|
83
|
+
)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## How It Works
|
|
87
|
+
|
|
88
|
+
When `MarkItDown(enable_plugins=True, llm_client=..., llm_model=...)` is called:
|
|
89
|
+
|
|
90
|
+
1. MarkItDown discovers the plugin via the `markitdown.plugin` entry point group
|
|
91
|
+
2. It calls `register_converters()`, forwarding all kwargs including `llm_client` and `llm_model`
|
|
92
|
+
3. The plugin creates an `LLMVisionOCRService` from those kwargs
|
|
93
|
+
4. Four OCR-enhanced converters are registered at **priority -1.0** — before the built-in converters at priority 0.0
|
|
94
|
+
|
|
95
|
+
When a file is converted:
|
|
96
|
+
|
|
97
|
+
1. The OCR converter accepts the file
|
|
98
|
+
2. It extracts embedded images from the document
|
|
99
|
+
3. Each image is sent to the LLM with an extraction prompt
|
|
100
|
+
4. The returned text is inserted inline, preserving document structure
|
|
101
|
+
5. If the LLM call fails, conversion continues without that image's text
|
|
102
|
+
|
|
103
|
+
## Supported File Formats
|
|
104
|
+
|
|
105
|
+
### PDF
|
|
106
|
+
|
|
107
|
+
- Embedded images are extracted by position (via `page.images` / page XObjects) and OCR'd inline, interleaved with the surrounding text in vertical reading order.
|
|
108
|
+
- **Scanned PDFs** (pages with no extractable text) are detected automatically: each page is rendered at 300 DPI and sent to the LLM as a full-page image.
|
|
109
|
+
- **Malformed PDFs** that pdfplumber/pdfminer cannot open (e.g. truncated EOF) are retried with PyMuPDF page rendering, so content is still recovered.
|
|
110
|
+
|
|
111
|
+
### DOCX
|
|
112
|
+
|
|
113
|
+
- Images are extracted via document part relationships (`doc.part.rels`).
|
|
114
|
+
- OCR is run before the DOCX→HTML→Markdown pipeline executes: placeholder tokens are injected into the HTML so that the markdown converter does not escape the OCR markers, and the final placeholders are replaced with the formatted `*[Image OCR]...[End OCR]*` blocks after conversion.
|
|
115
|
+
- Document flow (headings, paragraphs, tables) is fully preserved around the OCR blocks.
|
|
116
|
+
|
|
117
|
+
### PPTX
|
|
118
|
+
|
|
119
|
+
- Picture shapes, placeholder shapes with images, and images inside groups are all supported.
|
|
120
|
+
- Shapes are processed in top-to-left reading order per slide.
|
|
121
|
+
- If an `llm_client` is configured, the LLM is asked for a description first; OCR is used as the fallback when no description is returned.
|
|
122
|
+
|
|
123
|
+
### XLSX
|
|
124
|
+
|
|
125
|
+
- Images embedded in worksheets (`sheet._images`) are extracted per sheet.
|
|
126
|
+
- Cell position is calculated from the image anchor coordinates (column/row → Excel letter notation).
|
|
127
|
+
- Images are listed under a `### Images in this sheet:` section after the sheet's data table — they are not interleaved into the table rows.
|
|
128
|
+
|
|
129
|
+
### Output format
|
|
130
|
+
|
|
131
|
+
Every extracted OCR block is wrapped as:
|
|
132
|
+
|
|
133
|
+
```text
|
|
134
|
+
*[Image OCR]
|
|
135
|
+
<extracted text>
|
|
136
|
+
[End OCR]*
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Troubleshooting
|
|
140
|
+
|
|
141
|
+
### OCR text missing from output
|
|
142
|
+
|
|
143
|
+
The most likely cause is a missing `llm_client` or `llm_model`. Verify:
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
from openai import OpenAI
|
|
147
|
+
from markitdown import MarkItDown
|
|
148
|
+
|
|
149
|
+
md = MarkItDown(
|
|
150
|
+
enable_plugins=True,
|
|
151
|
+
llm_client=OpenAI(), # required
|
|
152
|
+
llm_model="gpt-4o", # required
|
|
153
|
+
)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Plugin not loading
|
|
157
|
+
|
|
158
|
+
Confirm the plugin is installed and discovered:
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
markitdown --list-plugins # should show: ocr
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### API errors
|
|
165
|
+
|
|
166
|
+
The plugin propagates LLM API errors as warnings and continues conversion. Check your API key, quota, and that the chosen model supports vision inputs.
|
|
167
|
+
|
|
168
|
+
## Development
|
|
169
|
+
|
|
170
|
+
### Running Tests
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
cd packages/markitdown-ocr
|
|
174
|
+
pytest tests/ -v
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Building from Source
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
git clone https://github.com/microsoft/markitdown.git
|
|
181
|
+
cd markitdown/packages/markitdown-ocr
|
|
182
|
+
pip install -e .
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Contributing
|
|
186
|
+
|
|
187
|
+
Contributions are welcome! See the [MarkItDown repository](https://github.com/microsoft/markitdown) for guidelines.
|
|
188
|
+
|
|
189
|
+
## License
|
|
190
|
+
|
|
191
|
+
MIT — see [LICENSE](LICENSE).
|
|
192
|
+
|
|
193
|
+
## Changelog
|
|
194
|
+
|
|
195
|
+
### 0.1.0 (Initial Release)
|
|
196
|
+
|
|
197
|
+
- LLM Vision OCR for PDF, DOCX, PPTX, XLSX
|
|
198
|
+
- Full-page OCR fallback for scanned PDFs
|
|
199
|
+
- Context-aware inline text insertion
|
|
200
|
+
- Priority-based converter replacement (no code changes required)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "markitdown-ocr"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = 'OCR plugin for MarkItDown - Extracts text from images in PDF, DOCX, PPTX, and XLSX via LLM Vision'
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
keywords = ["markitdown", "ocr", "pdf", "docx", "xlsx", "pptx", "llm", "vision"]
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "Contributors", email = "noreply@github.com" },
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Programming Language :: Python",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Programming Language :: Python :: Implementation :: CPython",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
# Core dependencies — matches the file-format libraries markitdown already uses
|
|
27
|
+
dependencies = [
|
|
28
|
+
"markitdown>=0.1.0",
|
|
29
|
+
"pdfminer.six>=20251230",
|
|
30
|
+
"pdfplumber>=0.11.9",
|
|
31
|
+
"PyMuPDF>=1.24.0",
|
|
32
|
+
"mammoth~=1.11.0",
|
|
33
|
+
"python-docx",
|
|
34
|
+
"python-pptx",
|
|
35
|
+
"pandas",
|
|
36
|
+
"openpyxl",
|
|
37
|
+
"Pillow>=9.0.0",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
# llm_client is passed in by the user (same as for markitdown image descriptions);
|
|
41
|
+
# install openai or any OpenAI-compatible SDK separately.
|
|
42
|
+
[project.optional-dependencies]
|
|
43
|
+
llm = [
|
|
44
|
+
"openai>=1.0.0",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[project.urls]
|
|
48
|
+
Documentation = "https://github.com/microsoft/markitdown#readme"
|
|
49
|
+
Issues = "https://github.com/microsoft/markitdown/issues"
|
|
50
|
+
Source = "https://github.com/microsoft/markitdown"
|
|
51
|
+
|
|
52
|
+
[tool.hatch.version]
|
|
53
|
+
path = "src/markitdown_ocr/__about__.py"
|
|
54
|
+
|
|
55
|
+
# CRITICAL: Plugin entry point - MarkItDown will discover this plugin through this entry point
|
|
56
|
+
[project.entry-points."markitdown.plugin"]
|
|
57
|
+
ocr = "markitdown_ocr"
|