diff --git a/.gitattributes b/.gitattributes index f787c0e47..304de55dd 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,5 @@ packages/markitdown/tests/test_files/** linguist-vendored packages/markitdown-sample-plugin/tests/test_files/** linguist-vendored + +# Treat PDF files as binary to prevent line ending conversion +*.pdf binary diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 321f82336..09454647b 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -5,7 +5,7 @@ jobs: pre-commit: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 - name: Set up Python uses: actions/setup-python@v5 with: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 78c7cdc1a..4785bba1a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -5,7 +5,7 @@ jobs: tests: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 - uses: actions/setup-python@v5 with: python-version: | diff --git a/.gitignore b/.gitignore index aa4abd389..15613ea8a 100644 --- a/.gitignore +++ b/.gitignore @@ -52,6 +52,7 @@ coverage.xml .hypothesis/ .pytest_cache/ cover/ +.test-logs/ # Translations *.mo diff --git a/README.md b/README.md index e030c408a..6da3ee1d9 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ > [!IMPORTANT] > Breaking changes between 0.0.1 to 0.1.0: -> * Dependencies are now organized into optional feature-groups (further details below). Use `pip install 'markitdown[all]'` to have backward-compatible behavior. +> * Dependencies are now organized into optional feature-groups (further details below). Use `pip install 'markitdown[all]'` to have backward-compatible behavior. > * convert\_stream() now requires a binary file-like object (e.g., a file opened in binary mode, or an io.BytesIO object). This is a breaking change from the previous version, where it previously also accepted text file-like objects, like io.StringIO. > * The DocumentConverter class interface has changed to read from file-like streams rather than file paths. *No temporary files are created anymore*. If you are the maintainer of a plugin, or custom DocumentConverter, you likely need to update your code. Otherwise, if only using the MarkItDown class or CLI (as in these examples), you should not need to change anything. @@ -132,6 +132,38 @@ markitdown --use-plugins path-to-file.pdf To find available plugins, search GitHub for the hashtag `#markitdown-plugin`. To develop a plugin, see `packages/markitdown-sample-plugin`. +#### markitdown-ocr Plugin + +The `markitdown-ocr` plugin adds OCR support to PDF, DOCX, PPTX, and XLSX converters, extracting text from embedded images using LLM Vision — the same `llm_client` / `llm_model` pattern that MarkItDown already uses for image descriptions. No new ML libraries or binary dependencies required. + +**Installation:** + +```bash +pip install markitdown-ocr +pip install openai # or any OpenAI-compatible client +``` + +**Usage:** + +Pass the same `llm_client` and `llm_model` you would use for image descriptions: + +```python +from markitdown import MarkItDown +from openai import OpenAI + +md = MarkItDown( + enable_plugins=True, + llm_client=OpenAI(), + llm_model="gpt-4o", +) +result = md.convert("document_with_images.pdf") +print(result.text_content) +``` + +If no `llm_client` is provided the plugin still loads, but OCR is silently skipped and the standard built-in converter is used instead. + +See [`packages/markitdown-ocr/README.md`](packages/markitdown-ocr/README.md) for detailed documentation. + ### Azure Document Intelligence To use Microsoft Document Intelligence for conversion: @@ -164,14 +196,14 @@ result = md.convert("test.pdf") print(result.text_content) ``` -To use Large Language Models for image descriptions, provide `llm_client` and `llm_model`: +To use Large Language Models for image descriptions (currently only for pptx and image files), provide `llm_client` and `llm_model`: ```python from markitdown import MarkItDown from openai import OpenAI client = OpenAI() -md = MarkItDown(llm_client=client, llm_model="gpt-4o") +md = MarkItDown(llm_client=client, llm_model="gpt-4o", llm_prompt="optional custom prompt") result = md.convert("example.jpg") print(result.text_content) ``` @@ -199,7 +231,7 @@ contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additio ### How to Contribute -You can help by looking at issues or helping review PRs. Any issue or PR is welcome, but we have also marked some as 'open for contribution' and 'open for reviewing' to help facilitate community contributions. These are ofcourse just suggestions and you are welcome to contribute in any way you like. +You can help by looking at issues or helping review PRs. Any issue or PR is welcome, but we have also marked some as 'open for contribution' and 'open for reviewing' to help facilitate community contributions. These are of course just suggestions and you are welcome to contribute in any way you like.
diff --git a/packages/markitdown-mcp/README.md b/packages/markitdown-mcp/README.md index 4c9cff836..d7e9224b4 100644 --- a/packages/markitdown-mcp/README.md +++ b/packages/markitdown-mcp/README.md @@ -1,5 +1,9 @@ # MarkItDown-MCP +> [!IMPORTANT] +> The MarkItDown-MCP package is meant for **local use**, with local trusted agents. In particular, when running the MCP server with Streamable HTTP or SSE, it binds to `localhost` by default, and is not exposed to other machines on the network or Internet. In this configuration, it is meant to be a direct alternative to the STDIO transport, which may be more convenient in some cases. DO NOT bind the server to other interfaces unless you understand the [security implications](#security-considerations) of doing so. + + [![PyPI](https://img.shields.io/pypi/v/markitdown-mcp.svg)](https://pypi.org/project/markitdown-mcp/) ![PyPI - Downloads](https://img.shields.io/pypi/dd/markitdown-mcp) [![Built by AutoGen Team](https://img.shields.io/badge/Built%20by-AutoGen%20Team-blue)](https://github.com/microsoft/autogen) @@ -18,14 +22,14 @@ pip install markitdown-mcp ## Usage -To run the MCP server, using STDIO (default) use the following command: +To run the MCP server, using STDIO (default), use the following command: ```bash markitdown-mcp ``` -To run the MCP server, using Streamable HTTP and SSE use the following command: +To run the MCP server, using Streamable HTTP and SSE, use the following command: ```bash markitdown-mcp --http --host 127.0.0.1 --port 3001 @@ -54,7 +58,7 @@ Once mounted, all files under data will be accessible under `/workdir` in the co It is recommended to use the Docker image when running the MCP server for Claude Desktop. -Follow [these instrutions](https://modelcontextprotocol.io/quickstart/user#for-claude-desktop-users) to access Claude's `claude_desktop_config.json` file. +Follow [these instructions](https://modelcontextprotocol.io/quickstart/user#for-claude-desktop-users) to access Claude's `claude_desktop_config.json` file. Edit it to include the following JSON entry: @@ -96,13 +100,13 @@ If you want to mount a directory, adjust it accordingly: ## Debugging -To debug the MCP server you can use the `mcpinspector` tool. +To debug the MCP server you can use the `MCP Inspector` tool. ```bash npx @modelcontextprotocol/inspector ``` -You can then connect to the insepctor through the specified host and port (e.g., `http://localhost:5173/`). +You can then connect to the inspector through the specified host and port (e.g., `http://localhost:5173/`). If using STDIO: * select `STDIO` as the transport type, @@ -127,8 +131,7 @@ Finally: ## Security Considerations -The server does not support authentication, and runs with the privileges if the user running it. For this reason, when running in SSE or Streamable HTTP mode, it is recommended to run the server bound to `localhost` (default). - +The server does not support authentication, and runs with the privileges of the user running it. For this reason, when running in SSE or Streamable HTTP mode, the server binds by default to `localhost`. Even still, it is important to recognize that the server can be accessed by any process or users on the same local machine, and that the `convert_to_markdown` tool can be used to read any file that the server's user has access to, or any data from the network. If you require additional security, consider running the server in a sandboxed environment, such as a virtual machine or container, and ensure that the user permissions are properly configured to limit access to sensitive files and network segments. Above all, DO NOT bind the server to other interfaces (non-localhost) unless you understand the security implications of doing so. ## Trademarks diff --git a/packages/markitdown-mcp/src/markitdown_mcp/__about__.py b/packages/markitdown-mcp/src/markitdown_mcp/__about__.py index d5c2ca558..64be02b7e 100644 --- a/packages/markitdown-mcp/src/markitdown_mcp/__about__.py +++ b/packages/markitdown-mcp/src/markitdown_mcp/__about__.py @@ -1,4 +1,4 @@ # SPDX-FileCopyrightText: 2024-present Adam Fourney # # SPDX-License-Identifier: MIT -__version__ = "0.0.1a4" +__version__ = "0.0.1a5" diff --git a/packages/markitdown-mcp/src/markitdown_mcp/__main__.py b/packages/markitdown-mcp/src/markitdown_mcp/__main__.py index 77ade9974..89f89444e 100644 --- a/packages/markitdown-mcp/src/markitdown_mcp/__main__.py +++ b/packages/markitdown-mcp/src/markitdown_mcp/__main__.py @@ -113,10 +113,23 @@ def main(): sys.exit(1) if use_http: + host = args.host if args.host else "127.0.0.1" + if args.host and args.host not in ("127.0.0.1", "localhost"): + print( + "\n" + "WARNING: The server is being bound to a non-localhost interface " + f"({host}).\n" + "This exposes the server to other machines on the network or Internet.\n" + "The server has NO authentication and runs with your user's privileges.\n" + "Any process or user that can reach this interface can read files and\n" + "fetch network resources accessible to this user.\n" + "Only proceed if you understand the security implications.\n", + file=sys.stderr, + ) starlette_app = create_starlette_app(mcp_server, debug=True) uvicorn.run( starlette_app, - host=args.host if args.host else "127.0.0.1", + host=host, port=args.port if args.port else 3001, ) else: diff --git a/packages/markitdown-ocr/LICENSE b/packages/markitdown-ocr/LICENSE new file mode 100644 index 000000000..9e841e7a2 --- /dev/null +++ b/packages/markitdown-ocr/LICENSE @@ -0,0 +1,21 @@ + MIT License + + Copyright (c) Microsoft Corporation. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE diff --git a/packages/markitdown-ocr/README.md b/packages/markitdown-ocr/README.md new file mode 100644 index 000000000..d0883db4a --- /dev/null +++ b/packages/markitdown-ocr/README.md @@ -0,0 +1,200 @@ +# MarkItDown OCR Plugin + +LLM Vision plugin for MarkItDown that extracts text from images embedded in PDF, DOCX, PPTX, and XLSX files. + +Uses the same `llm_client` / `llm_model` pattern that MarkItDown already supports for image descriptions — no new ML libraries or binary dependencies required. + +## Features + +- **Enhanced PDF Converter**: Extracts text from images within PDFs, with full-page OCR fallback for scanned documents +- **Enhanced DOCX Converter**: OCR for images in Word documents +- **Enhanced PPTX Converter**: OCR for images in PowerPoint presentations +- **Enhanced XLSX Converter**: OCR for images in Excel spreadsheets +- **Context Preservation**: Maintains document structure and flow when inserting extracted text + +## Installation + +```bash +pip install markitdown-ocr +``` + +The plugin uses whatever OpenAI-compatible client you already have. Install one if you don't have it yet: + +```bash +pip install openai +``` + +## Usage + +### Command Line + +```bash +markitdown document.pdf --use-plugins --llm-client openai --llm-model gpt-4o +``` + +### Python API + +Pass `llm_client` and `llm_model` to `MarkItDown()` exactly as you would for image descriptions: + +```python +from markitdown import MarkItDown +from openai import OpenAI + +md = MarkItDown( + enable_plugins=True, + llm_client=OpenAI(), + llm_model="gpt-4o", +) + +result = md.convert("document_with_images.pdf") +print(result.text_content) +``` + +If no `llm_client` is provided the plugin still loads, but OCR is silently skipped — falling back to the standard built-in converter. + +### Custom Prompt + +Override the default extraction prompt for specialized documents: + +```python +md = MarkItDown( + enable_plugins=True, + llm_client=OpenAI(), + llm_model="gpt-4o", + llm_prompt="Extract all text from this image, preserving table structure.", +) +``` + +### Any OpenAI-Compatible Client + +Works with any client that follows the OpenAI API: + +```python +from openai import AzureOpenAI + +md = MarkItDown( + enable_plugins=True, + llm_client=AzureOpenAI( + api_key="...", + azure_endpoint="https://your-resource.openai.azure.com/", + api_version="2024-02-01", + ), + llm_model="gpt-4o", +) +``` + +## How It Works + +When `MarkItDown(enable_plugins=True, llm_client=..., llm_model=...)` is called: + +1. MarkItDown discovers the plugin via the `markitdown.plugin` entry point group +2. It calls `register_converters()`, forwarding all kwargs including `llm_client` and `llm_model` +3. The plugin creates an `LLMVisionOCRService` from those kwargs +4. Four OCR-enhanced converters are registered at **priority -1.0** — before the built-in converters at priority 0.0 + +When a file is converted: + +1. The OCR converter accepts the file +2. It extracts embedded images from the document +3. Each image is sent to the LLM with an extraction prompt +4. The returned text is inserted inline, preserving document structure +5. If the LLM call fails, conversion continues without that image's text + +## Supported File Formats + +### PDF + +- Embedded images are extracted by position (via `page.images` / page XObjects) and OCR'd inline, interleaved with the surrounding text in vertical reading order. +- **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. +- **Malformed PDFs** that pdfplumber/pdfminer cannot open (e.g. truncated EOF) are retried with PyMuPDF page rendering, so content is still recovered. + +### DOCX + +- Images are extracted via document part relationships (`doc.part.rels`). +- 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. +- Document flow (headings, paragraphs, tables) is fully preserved around the OCR blocks. + +### PPTX + +- Picture shapes, placeholder shapes with images, and images inside groups are all supported. +- Shapes are processed in top-to-left reading order per slide. +- 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. + +### XLSX + +- Images embedded in worksheets (`sheet._images`) are extracted per sheet. +- Cell position is calculated from the image anchor coordinates (column/row → Excel letter notation). +- Images are listed under a `### Images in this sheet:` section after the sheet's data table — they are not interleaved into the table rows. + +### Output format + +Every extracted OCR block is wrapped as: + +```text +*[Image OCR] + +[End OCR]* +``` + +## Troubleshooting + +### OCR text missing from output + +The most likely cause is a missing `llm_client` or `llm_model`. Verify: + +```python +from openai import OpenAI +from markitdown import MarkItDown + +md = MarkItDown( + enable_plugins=True, + llm_client=OpenAI(), # required + llm_model="gpt-4o", # required +) +``` + +### Plugin not loading + +Confirm the plugin is installed and discovered: + +```bash +markitdown --list-plugins # should show: ocr +``` + +### API errors + +The plugin propagates LLM API errors as warnings and continues conversion. Check your API key, quota, and that the chosen model supports vision inputs. + +## Development + +### Running Tests + +```bash +cd packages/markitdown-ocr +pytest tests/ -v +``` + +### Building from Source + +```bash +git clone https://github.com/microsoft/markitdown.git +cd markitdown/packages/markitdown-ocr +pip install -e . +``` + +## Contributing + +Contributions are welcome! See the [MarkItDown repository](https://github.com/microsoft/markitdown) for guidelines. + +## License + +MIT — see [LICENSE](LICENSE). + +## Changelog + +### 0.1.0 (Initial Release) + +- LLM Vision OCR for PDF, DOCX, PPTX, XLSX +- Full-page OCR fallback for scanned PDFs +- Context-aware inline text insertion +- Priority-based converter replacement (no code changes required) diff --git a/packages/markitdown-ocr/pyproject.toml b/packages/markitdown-ocr/pyproject.toml new file mode 100644 index 000000000..eda3cdda5 --- /dev/null +++ b/packages/markitdown-ocr/pyproject.toml @@ -0,0 +1,57 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "markitdown-ocr" +dynamic = ["version"] +description = 'OCR plugin for MarkItDown - Extracts text from images in PDF, DOCX, PPTX, and XLSX via LLM Vision' +readme = "README.md" +requires-python = ">=3.10" +license = "MIT" +keywords = ["markitdown", "ocr", "pdf", "docx", "xlsx", "pptx", "llm", "vision"] +authors = [ + { name = "Contributors", email = "noreply@github.com" }, +] +classifiers = [ + "Development Status :: 4 - Beta", + "Programming Language :: Python", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: Implementation :: CPython", +] + +# Core dependencies — matches the file-format libraries markitdown already uses +dependencies = [ + "markitdown>=0.1.0", + "pdfminer.six>=20251230", + "pdfplumber>=0.11.9", + "PyMuPDF>=1.24.0", + "mammoth~=1.11.0", + "python-docx", + "python-pptx", + "pandas", + "openpyxl", + "Pillow>=9.0.0", +] + +# llm_client is passed in by the user (same as for markitdown image descriptions); +# install openai or any OpenAI-compatible SDK separately. +[project.optional-dependencies] +llm = [ + "openai>=1.0.0", +] + +[project.urls] +Documentation = "https://github.com/microsoft/markitdown#readme" +Issues = "https://github.com/microsoft/markitdown/issues" +Source = "https://github.com/microsoft/markitdown" + +[tool.hatch.version] +path = "src/markitdown_ocr/__about__.py" + +# CRITICAL: Plugin entry point - MarkItDown will discover this plugin through this entry point +[project.entry-points."markitdown.plugin"] +ocr = "markitdown_ocr" diff --git a/packages/markitdown-ocr/src/markitdown_ocr/__about__.py b/packages/markitdown-ocr/src/markitdown_ocr/__about__.py new file mode 100644 index 000000000..1c700dc50 --- /dev/null +++ b/packages/markitdown-ocr/src/markitdown_ocr/__about__.py @@ -0,0 +1,4 @@ +# SPDX-FileCopyrightText: 2025-present Contributors +# SPDX-License-Identifier: MIT + +__version__ = "0.1.0" diff --git a/packages/markitdown-ocr/src/markitdown_ocr/__init__.py b/packages/markitdown-ocr/src/markitdown_ocr/__init__.py new file mode 100644 index 000000000..f608e9625 --- /dev/null +++ b/packages/markitdown-ocr/src/markitdown_ocr/__init__.py @@ -0,0 +1,31 @@ +# SPDX-FileCopyrightText: 2025-present Contributors +# SPDX-License-Identifier: MIT + +""" +markitdown-ocr: OCR plugin for MarkItDown + +Adds LLM Vision-based text extraction from images embedded in PDF, DOCX, PPTX, and XLSX files. +""" + +from ._plugin import __plugin_interface_version__, register_converters +from .__about__ import __version__ +from ._ocr_service import ( + OCRResult, + LLMVisionOCRService, +) +from ._pdf_converter_with_ocr import PdfConverterWithOCR +from ._docx_converter_with_ocr import DocxConverterWithOCR +from ._pptx_converter_with_ocr import PptxConverterWithOCR +from ._xlsx_converter_with_ocr import XlsxConverterWithOCR + +__all__ = [ + "__version__", + "__plugin_interface_version__", + "register_converters", + "OCRResult", + "LLMVisionOCRService", + "PdfConverterWithOCR", + "DocxConverterWithOCR", + "PptxConverterWithOCR", + "XlsxConverterWithOCR", +] diff --git a/packages/markitdown-ocr/src/markitdown_ocr/_docx_converter_with_ocr.py b/packages/markitdown-ocr/src/markitdown_ocr/_docx_converter_with_ocr.py new file mode 100644 index 000000000..f2463de11 --- /dev/null +++ b/packages/markitdown-ocr/src/markitdown_ocr/_docx_converter_with_ocr.py @@ -0,0 +1,189 @@ +""" +Enhanced DOCX Converter with OCR support for embedded images. +Extracts images from Word documents and performs OCR while maintaining context. +""" + +import io +import re +import sys +from typing import Any, BinaryIO, Optional + +from markitdown.converters import HtmlConverter +from markitdown.converter_utils.docx.pre_process import pre_process_docx +from markitdown import DocumentConverterResult, StreamInfo +from markitdown._exceptions import ( + MissingDependencyException, + MISSING_DEPENDENCY_MESSAGE, +) +from ._ocr_service import LLMVisionOCRService + +# Try loading dependencies +_dependency_exc_info = None +try: + import mammoth + from docx import Document +except ImportError: + _dependency_exc_info = sys.exc_info() + +# Placeholder injected into HTML so that mammoth never sees the OCR markers. +# Must be a single token with no special markdown characters. +_PLACEHOLDER = "MARKITDOWNOCRBLOCK{}" + + +class DocxConverterWithOCR(HtmlConverter): + """ + Enhanced DOCX Converter with OCR support for embedded images. + Maintains document flow while extracting text from images inline. + """ + + def __init__(self, ocr_service: Optional[LLMVisionOCRService] = None): + super().__init__() + self._html_converter = HtmlConverter() + self.ocr_service = ocr_service + + def accepts( + self, + file_stream: BinaryIO, + stream_info: StreamInfo, + **kwargs: Any, + ) -> bool: + mimetype = (stream_info.mimetype or "").lower() + extension = (stream_info.extension or "").lower() + + if extension == ".docx": + return True + + if mimetype.startswith( + "application/vnd.openxmlformats-officedocument.wordprocessingml" + ): + return True + + return False + + def convert( + self, + file_stream: BinaryIO, + stream_info: StreamInfo, + **kwargs: Any, + ) -> DocumentConverterResult: + if _dependency_exc_info is not None: + raise MissingDependencyException( + MISSING_DEPENDENCY_MESSAGE.format( + converter=type(self).__name__, + extension=".docx", + feature="docx", + ) + ) from _dependency_exc_info[1].with_traceback( + _dependency_exc_info[2] + ) # type: ignore[union-attr] + + # Get OCR service if available (from kwargs or instance) + ocr_service: Optional[LLMVisionOCRService] = ( + kwargs.get("ocr_service") or self.ocr_service + ) + + if ocr_service: + # 1. Extract and OCR images — returns raw text per image + file_stream.seek(0) + image_ocr_map = self._extract_and_ocr_images(file_stream, ocr_service) + + # 2. Convert DOCX → HTML via mammoth + file_stream.seek(0) + pre_process_stream = pre_process_docx(file_stream) + html_result = mammoth.convert_to_html( + pre_process_stream, style_map=kwargs.get("style_map") + ).value + + # 3. Replace tags with plain placeholder tokens so that + # mammoth's HTML→markdown step never escapes our OCR markers. + html_with_placeholders, ocr_texts = self._inject_placeholders( + html_result, image_ocr_map + ) + + # 4. Convert HTML → markdown + md_result = self._html_converter.convert_string( + html_with_placeholders, **kwargs + ) + md = md_result.markdown + + # 5. Swap placeholders for the actual OCR blocks (post-conversion + # so * and _ are never escaped by the markdown converter). + for i, raw_text in enumerate(ocr_texts): + placeholder = _PLACEHOLDER.format(i) + ocr_block = f"*[Image OCR]\n{raw_text}\n[End OCR]*" + md = md.replace(placeholder, ocr_block) + + return DocumentConverterResult(markdown=md) + else: + # Standard conversion without OCR + style_map = kwargs.get("style_map", None) + pre_process_stream = pre_process_docx(file_stream) + return self._html_converter.convert_string( + mammoth.convert_to_html(pre_process_stream, style_map=style_map).value, + **kwargs, + ) + + def _extract_and_ocr_images( + self, file_stream: BinaryIO, ocr_service: LLMVisionOCRService + ) -> dict[str, str]: + """ + Extract images from DOCX and OCR them. + + Returns: + Dict mapping image relationship IDs to raw OCR text (no markers). + """ + ocr_map = {} + + try: + file_stream.seek(0) + doc = Document(file_stream) + + for rel in doc.part.rels.values(): + if "image" in rel.target_ref.lower(): + try: + image_bytes = rel.target_part.blob + image_stream = io.BytesIO(image_bytes) + ocr_result = ocr_service.extract_text(image_stream) + + if ocr_result.text.strip(): + # Store raw text only — markers added later + ocr_map[rel.rId] = ocr_result.text.strip() + + except Exception: + continue + + except Exception: + pass + + return ocr_map + + def _inject_placeholders( + self, html: str, ocr_map: dict[str, str] + ) -> tuple[str, list[str]]: + """ + Replace tags with numbered placeholder tokens. + + Returns: + (html_with_placeholders, ordered list of raw OCR texts) + """ + if not ocr_map: + return html, [] + + ocr_texts = list(ocr_map.values()) + used: list[int] = [] + + def replace_img(match: re.Match) -> str: # type: ignore[type-arg] + for i in range(len(ocr_texts)): + if i not in used: + used.append(i) + return f"

{_PLACEHOLDER.format(i)}

" + return "" # remove image if all OCR texts already used + + result = re.sub(r"]*>", replace_img, html) + + # Any OCR texts that had no matching tag go at the end + for i in range(len(ocr_texts)): + if i not in used: + result += f"

{_PLACEHOLDER.format(i)}

" + + return result, ocr_texts diff --git a/packages/markitdown-ocr/src/markitdown_ocr/_ocr_service.py b/packages/markitdown-ocr/src/markitdown_ocr/_ocr_service.py new file mode 100644 index 000000000..2885e1f47 --- /dev/null +++ b/packages/markitdown-ocr/src/markitdown_ocr/_ocr_service.py @@ -0,0 +1,110 @@ +""" +OCR Service Layer for MarkItDown +Provides LLM Vision-based image text extraction. +""" + +import base64 +from typing import Any, BinaryIO +from dataclasses import dataclass + +from markitdown import StreamInfo + + +@dataclass +class OCRResult: + """Result from OCR extraction.""" + + text: str + confidence: float | None = None + backend_used: str | None = None + error: str | None = None + + +class LLMVisionOCRService: + """OCR service using LLM vision models (OpenAI-compatible).""" + + def __init__( + self, + client: Any, + model: str, + default_prompt: str | None = None, + ) -> None: + """ + Initialize LLM Vision OCR service. + + Args: + client: OpenAI-compatible client + model: Model name (e.g., 'gpt-4o', 'gemini-2.0-flash') + default_prompt: Default prompt for OCR extraction + """ + self.client = client + self.model = model + self.default_prompt = default_prompt or ( + "Extract all text from this image. " + "Return ONLY the extracted text, maintaining the original " + "layout and order. Do not add any commentary or description." + ) + + def extract_text( + self, + image_stream: BinaryIO, + prompt: str | None = None, + stream_info: StreamInfo | None = None, + **kwargs: Any, + ) -> OCRResult: + """Extract text using LLM vision.""" + if self.client is None: + return OCRResult( + text="", + backend_used="llm_vision", + error="LLM client not configured", + ) + + try: + image_stream.seek(0) + + content_type: str | None = None + if stream_info: + content_type = stream_info.mimetype + + if not content_type: + try: + from PIL import Image + + image_stream.seek(0) + img = Image.open(image_stream) + fmt = img.format.lower() if img.format else "png" + content_type = f"image/{fmt}" + except Exception: + content_type = "image/png" + + image_stream.seek(0) + base64_image = base64.b64encode(image_stream.read()).decode("utf-8") + data_uri = f"data:{content_type};base64,{base64_image}" + + actual_prompt = prompt or self.default_prompt + response = self.client.chat.completions.create( + model=self.model, + messages=[ + { + "role": "user", + "content": [ + {"type": "text", "text": actual_prompt}, + { + "type": "image_url", + "image_url": {"url": data_uri}, + }, + ], + } + ], + ) + + text = response.choices[0].message.content + return OCRResult( + text=text.strip() if text else "", + backend_used="llm_vision", + ) + except Exception as e: + return OCRResult(text="", backend_used="llm_vision", error=str(e)) + finally: + image_stream.seek(0) diff --git a/packages/markitdown-ocr/src/markitdown_ocr/_pdf_converter_with_ocr.py b/packages/markitdown-ocr/src/markitdown_ocr/_pdf_converter_with_ocr.py new file mode 100644 index 000000000..c1dc0f613 --- /dev/null +++ b/packages/markitdown-ocr/src/markitdown_ocr/_pdf_converter_with_ocr.py @@ -0,0 +1,422 @@ +""" +Enhanced PDF Converter with OCR support for embedded images. +Extracts images from PDFs and performs OCR while maintaining document context. +""" + +import io +import sys +from typing import Any, BinaryIO, Optional + +from markitdown import DocumentConverter, DocumentConverterResult, StreamInfo +from markitdown._exceptions import ( + MissingDependencyException, + MISSING_DEPENDENCY_MESSAGE, +) +from ._ocr_service import LLMVisionOCRService + +# Import dependencies +_dependency_exc_info = None +try: + import pdfminer + import pdfminer.high_level + import pdfplumber + from PIL import Image +except ImportError: + _dependency_exc_info = sys.exc_info() + + +def _extract_images_from_page(page: Any) -> list[dict]: + """ + Extract images from a PDF page by rendering page regions. + + Returns: + List of dicts with 'stream', 'bbox', 'name', 'y_pos' keys + """ + images_info = [] + + try: + # Try multiple methods to detect images + images = [] + + # Method 1: Use page.images (standard approach) + if hasattr(page, "images") and page.images: + images = page.images + + # Method 2: If no images found, try underlying PDF objects + if not images and hasattr(page, "objects") and "image" in page.objects: + images = page.objects.get("image", []) + + # Method 3: Try filtering all objects for image types + if not images and hasattr(page, "objects"): + all_objs = page.objects + for obj_type in all_objs.keys(): + if "image" in obj_type.lower() or "xobject" in obj_type.lower(): + potential_imgs = all_objs.get(obj_type, []) + if potential_imgs: + images = potential_imgs + break + + for i, img_dict in enumerate(images): + try: + # Try to get the actual image stream from the PDF + img_stream = None + y_pos = 0 + + # Method A: If img_dict has 'stream' key, use it directly + if "stream" in img_dict and hasattr(img_dict["stream"], "get_data"): + try: + img_bytes = img_dict["stream"].get_data() + + # Try to open as PIL Image to validate/decode + pil_img = Image.open(io.BytesIO(img_bytes)) + + # Convert to RGB if needed (handle CMYK, etc.) + if pil_img.mode not in ("RGB", "L"): + pil_img = pil_img.convert("RGB") + + # Save to stream as PNG + img_stream = io.BytesIO() + pil_img.save(img_stream, format="PNG") + img_stream.seek(0) + + y_pos = img_dict.get("top", 0) + except Exception: + pass + + # Method B: Fallback to rendering page region + if img_stream is None: + x0 = img_dict.get("x0", 0) + y0 = img_dict.get("top", 0) + x1 = img_dict.get("x1", 0) + y1 = img_dict.get("bottom", 0) + y_pos = y0 + + # Check if dimensions are valid + if x1 <= x0 or y1 <= y0: + continue + + # Use pdfplumber's within_bbox to crop, then render + # This preserves coordinate system correctly + bbox = (x0, y0, x1, y1) + cropped_page = page.within_bbox(bbox) + + # Render at 150 DPI (balance between quality and size) + page_img = cropped_page.to_image(resolution=150) + + # Save to stream + img_stream = io.BytesIO() + page_img.original.save(img_stream, format="PNG") + img_stream.seek(0) + + if img_stream: + images_info.append( + { + "stream": img_stream, + "name": f"page_{page.page_number}_img_{i}", + "y_pos": y_pos, + } + ) + + except Exception: + continue + + except Exception: + pass + + return images_info + + +class PdfConverterWithOCR(DocumentConverter): + """ + Enhanced PDF Converter with OCR support for embedded images. + Maintains document structure while extracting text from images inline. + """ + + def __init__(self, ocr_service: Optional[LLMVisionOCRService] = None): + super().__init__() + self.ocr_service = ocr_service + + def accepts( + self, + file_stream: BinaryIO, + stream_info: StreamInfo, + **kwargs: Any, + ) -> bool: + mimetype = (stream_info.mimetype or "").lower() + extension = (stream_info.extension or "").lower() + + if extension == ".pdf": + return True + + if mimetype.startswith("application/pdf") or mimetype.startswith( + "application/x-pdf" + ): + return True + + return False + + def convert( + self, + file_stream: BinaryIO, + stream_info: StreamInfo, + **kwargs: Any, + ) -> DocumentConverterResult: + if _dependency_exc_info is not None: + raise MissingDependencyException( + MISSING_DEPENDENCY_MESSAGE.format( + converter=type(self).__name__, + extension=".pdf", + feature="pdf", + ) + ) from _dependency_exc_info[1].with_traceback( + _dependency_exc_info[2] + ) # type: ignore[union-attr] + + # Get OCR service if available (from kwargs or instance) + ocr_service: LLMVisionOCRService | None = ( + kwargs.get("ocr_service") or self.ocr_service + ) + + # Read PDF into BytesIO + file_stream.seek(0) + pdf_bytes = io.BytesIO(file_stream.read()) + + markdown_content = [] + + try: + with pdfplumber.open(pdf_bytes) as pdf: + for page_num, page in enumerate(pdf.pages, 1): + markdown_content.append(f"\n## Page {page_num}\n") + + # If OCR is enabled, interleave text and images by position + if ocr_service: + images_on_page = self._extract_page_images(pdf_bytes, page_num) + + if images_on_page: + # Extract text lines with Y positions + chars = page.chars + if chars: + # Group chars into lines based on Y position + lines_with_y = [] + current_line = [] + current_y = None + + for char in sorted( + chars, key=lambda c: (c["top"], c["x0"]) + ): + y = char["top"] + if current_y is None: + current_y = y + elif abs(y - current_y) > 2: # New line threshold + if current_line: + text = "".join( + [c["text"] for c in current_line] + ) + lines_with_y.append( + {"y": current_y, "text": text.strip()} + ) + current_line = [] + current_y = y + current_line.append(char) + + # Add last line + if current_line: + text = "".join([c["text"] for c in current_line]) + lines_with_y.append( + {"y": current_y, "text": text.strip()} + ) + else: + # Fallback: use simple text extraction + text_content = page.extract_text() or "" + lines_with_y = [ + {"y": i * 10, "text": line} + for i, line in enumerate(text_content.split("\n")) + ] + + # OCR all images + image_data = [] + for img_info in images_on_page: + ocr_result = ocr_service.extract_text( + img_info["stream"] + ) + if ocr_result.text.strip(): + image_data.append( + { + "y_pos": img_info["y_pos"], + "name": img_info["name"], + "ocr_text": ocr_result.text, + "backend": ocr_result.backend_used, + "type": "image", + } + ) + + # Add text items + content_items = [ + { + "y_pos": item["y"], + "text": item["text"], + "type": "text", + } + for item in lines_with_y + if item["text"] + ] + content_items.extend(image_data) + + # Sort all items by Y position (top to bottom) + content_items.sort(key=lambda x: x["y_pos"]) + + # Build markdown by interleaving text and images + for item in content_items: + if item["type"] == "text": + markdown_content.append(item["text"]) + else: # image + ocr_text = item["ocr_text"] + img_marker = ( + f"\n\n*[Image OCR]\n{ocr_text}\n[End OCR]*\n" + ) + markdown_content.append(img_marker) + else: + # No images detected - just extract regular text + text_content = page.extract_text() or "" + if text_content.strip(): + markdown_content.append(text_content.strip()) + else: + # No OCR, just extract text + text_content = page.extract_text() or "" + if text_content.strip(): + markdown_content.append(text_content.strip()) + + # Build final markdown + markdown = "\n\n".join(markdown_content).strip() + + # Fallback to pdfminer if empty + if not markdown: + pdf_bytes.seek(0) + markdown = pdfminer.high_level.extract_text(pdf_bytes) + + except Exception: + # Fallback to pdfminer + try: + pdf_bytes.seek(0) + markdown = pdfminer.high_level.extract_text(pdf_bytes) + except Exception: + markdown = "" + + # Final fallback: If still empty/whitespace and OCR is available, + # treat as scanned PDF and OCR full pages + if ocr_service and (not markdown or not markdown.strip()): + pdf_bytes.seek(0) + markdown = self._ocr_full_pages(pdf_bytes, ocr_service) + + return DocumentConverterResult(markdown=markdown) + + def _extract_page_images(self, pdf_bytes: io.BytesIO, page_num: int) -> list[dict]: + """ + Extract images from a PDF page using pdfplumber. + + Args: + pdf_bytes: PDF file as BytesIO + page_num: Page number (1-indexed) + + Returns: + List of image info dicts with 'stream', 'bbox', 'name', 'y_pos' + """ + images = [] + + try: + pdf_bytes.seek(0) + with pdfplumber.open(pdf_bytes) as pdf: + if page_num <= len(pdf.pages): + page = pdf.pages[page_num - 1] # 0-indexed + images = _extract_images_from_page(page) + except Exception: + pass + + # Sort by vertical position (top to bottom) + images.sort(key=lambda x: x["y_pos"]) + + return images + + def _ocr_full_pages( + self, pdf_bytes: io.BytesIO, ocr_service: LLMVisionOCRService + ) -> str: + """ + Fallback for scanned PDFs: Convert entire pages to images and OCR them. + Used when text extraction returns empty/whitespace results. + + Args: + pdf_bytes: PDF file as BytesIO + ocr_service: OCR service to use + + Returns: + Markdown text extracted from OCR of full pages + """ + markdown_parts = [] + + try: + pdf_bytes.seek(0) + with pdfplumber.open(pdf_bytes) as pdf: + for page_num, page in enumerate(pdf.pages, 1): + try: + markdown_parts.append(f"\n## Page {page_num}\n") + + # Render page to image + page_img = page.to_image(resolution=300) + img_stream = io.BytesIO() + page_img.original.save(img_stream, format="PNG") + img_stream.seek(0) + + # Run OCR + ocr_result = ocr_service.extract_text(img_stream) + + if ocr_result.text.strip(): + text = ocr_result.text.strip() + markdown_parts.append(f"*[Image OCR]\n{text}\n[End OCR]*") + else: + markdown_parts.append( + "*[No text could be extracted from this page]*" + ) + + except Exception as e: + markdown_parts.append( + f"*[Error processing page {page_num}: {str(e)}]*" + ) + continue + + except Exception: + # pdfplumber failed (e.g. malformed EOF) — try PyMuPDF for rendering + markdown_parts = [] + try: + import fitz # PyMuPDF + + pdf_bytes.seek(0) + doc = fitz.open(stream=pdf_bytes.read(), filetype="pdf") + for page_num in range(1, doc.page_count + 1): + try: + markdown_parts.append(f"\n## Page {page_num}\n") + page = doc[page_num - 1] + mat = fitz.Matrix(300 / 72, 300 / 72) # 300 DPI + pix = page.get_pixmap(matrix=mat) + img_stream = io.BytesIO(pix.tobytes("png")) + img_stream.seek(0) + + ocr_result = ocr_service.extract_text(img_stream) + + if ocr_result.text.strip(): + text = ocr_result.text.strip() + markdown_parts.append(f"*[Image OCR]\n{text}\n[End OCR]*") + else: + markdown_parts.append( + "*[No text could be extracted from this page]*" + ) + + except Exception as e: + markdown_parts.append( + f"*[Error processing page {page_num}: {str(e)}]*" + ) + continue + doc.close() + except Exception: + return "*[Error: Could not process scanned PDF]*" + + return "\n\n".join(markdown_parts).strip() diff --git a/packages/markitdown-ocr/src/markitdown_ocr/_plugin.py b/packages/markitdown-ocr/src/markitdown_ocr/_plugin.py new file mode 100644 index 000000000..f4d7bcf5a --- /dev/null +++ b/packages/markitdown-ocr/src/markitdown_ocr/_plugin.py @@ -0,0 +1,68 @@ +""" +Plugin registration for markitdown-ocr. +Registers OCR-enhanced converters with priority-based replacement strategy. +""" + +from typing import Any +from markitdown import MarkItDown + +from ._ocr_service import LLMVisionOCRService +from ._pdf_converter_with_ocr import PdfConverterWithOCR +from ._docx_converter_with_ocr import DocxConverterWithOCR +from ._pptx_converter_with_ocr import PptxConverterWithOCR +from ._xlsx_converter_with_ocr import XlsxConverterWithOCR + + +__plugin_interface_version__ = 1 + + +def register_converters(markitdown: MarkItDown, **kwargs: Any) -> None: + """ + Register OCR-enhanced converters with MarkItDown. + + This plugin provides OCR support for PDF, DOCX, PPTX, and XLSX files. + The converters are registered with priority -1.0 to run BEFORE built-in + converters (which have priority 0.0), effectively replacing them when + the plugin is enabled. + + Args: + markitdown: MarkItDown instance to register converters with + **kwargs: Additional keyword arguments that may include: + - llm_client: OpenAI-compatible client for LLM-based OCR (required for OCR to work) + - llm_model: Model name (e.g., 'gpt-4o') + - llm_prompt: Custom prompt for text extraction + """ + # Create OCR service — reads the same llm_client/llm_model kwargs + # that MarkItDown itself already accepts for image descriptions + llm_client = kwargs.get("llm_client") + llm_model = kwargs.get("llm_model") + llm_prompt = kwargs.get("llm_prompt") + + ocr_service: LLMVisionOCRService | None = None + if llm_client and llm_model: + ocr_service = LLMVisionOCRService( + client=llm_client, + model=llm_model, + default_prompt=llm_prompt, + ) + + # Register converters with priority -1.0 (before built-ins at 0.0) + # This effectively "replaces" the built-in converters when plugin is installed + # Pass the OCR service to each converter's constructor + PRIORITY_OCR_ENHANCED = -1.0 + + markitdown.register_converter( + PdfConverterWithOCR(ocr_service=ocr_service), priority=PRIORITY_OCR_ENHANCED + ) + + markitdown.register_converter( + DocxConverterWithOCR(ocr_service=ocr_service), priority=PRIORITY_OCR_ENHANCED + ) + + markitdown.register_converter( + PptxConverterWithOCR(ocr_service=ocr_service), priority=PRIORITY_OCR_ENHANCED + ) + + markitdown.register_converter( + XlsxConverterWithOCR(ocr_service=ocr_service), priority=PRIORITY_OCR_ENHANCED + ) diff --git a/packages/markitdown-ocr/src/markitdown_ocr/_pptx_converter_with_ocr.py b/packages/markitdown-ocr/src/markitdown_ocr/_pptx_converter_with_ocr.py new file mode 100644 index 000000000..7e91ed6b4 --- /dev/null +++ b/packages/markitdown-ocr/src/markitdown_ocr/_pptx_converter_with_ocr.py @@ -0,0 +1,249 @@ +""" +Enhanced PPTX Converter with improved OCR support. +Already has LLM-based image description, this enhances it with traditional OCR fallback. +""" + +import io +import sys +from typing import Any, BinaryIO, Optional + +from typing import BinaryIO, Any, Optional + +from markitdown.converters import HtmlConverter +from markitdown import DocumentConverter, DocumentConverterResult, StreamInfo +from markitdown._exceptions import ( + MissingDependencyException, + MISSING_DEPENDENCY_MESSAGE, +) +from ._ocr_service import LLMVisionOCRService + +_dependency_exc_info = None +try: + import pptx +except ImportError: + _dependency_exc_info = sys.exc_info() + + +class PptxConverterWithOCR(DocumentConverter): + """Enhanced PPTX Converter with OCR fallback.""" + + def __init__(self, ocr_service: Optional[LLMVisionOCRService] = None): + super().__init__() + self._html_converter = HtmlConverter() + self.ocr_service = ocr_service + + def accepts( + self, + file_stream: BinaryIO, + stream_info: StreamInfo, + **kwargs: Any, + ) -> bool: + mimetype = (stream_info.mimetype or "").lower() + extension = (stream_info.extension or "").lower() + + if extension == ".pptx": + return True + + if mimetype.startswith( + "application/vnd.openxmlformats-officedocument.presentationml" + ): + return True + + return False + + def convert( + self, + file_stream: BinaryIO, + stream_info: StreamInfo, + **kwargs: Any, + ) -> DocumentConverterResult: + if _dependency_exc_info is not None: + raise MissingDependencyException( + MISSING_DEPENDENCY_MESSAGE.format( + converter=type(self).__name__, + extension=".pptx", + feature="pptx", + ) + ) from _dependency_exc_info[1].with_traceback( + _dependency_exc_info[2] + ) # type: ignore[union-attr] + + # Get OCR service (from kwargs or instance) + ocr_service: Optional[LLMVisionOCRService] = ( + kwargs.get("ocr_service") or self.ocr_service + ) + llm_client = kwargs.get("llm_client") + + presentation = pptx.Presentation(file_stream) + md_content = "" + slide_num = 0 + + for slide in presentation.slides: + slide_num += 1 + md_content += f"\\n\\n\\n" + + title = slide.shapes.title + + def get_shape_content(shape, **kwargs): + nonlocal md_content + + # Pictures + if self._is_picture(shape): + # Get image data + image_stream = io.BytesIO(shape.image.blob) + + # Try LLM description first if available + llm_description = "" + if llm_client and kwargs.get("llm_model"): + try: + from ._llm_caption import llm_caption + + image_filename = shape.image.filename + image_extension = None + if image_filename: + import os + + image_extension = os.path.splitext(image_filename)[1] + + image_stream_info = StreamInfo( + mimetype=shape.image.content_type, + extension=image_extension, + filename=image_filename, + ) + + llm_description = llm_caption( + image_stream, + image_stream_info, + client=llm_client, + model=kwargs.get("llm_model"), + prompt=kwargs.get("llm_prompt"), + ) + except Exception: + pass + + # Try OCR if LLM failed or not available + ocr_text = "" + if not llm_description and ocr_service: + try: + image_stream.seek(0) + ocr_result = ocr_service.extract_text(image_stream) + if ocr_result.text.strip(): + ocr_text = ocr_result.text.strip() + except Exception: + pass + + # Format extracted content using unified OCR block format + content = (llm_description or ocr_text or "").strip() + if content: + md_content += f"\n*[Image OCR]\n{content}\n[End OCR]*\n" + + # Tables + if self._is_table(shape): + md_content += self._convert_table_to_markdown(shape.table, **kwargs) + + # Charts + if shape.has_chart: + md_content += self._convert_chart_to_markdown(shape.chart) + + # Text areas + elif shape.has_text_frame: + if shape == title: + md_content += "# " + shape.text.lstrip() + "\\n" + else: + md_content += shape.text + "\\n" + + # Group Shapes + if shape.shape_type == pptx.enum.shapes.MSO_SHAPE_TYPE.GROUP: + sorted_shapes = sorted( + shape.shapes, + key=lambda x: ( + float("-inf") if not x.top else x.top, + float("-inf") if not x.left else x.left, + ), + ) + for subshape in sorted_shapes: + get_shape_content(subshape, **kwargs) + + sorted_shapes = sorted( + slide.shapes, + key=lambda x: ( + float("-inf") if not x.top else x.top, + float("-inf") if not x.left else x.left, + ), + ) + for shape in sorted_shapes: + get_shape_content(shape, **kwargs) + + md_content = md_content.strip() + + if slide.has_notes_slide: + md_content += "\\n\\n### Notes:\\n" + notes_frame = slide.notes_slide.notes_text_frame + if notes_frame is not None: + md_content += notes_frame.text + md_content = md_content.strip() + + return DocumentConverterResult(markdown=md_content.strip()) + + def _is_picture(self, shape): + if shape.shape_type == pptx.enum.shapes.MSO_SHAPE_TYPE.PICTURE: + return True + if shape.shape_type == pptx.enum.shapes.MSO_SHAPE_TYPE.PLACEHOLDER: + if hasattr(shape, "image"): + return True + return False + + def _is_table(self, shape): + if shape.shape_type == pptx.enum.shapes.MSO_SHAPE_TYPE.TABLE: + return True + return False + + def _convert_table_to_markdown(self, table, **kwargs): + import html + + html_table = "" + first_row = True + for row in table.rows: + html_table += "" + for cell in row.cells: + if first_row: + html_table += "" + else: + html_table += "" + html_table += "" + first_row = False + html_table += "
" + html.escape(cell.text) + "" + html.escape(cell.text) + "
" + + return ( + self._html_converter.convert_string(html_table, **kwargs).markdown.strip() + + "\\n" + ) + + def _convert_chart_to_markdown(self, chart): + try: + md = "\\n\\n### Chart" + if chart.has_title: + md += f": {chart.chart_title.text_frame.text}" + md += "\\n\\n" + data = [] + category_names = [c.label for c in chart.plots[0].categories] + series_names = [s.name for s in chart.series] + data.append(["Category"] + series_names) + + for idx, category in enumerate(category_names): + row = [category] + for series in chart.series: + row.append(series.values[idx]) + data.append(row) + + markdown_table = [] + for row in data: + markdown_table.append("| " + " | ".join(map(str, row)) + " |") + header = markdown_table[0] + separator = "|" + "|".join(["---"] * len(data[0])) + "|" + return md + "\\n".join([header, separator] + markdown_table[1:]) + except ValueError as e: + if "unsupported plot type" in str(e): + return "\\n\\n[unsupported chart]\\n\\n" + except Exception: + return "\\n\\n[unsupported chart]\\n\\n" diff --git a/packages/markitdown-ocr/src/markitdown_ocr/_xlsx_converter_with_ocr.py b/packages/markitdown-ocr/src/markitdown_ocr/_xlsx_converter_with_ocr.py new file mode 100644 index 000000000..481e07195 --- /dev/null +++ b/packages/markitdown-ocr/src/markitdown_ocr/_xlsx_converter_with_ocr.py @@ -0,0 +1,225 @@ +""" +Enhanced XLSX Converter with OCR support for embedded images. +Extracts images from Excel spreadsheets and performs OCR while maintaining cell context. +""" + +import io +import sys +from typing import Any, BinaryIO, Optional + +from markitdown.converters import HtmlConverter +from markitdown import DocumentConverter, DocumentConverterResult, StreamInfo +from markitdown._exceptions import ( + MissingDependencyException, + MISSING_DEPENDENCY_MESSAGE, +) +from ._ocr_service import LLMVisionOCRService + +# Try loading dependencies +_xlsx_dependency_exc_info = None +try: + import pandas as pd + from openpyxl import load_workbook +except ImportError: + _xlsx_dependency_exc_info = sys.exc_info() + + +class XlsxConverterWithOCR(DocumentConverter): + """ + Enhanced XLSX Converter with OCR support for embedded images. + Extracts images with their cell positions and performs OCR. + """ + + def __init__(self, ocr_service: Optional[LLMVisionOCRService] = None): + super().__init__() + self._html_converter = HtmlConverter() + self.ocr_service = ocr_service + + def accepts( + self, + file_stream: BinaryIO, + stream_info: StreamInfo, + **kwargs: Any, + ) -> bool: + mimetype = (stream_info.mimetype or "").lower() + extension = (stream_info.extension or "").lower() + + if extension == ".xlsx": + return True + + if mimetype.startswith( + "application/vnd.openxmlformats-officedocument.spreadsheetml" + ): + return True + + return False + + def convert( + self, + file_stream: BinaryIO, + stream_info: StreamInfo, + **kwargs: Any, + ) -> DocumentConverterResult: + if _xlsx_dependency_exc_info is not None: + raise MissingDependencyException( + MISSING_DEPENDENCY_MESSAGE.format( + converter=type(self).__name__, + extension=".xlsx", + feature="xlsx", + ) + ) from _xlsx_dependency_exc_info[1].with_traceback( + _xlsx_dependency_exc_info[2] + ) # type: ignore[union-attr] + + # Get OCR service if available (from kwargs or instance) + ocr_service: Optional[LLMVisionOCRService] = ( + kwargs.get("ocr_service") or self.ocr_service + ) + + if ocr_service: + # Remove ocr_service from kwargs to avoid duplicate argument error + kwargs_without_ocr = {k: v for k, v in kwargs.items() if k != "ocr_service"} + return self._convert_with_ocr( + file_stream, ocr_service, **kwargs_without_ocr + ) + else: + return self._convert_standard(file_stream, **kwargs) + + def _convert_standard( + self, file_stream: BinaryIO, **kwargs: Any + ) -> DocumentConverterResult: + """Standard conversion without OCR.""" + file_stream.seek(0) + sheets = pd.read_excel(file_stream, sheet_name=None, engine="openpyxl") + md_content = "" + + for sheet_name in sheets: + md_content += f"## {sheet_name}\n" + html_content = sheets[sheet_name].to_html(index=False) + md_content += ( + self._html_converter.convert_string( + html_content, **kwargs + ).markdown.strip() + + "\n\n" + ) + + return DocumentConverterResult(markdown=md_content.strip()) + + def _convert_with_ocr( + self, file_stream: BinaryIO, ocr_service: LLMVisionOCRService, **kwargs: Any + ) -> DocumentConverterResult: + """Convert XLSX with image OCR.""" + file_stream.seek(0) + wb = load_workbook(file_stream) + + md_content = "" + + for sheet_name in wb.sheetnames: + sheet = wb[sheet_name] + md_content += f"## {sheet_name}\n\n" + + # Convert sheet data to markdown table + file_stream.seek(0) + try: + df = pd.read_excel( + file_stream, sheet_name=sheet_name, engine="openpyxl" + ) + html_content = df.to_html(index=False) + md_content += ( + self._html_converter.convert_string( + html_content, **kwargs + ).markdown.strip() + + "\n\n" + ) + except Exception: + # If pandas fails, just skip the table + pass + + # Extract and OCR images in this sheet + images_with_ocr = self._extract_and_ocr_sheet_images(sheet, ocr_service) + + if images_with_ocr: + md_content += "### Images in this sheet:\n\n" + for img_info in images_with_ocr: + ocr_text = img_info["ocr_text"] + md_content += f"*[Image OCR]\n{ocr_text}\n[End OCR]*\n\n" + + return DocumentConverterResult(markdown=md_content.strip()) + + def _extract_and_ocr_sheet_images( + self, sheet: Any, ocr_service: LLMVisionOCRService + ) -> list[dict]: + """ + Extract and OCR images from an Excel sheet. + + Args: + sheet: openpyxl worksheet + ocr_service: OCR service + + Returns: + List of dicts with 'cell_ref' and 'ocr_text' + """ + results = [] + + try: + # Check if sheet has images + if hasattr(sheet, "_images"): + for img in sheet._images: + try: + # Get image data + if hasattr(img, "_data"): + image_data = img._data() + elif hasattr(img, "image"): + # Some versions store it differently + image_data = img.image + else: + continue + + # Create image stream + image_stream = io.BytesIO(image_data) + + # Get cell reference + cell_ref = "unknown" + if hasattr(img, "anchor"): + anchor = img.anchor + if hasattr(anchor, "_from"): + from_cell = anchor._from + if hasattr(from_cell, "col") and hasattr( + from_cell, "row" + ): + # Convert column number to letter + col_letter = self._column_number_to_letter( + from_cell.col + ) + cell_ref = f"{col_letter}{from_cell.row + 1}" + + # Perform OCR + ocr_result = ocr_service.extract_text(image_stream) + + if ocr_result.text.strip(): + results.append( + { + "cell_ref": cell_ref, + "ocr_text": ocr_result.text.strip(), + "backend": ocr_result.backend_used, + } + ) + + except Exception: + continue + + except Exception: + pass + + return results + + @staticmethod + def _column_number_to_letter(n: int) -> str: + """Convert column number to Excel column letter (0-indexed).""" + result = "" + n = n + 1 # Make 1-indexed + while n > 0: + n -= 1 + result = chr(65 + (n % 26)) + result + n //= 26 + return result diff --git a/packages/markitdown-ocr/tests/__init__.py b/packages/markitdown-ocr/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/packages/markitdown-ocr/tests/ocr_test_data/docx_complex_layout.docx b/packages/markitdown-ocr/tests/ocr_test_data/docx_complex_layout.docx new file mode 100644 index 000000000..4ddd69746 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/docx_complex_layout.docx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/docx_image_end.docx b/packages/markitdown-ocr/tests/ocr_test_data/docx_image_end.docx new file mode 100644 index 000000000..f2a9a8694 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/docx_image_end.docx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/docx_image_middle.docx b/packages/markitdown-ocr/tests/ocr_test_data/docx_image_middle.docx new file mode 100644 index 000000000..200f3c6c7 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/docx_image_middle.docx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/docx_image_start.docx b/packages/markitdown-ocr/tests/ocr_test_data/docx_image_start.docx new file mode 100644 index 000000000..7855bd166 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/docx_image_start.docx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/docx_multipage.docx b/packages/markitdown-ocr/tests/ocr_test_data/docx_multipage.docx new file mode 100644 index 000000000..c698b0fa2 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/docx_multipage.docx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/docx_multiple_images.docx b/packages/markitdown-ocr/tests/ocr_test_data/docx_multiple_images.docx new file mode 100644 index 000000000..790ce0bcb Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/docx_multiple_images.docx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pdf_complex_layout.pdf b/packages/markitdown-ocr/tests/ocr_test_data/pdf_complex_layout.pdf new file mode 100644 index 000000000..f843ab891 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pdf_complex_layout.pdf differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pdf_image_end.pdf b/packages/markitdown-ocr/tests/ocr_test_data/pdf_image_end.pdf new file mode 100644 index 000000000..8b020edf6 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pdf_image_end.pdf differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pdf_image_middle.pdf b/packages/markitdown-ocr/tests/ocr_test_data/pdf_image_middle.pdf new file mode 100644 index 000000000..d90bc9d3e Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pdf_image_middle.pdf differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pdf_image_start.pdf b/packages/markitdown-ocr/tests/ocr_test_data/pdf_image_start.pdf new file mode 100644 index 000000000..0b57b7f96 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pdf_image_start.pdf differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pdf_multipage.pdf b/packages/markitdown-ocr/tests/ocr_test_data/pdf_multipage.pdf new file mode 100644 index 000000000..71ffe8d83 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pdf_multipage.pdf differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pdf_multiple_images.pdf b/packages/markitdown-ocr/tests/ocr_test_data/pdf_multiple_images.pdf new file mode 100644 index 000000000..8a5e47416 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pdf_multiple_images.pdf differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_invoice.pdf b/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_invoice.pdf new file mode 100644 index 000000000..5e1caacc5 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_invoice.pdf differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_meeting_minutes.pdf b/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_meeting_minutes.pdf new file mode 100644 index 000000000..33c717bed Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_meeting_minutes.pdf differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_minimal.pdf b/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_minimal.pdf new file mode 100644 index 000000000..9410339e3 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_minimal.pdf differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_report.pdf b/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_report.pdf new file mode 100644 index 000000000..4c2112ff7 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_report.pdf differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_sales_report.pdf b/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_sales_report.pdf new file mode 100644 index 000000000..178c63826 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pdf_scanned_sales_report.pdf differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pptx_complex_layout.pptx b/packages/markitdown-ocr/tests/ocr_test_data/pptx_complex_layout.pptx new file mode 100644 index 000000000..10467ea0e Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pptx_complex_layout.pptx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pptx_image_end.pptx b/packages/markitdown-ocr/tests/ocr_test_data/pptx_image_end.pptx new file mode 100644 index 000000000..1ed9804cd Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pptx_image_end.pptx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pptx_image_middle.pptx b/packages/markitdown-ocr/tests/ocr_test_data/pptx_image_middle.pptx new file mode 100644 index 000000000..315586a23 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pptx_image_middle.pptx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pptx_image_start.pptx b/packages/markitdown-ocr/tests/ocr_test_data/pptx_image_start.pptx new file mode 100644 index 000000000..32a50aa8c Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pptx_image_start.pptx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/pptx_multiple_images.pptx b/packages/markitdown-ocr/tests/ocr_test_data/pptx_multiple_images.pptx new file mode 100644 index 000000000..a8eaa4dee Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/pptx_multiple_images.pptx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/xlsx_complex_layout.xlsx b/packages/markitdown-ocr/tests/ocr_test_data/xlsx_complex_layout.xlsx new file mode 100644 index 000000000..6052c1e30 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/xlsx_complex_layout.xlsx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/xlsx_image_end.xlsx b/packages/markitdown-ocr/tests/ocr_test_data/xlsx_image_end.xlsx new file mode 100644 index 000000000..3e26b33fd Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/xlsx_image_end.xlsx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/xlsx_image_middle.xlsx b/packages/markitdown-ocr/tests/ocr_test_data/xlsx_image_middle.xlsx new file mode 100644 index 000000000..2a6c91b77 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/xlsx_image_middle.xlsx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/xlsx_image_start.xlsx b/packages/markitdown-ocr/tests/ocr_test_data/xlsx_image_start.xlsx new file mode 100644 index 000000000..9e461821a Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/xlsx_image_start.xlsx differ diff --git a/packages/markitdown-ocr/tests/ocr_test_data/xlsx_multiple_images.xlsx b/packages/markitdown-ocr/tests/ocr_test_data/xlsx_multiple_images.xlsx new file mode 100644 index 000000000..eb8d0cfe6 Binary files /dev/null and b/packages/markitdown-ocr/tests/ocr_test_data/xlsx_multiple_images.xlsx differ diff --git a/packages/markitdown-ocr/tests/test_docx_converter.py b/packages/markitdown-ocr/tests/test_docx_converter.py new file mode 100644 index 000000000..0fb666504 --- /dev/null +++ b/packages/markitdown-ocr/tests/test_docx_converter.py @@ -0,0 +1,223 @@ +""" +Unit tests for DocxConverterWithOCR. + +For each DOCX test file: convert with a mock OCR service then compare the +full output string against the expected snapshot. + +OCR block format used by the converter: + *[Image OCR] + MOCK_OCR_TEXT_12345 + [End OCR]* +""" + +import sys +from pathlib import Path +from typing import Any + +import pytest + +sys.path.insert(0, str(Path(__file__).parent.parent / "src")) + +from markitdown_ocr._ocr_service import OCRResult # noqa: E402 +from markitdown_ocr._docx_converter_with_ocr import ( # noqa: E402 + DocxConverterWithOCR, +) +from markitdown import StreamInfo # noqa: E402 + +TEST_DATA_DIR = Path(__file__).parent / "ocr_test_data" + +_MOCK_TEXT = "MOCK_OCR_TEXT_12345" + + +class MockOCRService: + def extract_text( # noqa: ANN101 + self, image_stream: Any, **kwargs: Any + ) -> OCRResult: + return OCRResult(text=_MOCK_TEXT, backend_used="mock") + + +@pytest.fixture(scope="module") +def svc() -> MockOCRService: + return MockOCRService() + + +def _convert(filename: str, ocr_service: MockOCRService) -> str: + path = TEST_DATA_DIR / filename + if not path.exists(): + pytest.skip(f"Test file not found: {path}") + converter = DocxConverterWithOCR() + with open(path, "rb") as f: + return converter.convert( + f, StreamInfo(extension=".docx"), ocr_service=ocr_service + ).text_content + + +# --------------------------------------------------------------------------- +# docx_image_start.docx +# --------------------------------------------------------------------------- + + +def test_docx_image_start(svc: MockOCRService) -> None: + expected = ( + "Document with Image at Start\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "This is the main content after the header image.\n\n" + "More text content here." + ) + assert _convert("docx_image_start.docx", svc) == expected + + +# --------------------------------------------------------------------------- +# docx_image_middle.docx +# --------------------------------------------------------------------------- + + +def test_docx_image_middle(svc: MockOCRService) -> None: + expected = ( + "# Introduction\n\n" + "This is the introduction section.\n\n" + "We will see an image below.\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "# Analysis\n\n" + "This section comes after the image." + ) + assert _convert("docx_image_middle.docx", svc) == expected + + +# --------------------------------------------------------------------------- +# docx_image_end.docx +# --------------------------------------------------------------------------- + + +def test_docx_image_end(svc: MockOCRService) -> None: + expected = ( + "Report\n\n" + "Main findings of the report.\n\n" + "Details and analysis.\n\n" + "Recommendations.\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + ) + assert _convert("docx_image_end.docx", svc) == expected + + +# --------------------------------------------------------------------------- +# docx_multiple_images.docx +# --------------------------------------------------------------------------- + + +def test_docx_multiple_images(svc: MockOCRService) -> None: + expected = ( + "Multi-Image Document\n\n" + "First section\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "Second section with another image\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "Conclusion" + ) + assert _convert("docx_multiple_images.docx", svc) == expected + + +# --------------------------------------------------------------------------- +# docx_multipage.docx +# --------------------------------------------------------------------------- + + +def test_docx_multipage(svc: MockOCRService) -> None: + expected = ( + "# Page 1 - Mixed Content\n\n" + "This is the first paragraph on page 1.\n\n" + "BEFORE IMAGE: Important content appears here.\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "AFTER IMAGE: This content follows the image.\n\n" + "More text on page 1.\n\n" + "# Page 2 - Image at End\n\n" + "Content on page 2.\n\n" + "Multiple paragraphs of text.\n\n" + "Building up to the image...\n\n" + "Final paragraph before image.\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "# Page 3 - Image at Start\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "Content that follows the header image.\n\n" + "AFTER IMAGE: This text is after the image." + ) + assert _convert("docx_multipage.docx", svc) == expected + + +# --------------------------------------------------------------------------- +# docx_complex_layout.docx +# --------------------------------------------------------------------------- + + +def test_docx_complex_layout(svc: MockOCRService) -> None: + expected = ( + "Complex Document\n\n" + "| | |\n" + "| --- | --- |\n" + "| Feature | Status |\n" + "| Authentication | Active |\n" + "| Encryption | Enabled |\n\n" + "Security notice:\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + ) + assert _convert("docx_complex_layout.docx", svc) == expected + + +# --------------------------------------------------------------------------- +# _inject_placeholders — internal unit tests (no file I/O) +# --------------------------------------------------------------------------- + + +def test_inject_placeholders_single_image() -> None: + converter = DocxConverterWithOCR() + html = "

Before

After

" + result_html, texts = converter._inject_placeholders(html, {"rId1": "TEXT"}) + assert " None: + converter = DocxConverterWithOCR() + html = "

Mid

" + result_html, texts = converter._inject_placeholders( + html, {"rId1": "FIRST", "rId2": "SECOND"} + ) + assert "MARKITDOWNOCRBLOCK0" in result_html + assert "MARKITDOWNOCRBLOCK1" in result_html + assert result_html.index("MARKITDOWNOCRBLOCK0") < result_html.index( + "MARKITDOWNOCRBLOCK1" + ) + assert len(texts) == 2 + + +def test_inject_placeholders_no_img_tag_appends_at_end() -> None: + converter = DocxConverterWithOCR() + html = "

No images

" + result_html, texts = converter._inject_placeholders(html, {"rId1": "ORPHAN"}) + assert "MARKITDOWNOCRBLOCK0" in result_html + assert texts == ["ORPHAN"] + + +def test_inject_placeholders_empty_map_leaves_html_unchanged() -> None: + converter = DocxConverterWithOCR() + html = "

Content

" + result_html, texts = converter._inject_placeholders(html, {}) + assert result_html == html + assert texts == [] + + +# --------------------------------------------------------------------------- +# No OCR service — no OCR tags emitted +# --------------------------------------------------------------------------- + + +def test_docx_no_ocr_service_no_tags() -> None: + path = TEST_DATA_DIR / "docx_image_middle.docx" + if not path.exists(): + pytest.skip(f"Test file not found: {path}") + converter = DocxConverterWithOCR() + with open(path, "rb") as f: + md = converter.convert(f, StreamInfo(extension=".docx")).text_content + assert "*[Image OCR]" not in md + assert "[End OCR]*" not in md diff --git a/packages/markitdown-ocr/tests/test_pdf_converter.py b/packages/markitdown-ocr/tests/test_pdf_converter.py new file mode 100644 index 000000000..5d4adcc5e --- /dev/null +++ b/packages/markitdown-ocr/tests/test_pdf_converter.py @@ -0,0 +1,234 @@ +""" +Unit tests for PdfConverterWithOCR. + +For each PDF test file: convert with a mock OCR service then compare the +full output string against the expected snapshot. + +OCR block format used by the converter: + *[Image OCR] + MOCK_OCR_TEXT_12345 + [End OCR]* +""" + +import io +import sys +from pathlib import Path +from typing import Any +from unittest.mock import MagicMock, patch + +import pytest + +sys.path.insert(0, str(Path(__file__).parent.parent / "src")) + +from markitdown_ocr._ocr_service import OCRResult # noqa: E402 +from markitdown_ocr._pdf_converter_with_ocr import ( # noqa: E402 + PdfConverterWithOCR, +) +from markitdown import StreamInfo # noqa: E402 + +TEST_DATA_DIR = Path(__file__).parent / "ocr_test_data" + +_MOCK_TEXT = "MOCK_OCR_TEXT_12345" +_OCR_BLOCK = f"*[Image OCR]\n{_MOCK_TEXT}\n[End OCR]*" +_PAGE_1_SCANNED = f"## Page 1\n\n\n\n\n{_OCR_BLOCK}" + + +class MockOCRService: + def extract_text( + self, # noqa: ANN101 + image_stream: Any, + **kwargs: Any, + ) -> OCRResult: + return OCRResult(text=_MOCK_TEXT, backend_used="mock") + + +@pytest.fixture(scope="module") +def svc() -> MockOCRService: + return MockOCRService() + + +def _convert(filename: str, ocr_service: MockOCRService) -> str: + path = TEST_DATA_DIR / filename + if not path.exists(): + pytest.skip(f"Test file not found: {path}") + converter = PdfConverterWithOCR() + with open(path, "rb") as f: + return converter.convert( + f, StreamInfo(extension=".pdf"), ocr_service=ocr_service + ).text_content + + +# --------------------------------------------------------------------------- +# pdf_image_start.pdf +# --------------------------------------------------------------------------- + + +def test_pdf_image_start(svc: MockOCRService) -> None: + expected = ( + "## Page 1\n\n\n\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n\n" + "This is text BEFORE the image.\n\n" + "The image should appear above this text.\n\n" + "This is more content after the image." + ) + assert _convert("pdf_image_start.pdf", svc) == expected + + +# --------------------------------------------------------------------------- +# pdf_image_middle.pdf +# --------------------------------------------------------------------------- + + +def test_pdf_image_middle(svc: MockOCRService) -> None: + expected = ( + "## Page 1\n\n\n" + "Section 1: Introduction\n\n" + "This document contains an image in the middle.\n\n" + "Here is some introductory text.\n\n\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n\n" + "Section 2: Details\n\n" + "This text appears AFTER the image." + ) + assert _convert("pdf_image_middle.pdf", svc) == expected + + +# --------------------------------------------------------------------------- +# pdf_image_end.pdf +# --------------------------------------------------------------------------- + + +def test_pdf_image_end(svc: MockOCRService) -> None: + expected = ( + "## Page 1\n\n\n" + "Main Content\n\n" + "This is the main text content.\n\n" + "The image will appear at the end.\n\n" + "Keep reading...\n\n\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + ) + assert _convert("pdf_image_end.pdf", svc) == expected + + +# --------------------------------------------------------------------------- +# pdf_multiple_images.pdf +# --------------------------------------------------------------------------- + + +def test_pdf_multiple_images(svc: MockOCRService) -> None: + expected = ( + "## Page 1\n\n\n" + "Document with Multiple Images\n\n\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n\n" + "Text between first and second image.\n\n\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n\n" + "Final text after all images." + ) + assert _convert("pdf_multiple_images.pdf", svc) == expected + + +# --------------------------------------------------------------------------- +# pdf_complex_layout.pdf +# --------------------------------------------------------------------------- + + +def test_pdf_complex_layout(svc: MockOCRService) -> None: + expected = ( + "## Page 1\n\n\n" + "Complex Layout Document\n\n" + "Table:\n\n" + "ItemQuantity\n\n\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n\n" + "Widget A5" + ) + assert _convert("pdf_complex_layout.pdf", svc) == expected + + +# --------------------------------------------------------------------------- +# pdf_multipage.pdf — pdfplumber/pdfminer fail (EOF); PyMuPDF fallback used +# --------------------------------------------------------------------------- + + +def test_pdf_multipage(svc: MockOCRService) -> None: + # pdfplumber cannot open this file (Unexpected EOF), so _ocr_full_pages + # falls back to PyMuPDF for page rendering. Each page becomes one OCR block. + expected = ( + f"## Page 1\n\n\n{_OCR_BLOCK}\n\n\n" + f"## Page 2\n\n\n{_OCR_BLOCK}\n\n\n" + f"## Page 3\n\n\n{_OCR_BLOCK}" + ) + assert _convert("pdf_multipage.pdf", svc) == expected + + +# --------------------------------------------------------------------------- +# pdf_scanned_*.pdf — raster-only pages → full-page OCR +# --------------------------------------------------------------------------- + + +def test_pdf_scanned_invoice(svc: MockOCRService) -> None: + assert _convert("pdf_scanned_invoice.pdf", svc) == _PAGE_1_SCANNED + + +def test_pdf_scanned_meeting_minutes(svc: MockOCRService) -> None: + assert _convert("pdf_scanned_meeting_minutes.pdf", svc) == _PAGE_1_SCANNED + + +def test_pdf_scanned_minimal(svc: MockOCRService) -> None: + assert _convert("pdf_scanned_minimal.pdf", svc) == _PAGE_1_SCANNED + + +def test_pdf_scanned_sales_report(svc: MockOCRService) -> None: + assert _convert("pdf_scanned_sales_report.pdf", svc) == _PAGE_1_SCANNED + + +def test_pdf_scanned_report(svc: MockOCRService) -> None: + expected = ( + f"{_PAGE_1_SCANNED}\n\n\n\n" + f"## Page 2\n\n\n\n\n{_OCR_BLOCK}\n\n\n\n" + f"## Page 3\n\n\n\n\n{_OCR_BLOCK}" + ) + assert _convert("pdf_scanned_report.pdf", svc) == expected + + +# --------------------------------------------------------------------------- +# Scanned PDF fallback path (pdfplumber finds no text → full-page OCR) +# --------------------------------------------------------------------------- + + +def test_pdf_scanned_fallback_format(svc: MockOCRService) -> None: + """_ocr_full_pages emits *[Image OCR]...[End OCR]* for each page.""" + path = TEST_DATA_DIR / "pdf_image_start.pdf" + if not path.exists(): + pytest.skip(f"Test file not found: {path}") + + converter = PdfConverterWithOCR() + with patch("pdfplumber.open") as mock_plumber: + mock_pdf = MagicMock() + mock_page = MagicMock() + mock_page.page_number = 1 + mock_pdf.pages = [mock_page] + mock_pdf.__enter__.return_value = mock_pdf + mock_plumber.return_value = mock_pdf + + with open(path, "rb") as f: + md = converter._ocr_full_pages(io.BytesIO(f.read()), svc) + + expected = "## Page 1\n\n\n" "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + assert ( + md == expected + ), f"_ocr_full_pages must produce:\n{expected!r}\nActual:\n{md!r}" + + +# --------------------------------------------------------------------------- +# No OCR service — no OCR tags emitted +# --------------------------------------------------------------------------- + + +def test_pdf_no_ocr_service_no_tags() -> None: + path = TEST_DATA_DIR / "pdf_image_middle.pdf" + if not path.exists(): + pytest.skip(f"Test file not found: {path}") + converter = PdfConverterWithOCR() + with open(path, "rb") as f: + md = converter.convert(f, StreamInfo(extension=".pdf")).text_content + assert "*[Image OCR]" not in md + assert "[End OCR]*" not in md diff --git a/packages/markitdown-ocr/tests/test_pptx_converter.py b/packages/markitdown-ocr/tests/test_pptx_converter.py new file mode 100644 index 000000000..724f1039c --- /dev/null +++ b/packages/markitdown-ocr/tests/test_pptx_converter.py @@ -0,0 +1,148 @@ +""" +Unit tests for PptxConverterWithOCR. + +For each PPTX test file: convert with a mock OCR service then compare the +full output string against the expected snapshot. + +OCR block format used by the converter: + *[Image OCR] + MOCK_OCR_TEXT_12345 + [End OCR]* + +Note: PPTX slide text uses literal backslash-n (\\n) sequences from the +underlying PPTX converter template; OCR blocks use real newlines. +""" + +import sys +from pathlib import Path +from typing import Any + +import pytest + +sys.path.insert(0, str(Path(__file__).parent.parent / "src")) + +from markitdown_ocr._ocr_service import OCRResult # noqa: E402 +from markitdown_ocr._pptx_converter_with_ocr import ( # noqa: E402 + PptxConverterWithOCR, +) +from markitdown import StreamInfo # noqa: E402 + +TEST_DATA_DIR = Path(__file__).parent / "ocr_test_data" + +_MOCK_TEXT = "MOCK_OCR_TEXT_12345" +_OCR_BLOCK = f"*[Image OCR]\n{_MOCK_TEXT}\n[End OCR]*" + + +class MockOCRService: + def extract_text( + self, # noqa: ANN101 + image_stream: Any, + **kwargs: Any, + ) -> OCRResult: + return OCRResult(text=_MOCK_TEXT, backend_used="mock") + + +@pytest.fixture(scope="module") +def svc() -> MockOCRService: + return MockOCRService() + + +def _convert(filename: str, ocr_service: MockOCRService) -> str: + path = TEST_DATA_DIR / filename + if not path.exists(): + pytest.skip(f"Test file not found: {path}") + converter = PptxConverterWithOCR() + with open(path, "rb") as f: + return converter.convert( + f, StreamInfo(extension=".pptx"), ocr_service=ocr_service + ).text_content + + +# --------------------------------------------------------------------------- +# pptx_image_start.pptx +# --------------------------------------------------------------------------- + + +def test_pptx_image_start(svc: MockOCRService) -> None: + # Slide 1: title "Welcome" followed by an image + expected = ( + "\\n\\n\\n# Welcome\\n\\n" + "\n*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + ) + assert _convert("pptx_image_start.pptx", svc) == expected + + +# --------------------------------------------------------------------------- +# pptx_image_middle.pptx +# --------------------------------------------------------------------------- + + +def test_pptx_image_middle(svc: MockOCRService) -> None: + # Slide 1: Introduction | Slide 2: Architecture + image | Slide 3: Conclusion # noqa: E501 + expected = ( + "\\n\\n\\n# Introduction" + "\\n\\n\\n\\n\\n# Architecture\\n\\n" + "\n*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + "\\n\\n\\n# Conclusion\\n\\n" + ) + assert _convert("pptx_image_middle.pptx", svc) == expected + + +# --------------------------------------------------------------------------- +# pptx_image_end.pptx +# --------------------------------------------------------------------------- + + +def test_pptx_image_end(svc: MockOCRService) -> None: + # Slide 1: Presentation | Slide 2: Thank You + image + expected = ( + "\\n\\n\\n# Presentation" + "\\n\\n\\n\\n\\n# Thank You\\n\\n" + "\n*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + ) + assert _convert("pptx_image_end.pptx", svc) == expected + + +# --------------------------------------------------------------------------- +# pptx_multiple_images.pptx +# --------------------------------------------------------------------------- + + +def test_pptx_multiple_images(svc: MockOCRService) -> None: + # Slide 1: two images, no title text + expected = ( + "\\n\\n\\n# \\n" + "\n*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + "\n\n*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + ) + assert _convert("pptx_multiple_images.pptx", svc) == expected + + +# --------------------------------------------------------------------------- +# pptx_complex_layout.pptx +# --------------------------------------------------------------------------- + + +def test_pptx_complex_layout(svc: MockOCRService) -> None: + expected = ( + "\\n\\n\\n# Product Comparison" + "\\n\\nOur products lead the market\\n" + "\n*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + ) + assert _convert("pptx_complex_layout.pptx", svc) == expected + + +# --------------------------------------------------------------------------- +# No OCR service — no OCR tags emitted +# --------------------------------------------------------------------------- + + +def test_pptx_no_ocr_service_no_tags() -> None: + path = TEST_DATA_DIR / "pptx_image_middle.pptx" + if not path.exists(): + pytest.skip(f"Test file not found: {path}") + converter = PptxConverterWithOCR() + with open(path, "rb") as f: + md = converter.convert(f, StreamInfo(extension=".pptx")).text_content + assert "*[Image OCR]" not in md + assert "[End OCR]*" not in md diff --git a/packages/markitdown-ocr/tests/test_xlsx_converter.py b/packages/markitdown-ocr/tests/test_xlsx_converter.py new file mode 100644 index 000000000..4ab30c600 --- /dev/null +++ b/packages/markitdown-ocr/tests/test_xlsx_converter.py @@ -0,0 +1,249 @@ +""" +Unit tests for XlsxConverterWithOCR. + +For each XLSX test file: convert with a mock OCR service then compare the +full output string against the expected snapshot. + +OCR block format used by the converter: + *[Image OCR] + MOCK_OCR_TEXT_12345 + [End OCR]* + +Images are grouped at the end of each sheet under: + ### Images in this sheet: +""" + +import sys +from pathlib import Path +from typing import Any + +import pytest + +sys.path.insert(0, str(Path(__file__).parent.parent / "src")) + +from markitdown_ocr._ocr_service import OCRResult # noqa: E402 +from markitdown_ocr._xlsx_converter_with_ocr import ( # noqa: E402 + XlsxConverterWithOCR, +) +from markitdown import StreamInfo # noqa: E402 + +TEST_DATA_DIR = Path(__file__).parent / "ocr_test_data" + +_MOCK_TEXT = "MOCK_OCR_TEXT_12345" +_OCR_BLOCK = f"*[Image OCR]\n{_MOCK_TEXT}\n[End OCR]*" +_IMG_SECTION = "### Images in this sheet:" + + +class MockOCRService: + def extract_text( + self, # noqa: ANN101 + image_stream: Any, + **kwargs: Any, + ) -> OCRResult: + return OCRResult(text=_MOCK_TEXT, backend_used="mock") + + +@pytest.fixture(scope="module") +def svc() -> MockOCRService: + return MockOCRService() + + +def _convert(filename: str, ocr_service: MockOCRService) -> str: + path = TEST_DATA_DIR / filename + if not path.exists(): + pytest.skip(f"Test file not found: {path}") + converter = XlsxConverterWithOCR() + with open(path, "rb") as f: + return converter.convert( + f, StreamInfo(extension=".xlsx"), ocr_service=ocr_service + ).text_content + + +# --------------------------------------------------------------------------- +# xlsx_image_start.xlsx +# --------------------------------------------------------------------------- + + +def test_xlsx_image_start(svc: MockOCRService) -> None: + expected = ( + "## Sales Q1\n\n" + "| Product | Sales |\n" + "| --- | --- |\n" + "| Widget A | 100 |\n" + "| Widget B | 150 |\n\n" + "### Images in this sheet:\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "## Forecast Q2\n\n" + "| Projected Sales | Unnamed: 1 |\n" + "| --- | --- |\n" + "| Widget A | 120 |\n" + "| Widget B | 180 |\n\n" + "### Images in this sheet:\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + ) + assert _convert("xlsx_image_start.xlsx", svc) == expected + + +# --------------------------------------------------------------------------- +# xlsx_image_middle.xlsx +# --------------------------------------------------------------------------- + + +def test_xlsx_image_middle(svc: MockOCRService) -> None: + expected = ( + "## Revenue\n\n" + "| Q1 Report | Unnamed: 1 |\n" + "| --- | --- |\n" + "| NaN | NaN |\n" + "| Revenue | $50,000 |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| Profit Margin | 40% |\n\n" + "### Images in this sheet:\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "## Expenses\n\n" + "| Expense Breakdown | Unnamed: 1 |\n" + "| --- | --- |\n" + "| NaN | NaN |\n" + "| Expenses | $30,000 |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| Savings | $5,000 |\n\n" + "### Images in this sheet:\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + ) + assert _convert("xlsx_image_middle.xlsx", svc) == expected + + +# --------------------------------------------------------------------------- +# xlsx_image_end.xlsx +# --------------------------------------------------------------------------- + + +def test_xlsx_image_end(svc: MockOCRService) -> None: + expected = ( + "## Sheet\n\n" + "| Financial Summary | Unnamed: 1 |\n" + "| --- | --- |\n" + "| Total Revenue | $500,000 |\n" + "| Total Expenses | $300,000 |\n" + "| Net Profit | $200,000 |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| Signature: | NaN |\n\n" + "### Images in this sheet:\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "## Budget\n\n" + "| Budget Allocation | Unnamed: 1 |\n" + "| --- | --- |\n" + "| Marketing | $100,000 |\n" + "| R&D | $150,000 |\n" + "| Operations | $50,000 |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| NaN | NaN |\n" + "| Approved: | NaN |\n\n" + "### Images in this sheet:\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + ) + assert _convert("xlsx_image_end.xlsx", svc) == expected + + +# --------------------------------------------------------------------------- +# xlsx_multiple_images.xlsx +# --------------------------------------------------------------------------- + + +def test_xlsx_multiple_images(svc: MockOCRService) -> None: + expected = ( + "## Overview\n\n" + "| Dashboard |\n" + "| --- |\n" + "| Status: Active |\n" + "| NaN |\n" + "| NaN |\n" + "| NaN |\n" + "| NaN |\n" + "| Performance Summary |\n\n" + "### Images in this sheet:\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "## Details\n\n" + "| Detailed Metrics |\n" + "| --- |\n" + "| System Health |\n\n" + "### Images in this sheet:\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "## Summary\n\n" + "| Quarter Summary |\n" + "| --- |\n" + "| Overall Performance |\n\n" + "### Images in this sheet:\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + ) + assert _convert("xlsx_multiple_images.xlsx", svc) == expected + + +# --------------------------------------------------------------------------- +# xlsx_complex_layout.xlsx +# --------------------------------------------------------------------------- + + +def test_xlsx_complex_layout(svc: MockOCRService) -> None: + expected = ( + "## Complex Report\n\n" + "| Annual Report 2024 | Unnamed: 1 |\n" + "| --- | --- |\n" + "| NaN | NaN |\n" + "| Month | Sales |\n" + "| Jan | 1000 |\n" + "| Feb | 1200 |\n" + "| NaN | NaN |\n" + "| Total | 2200 |\n\n" + "### Images in this sheet:\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "## Customers\n\n" + "| Customer Metrics | Unnamed: 1 |\n" + "| --- | --- |\n" + "| NaN | NaN |\n" + "| New Customers | 250 |\n" + "| Retention Rate | 92% |\n\n" + "### Images in this sheet:\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*\n\n" + "## Regions\n\n" + "| Regional Breakdown | Unnamed: 1 |\n" + "| --- | --- |\n" + "| NaN | NaN |\n" + "| Region | Revenue |\n" + "| North | $800K |\n" + "| South | $600K |\n\n" + "### Images in this sheet:\n\n" + "*[Image OCR]\nMOCK_OCR_TEXT_12345\n[End OCR]*" + ) + assert _convert("xlsx_complex_layout.xlsx", svc) == expected + + +# --------------------------------------------------------------------------- +# No OCR service — no OCR tags emitted +# --------------------------------------------------------------------------- + + +def test_xlsx_no_ocr_service_no_tags() -> None: + path = TEST_DATA_DIR / "xlsx_image_middle.xlsx" + if not path.exists(): + pytest.skip(f"Test file not found: {path}") + converter = XlsxConverterWithOCR() + with open(path, "rb") as f: + md = converter.convert(f, StreamInfo(extension=".xlsx")).text_content + assert "*[Image OCR]" not in md + assert "[End OCR]*" not in md diff --git a/packages/markitdown/pyproject.toml b/packages/markitdown/pyproject.toml index afb5d3197..ac3c8d947 100644 --- a/packages/markitdown/pyproject.toml +++ b/packages/markitdown/pyproject.toml @@ -30,30 +30,30 @@ dependencies = [ "magika~=0.6.1", "charset-normalizer", "defusedxml", - "onnxruntime<=1.20.1; sys_platform == 'win32'", ] [project.optional-dependencies] all = [ "python-pptx", - "mammoth", + "mammoth~=1.11.0", "pandas", "openpyxl", "xlrd", "lxml", - "pdfminer.six", + "pdfminer.six>=20251230", + "pdfplumber>=0.11.9", "olefile", "pydub", "SpeechRecognition", "youtube-transcript-api~=1.0.0", "azure-ai-documentintelligence", - "azure-identity" + "azure-identity", ] pptx = ["python-pptx"] -docx = ["mammoth", "lxml"] +docx = ["mammoth~=1.11.0", "lxml"] xlsx = ["pandas", "openpyxl"] xls = ["pandas", "xlrd"] -pdf = ["pdfminer.six"] +pdf = ["pdfminer.six>=20251230", "pdfplumber>=0.11.9"] outlook = ["olefile"] audio-transcription = ["pydub", "SpeechRecognition"] youtube-transcription = ["youtube-transcript-api"] diff --git a/packages/markitdown/src/markitdown/__about__.py b/packages/markitdown/src/markitdown/__about__.py index d2336ca4c..538a2c19a 100644 --- a/packages/markitdown/src/markitdown/__about__.py +++ b/packages/markitdown/src/markitdown/__about__.py @@ -1,4 +1,4 @@ # SPDX-FileCopyrightText: 2024-present Adam Fourney # # SPDX-License-Identifier: MIT -__version__ = "0.1.2" +__version__ = "0.1.6b2" diff --git a/packages/markitdown/src/markitdown/_base_converter.py b/packages/markitdown/src/markitdown/_base_converter.py index a6f2a2d91..fa2b11145 100644 --- a/packages/markitdown/src/markitdown/_base_converter.py +++ b/packages/markitdown/src/markitdown/_base_converter.py @@ -69,7 +69,7 @@ def accepts( data = file_stream.read(100) # ... peek at the first 100 bytes, etc. file_stream.seek(cur_pos) # Reset the position to the original position - Prameters: + Parameters: - file_stream: The file-like object to convert. Must support seek(), tell(), and read() methods. - stream_info: The StreamInfo object containing metadata about the file (mimetype, extension, charset, set) - kwargs: Additional keyword arguments for the converter. @@ -90,7 +90,7 @@ def convert( """ Convert a document to Markdown text. - Prameters: + Parameters: - file_stream: The file-like object to convert. Must support seek(), tell(), and read() methods. - stream_info: The StreamInfo object containing metadata about the file (mimetype, extension, charset, set) - kwargs: Additional keyword arguments for the converter. diff --git a/packages/markitdown/src/markitdown/_markitdown.py b/packages/markitdown/src/markitdown/_markitdown.py index 3027efc65..f342a614b 100644 --- a/packages/markitdown/src/markitdown/_markitdown.py +++ b/packages/markitdown/src/markitdown/_markitdown.py @@ -107,6 +107,13 @@ def __init__( requests_session = kwargs.get("requests_session") if requests_session is None: self._requests_session = requests.Session() + # Signal that we prefer markdown over HTML, etc. if the server supports it. + # e.g., https://blog.cloudflare.com/markdown-for-agents/ + self._requests_session.headers.update( + { + "Accept": "text/markdown, text/html;q=0.9, text/plain;q=0.8, */*;q=0.1" + } + ) else: self._requests_session = requests_session @@ -115,6 +122,7 @@ def __init__( # TODO - remove these (see enable_builtins) self._llm_client: Any = None self._llm_model: Union[str | None] = None + self._llm_prompt: Union[str | None] = None self._exiftool_path: Union[str | None] = None self._style_map: Union[str | None] = None @@ -139,6 +147,7 @@ def enable_builtins(self, **kwargs) -> None: # TODO: Move these into converter constructors self._llm_client = kwargs.get("llm_client") self._llm_model = kwargs.get("llm_model") + self._llm_prompt = kwargs.get("llm_prompt") self._exiftool_path = kwargs.get("exiftool_path") self._style_map = kwargs.get("style_map") @@ -559,6 +568,9 @@ def _convert( if "llm_model" not in _kwargs and self._llm_model is not None: _kwargs["llm_model"] = self._llm_model + if "llm_prompt" not in _kwargs and self._llm_prompt is not None: + _kwargs["llm_prompt"] = self._llm_prompt + if "style_map" not in _kwargs and self._style_map is not None: _kwargs["style_map"] = self._style_map diff --git a/packages/markitdown/src/markitdown/converters/_doc_intel_converter.py b/packages/markitdown/src/markitdown/converters/_doc_intel_converter.py index ba66b5b5a..fd843f231 100644 --- a/packages/markitdown/src/markitdown/converters/_doc_intel_converter.py +++ b/packages/markitdown/src/markitdown/converters/_doc_intel_converter.py @@ -84,6 +84,9 @@ def _get_mime_type_prefixes(types: List[DocumentIntelligenceFileType]) -> List[s prefixes.append( "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) + elif type_ == DocumentIntelligenceFileType.HTML: + prefixes.append("text/html") + prefixes.append("application/xhtml+xml") elif type_ == DocumentIntelligenceFileType.PDF: prefixes.append("application/pdf") prefixes.append("application/x-pdf") @@ -119,6 +122,8 @@ def _get_file_extensions(types: List[DocumentIntelligenceFileType]) -> List[str] extensions.append(".bmp") elif type_ == DocumentIntelligenceFileType.TIFF: extensions.append(".tiff") + elif type_ == DocumentIntelligenceFileType.HTML: + extensions.append(".html") return extensions diff --git a/packages/markitdown/src/markitdown/converters/_docx_converter.py b/packages/markitdown/src/markitdown/converters/_docx_converter.py index 69c1ea833..3975107b1 100644 --- a/packages/markitdown/src/markitdown/converters/_docx_converter.py +++ b/packages/markitdown/src/markitdown/converters/_docx_converter.py @@ -1,4 +1,6 @@ import sys +import io +from warnings import warn from typing import BinaryIO, Any @@ -13,6 +15,7 @@ _dependency_exc_info = None try: import mammoth + except ImportError: # Preserve the error and stack trace for later _dependency_exc_info = sys.exc_info() diff --git a/packages/markitdown/src/markitdown/converters/_exiftool.py b/packages/markitdown/src/markitdown/converters/_exiftool.py index 1af155f28..f605024fd 100644 --- a/packages/markitdown/src/markitdown/converters/_exiftool.py +++ b/packages/markitdown/src/markitdown/converters/_exiftool.py @@ -1,7 +1,11 @@ import json -import subprocess import locale -from typing import BinaryIO, Any, Union +import subprocess +from typing import Any, BinaryIO, Union + + +def _parse_version(version: str) -> tuple: + return tuple(map(int, (version.split(".")))) def exiftool_metadata( @@ -13,6 +17,24 @@ def exiftool_metadata( if not exiftool_path: return {} + # Verify exiftool version + try: + version_output = subprocess.run( + [exiftool_path, "-ver"], + capture_output=True, + text=True, + check=True, + ).stdout.strip() + version = _parse_version(version_output) + min_version = (12, 24) + if version < min_version: + raise RuntimeError( + f"ExifTool version {version_output} is vulnerable to CVE-2021-22204. " + "Please upgrade to version 12.24 or later." + ) + except (subprocess.CalledProcessError, ValueError) as e: + raise RuntimeError("Failed to verify ExifTool version.") from e + # Run exiftool cur_pos = file_stream.tell() try: diff --git a/packages/markitdown/src/markitdown/converters/_markdownify.py b/packages/markitdown/src/markitdown/converters/_markdownify.py index d98bdfbd7..19e8a2984 100644 --- a/packages/markitdown/src/markitdown/converters/_markdownify.py +++ b/packages/markitdown/src/markitdown/converters/_markdownify.py @@ -92,9 +92,11 @@ def convert_img( """Same as usual converter, but removes data URIs""" alt = el.attrs.get("alt", None) or "" - src = el.attrs.get("src", None) or "" + src = el.attrs.get("src", None) or el.attrs.get("data-src", None) or "" title = el.attrs.get("title", None) or "" title_part = ' "%s"' % title.replace('"', r"\"") if title else "" + # Remove all line breaks from alt + alt = alt.replace("\n", " ") if ( convert_as_inline and el.parent.name not in self.options["keep_inline_images_in"] @@ -107,5 +109,18 @@ def convert_img( return "![%s](%s%s)" % (alt, src, title_part) + def convert_input( + self, + el: Any, + text: str, + convert_as_inline: Optional[bool] = False, + **kwargs, + ) -> str: + """Convert checkboxes to Markdown [x]/[ ] syntax.""" + + if el.get("type") == "checkbox": + return "[x] " if el.has_attr("checked") else "[ ] " + return "" + def convert_soup(self, soup: Any) -> str: return super().convert_soup(soup) # type: ignore diff --git a/packages/markitdown/src/markitdown/converters/_pdf_converter.py b/packages/markitdown/src/markitdown/converters/_pdf_converter.py index 63162d523..ffbcbd990 100644 --- a/packages/markitdown/src/markitdown/converters/_pdf_converter.py +++ b/packages/markitdown/src/markitdown/converters/_pdf_converter.py @@ -1,22 +1,69 @@ import sys import io - +import re from typing import BinaryIO, Any - from .._base_converter import DocumentConverter, DocumentConverterResult from .._stream_info import StreamInfo from .._exceptions import MissingDependencyException, MISSING_DEPENDENCY_MESSAGE +# Pattern for MasterFormat-style partial numbering (e.g., ".1", ".2", ".10") +PARTIAL_NUMBERING_PATTERN = re.compile(r"^\.\d+$") + + +def _merge_partial_numbering_lines(text: str) -> str: + """ + Post-process extracted text to merge MasterFormat-style partial numbering + with the following text line. + + MasterFormat documents use partial numbering like: + .1 The intent of this Request for Proposal... + .2 Available information relative to... + + Some PDF extractors split these into separate lines: + .1 + The intent of this Request for Proposal... + + This function merges them back together. + """ + lines = text.split("\n") + result_lines: list[str] = [] + i = 0 + + while i < len(lines): + line = lines[i] + stripped = line.strip() + + # Check if this line is ONLY a partial numbering + if PARTIAL_NUMBERING_PATTERN.match(stripped): + # Look for the next non-empty line to merge with + j = i + 1 + while j < len(lines) and not lines[j].strip(): + j += 1 + + if j < len(lines): + # Merge the partial numbering with the next line + next_line = lines[j].strip() + result_lines.append(f"{stripped} {next_line}") + i = j + 1 # Skip past the merged line + else: + # No next line to merge with, keep as is + result_lines.append(line) + i += 1 + else: + result_lines.append(line) + i += 1 + + return "\n".join(result_lines) -# Try loading optional (but in this case, required) dependencies -# Save reporting of any exceptions for later + +# Load dependencies _dependency_exc_info = None try: import pdfminer import pdfminer.high_level + import pdfplumber except ImportError: - # Preserve the error and stack trace for later _dependency_exc_info = sys.exc_info() @@ -28,16 +75,435 @@ ACCEPTED_FILE_EXTENSIONS = [".pdf"] +def _to_markdown_table(table: list[list[str]], include_separator: bool = True) -> str: + """Convert a 2D list (rows/columns) into a nicely aligned Markdown table. + + Args: + table: 2D list of cell values + include_separator: If True, include header separator row (standard markdown). + If False, output simple pipe-separated rows. + """ + if not table: + return "" + + # Normalize None → "" + table = [[cell if cell is not None else "" for cell in row] for row in table] + + # Filter out empty rows + table = [row for row in table if any(cell.strip() for cell in row)] + + if not table: + return "" + + # Column widths + col_widths = [max(len(str(cell)) for cell in col) for col in zip(*table)] + + def fmt_row(row: list[str]) -> str: + return ( + "|" + + "|".join(str(cell).ljust(width) for cell, width in zip(row, col_widths)) + + "|" + ) + + if include_separator: + header, *rows = table + md = [fmt_row(header)] + md.append("|" + "|".join("-" * w for w in col_widths) + "|") + for row in rows: + md.append(fmt_row(row)) + else: + md = [fmt_row(row) for row in table] + + return "\n".join(md) + + +def _extract_form_content_from_words(page: Any) -> str | None: + """ + Extract form-style content from a PDF page by analyzing word positions. + This handles borderless forms/tables where words are aligned in columns. + + Returns markdown with proper table formatting: + - Tables have pipe-separated columns with header separator rows + - Non-table content is rendered as plain text + + Returns None if the page doesn't appear to be a form-style document, + indicating that pdfminer should be used instead for better text spacing. + """ + words = page.extract_words(keep_blank_chars=True, x_tolerance=3, y_tolerance=3) + if not words: + return None + + # Group words by their Y position (rows) + y_tolerance = 5 + rows_by_y: dict[float, list[dict]] = {} + for word in words: + y_key = round(word["top"] / y_tolerance) * y_tolerance + if y_key not in rows_by_y: + rows_by_y[y_key] = [] + rows_by_y[y_key].append(word) + + # Sort rows by Y position + sorted_y_keys = sorted(rows_by_y.keys()) + page_width = page.width if hasattr(page, "width") else 612 + + # First pass: analyze each row + row_info: list[dict] = [] + for y_key in sorted_y_keys: + row_words = sorted(rows_by_y[y_key], key=lambda w: w["x0"]) + if not row_words: + continue + + first_x0 = row_words[0]["x0"] + last_x1 = row_words[-1]["x1"] + line_width = last_x1 - first_x0 + combined_text = " ".join(w["text"] for w in row_words) + + # Count distinct x-position groups (columns) + x_positions = [w["x0"] for w in row_words] + x_groups: list[float] = [] + for x in sorted(x_positions): + if not x_groups or x - x_groups[-1] > 50: + x_groups.append(x) + + # Determine row type + is_paragraph = line_width > page_width * 0.55 and len(combined_text) > 60 + + # Check for MasterFormat-style partial numbering (e.g., ".1", ".2") + # These should be treated as list items, not table rows + has_partial_numbering = False + if row_words: + first_word = row_words[0]["text"].strip() + if PARTIAL_NUMBERING_PATTERN.match(first_word): + has_partial_numbering = True + + row_info.append( + { + "y_key": y_key, + "words": row_words, + "text": combined_text, + "x_groups": x_groups, + "is_paragraph": is_paragraph, + "num_columns": len(x_groups), + "has_partial_numbering": has_partial_numbering, + } + ) + + # Collect ALL x-positions from rows with 3+ columns (table-like rows) + # This gives us the global column structure + all_table_x_positions: list[float] = [] + for info in row_info: + if info["num_columns"] >= 3 and not info["is_paragraph"]: + all_table_x_positions.extend(info["x_groups"]) + + if not all_table_x_positions: + return None + + # Compute adaptive column clustering tolerance based on gap analysis + all_table_x_positions.sort() + + # Calculate gaps between consecutive x-positions + gaps = [] + for i in range(len(all_table_x_positions) - 1): + gap = all_table_x_positions[i + 1] - all_table_x_positions[i] + if gap > 5: # Only significant gaps + gaps.append(gap) + + # Determine optimal tolerance using statistical analysis + if gaps and len(gaps) >= 3: + # Use 70th percentile of gaps as threshold (balances precision/recall) + sorted_gaps = sorted(gaps) + percentile_70_idx = int(len(sorted_gaps) * 0.70) + adaptive_tolerance = sorted_gaps[percentile_70_idx] + + # Clamp tolerance to reasonable range [25, 50] + adaptive_tolerance = max(25, min(50, adaptive_tolerance)) + else: + # Fallback to conservative value + adaptive_tolerance = 35 + + # Compute global column boundaries using adaptive tolerance + global_columns: list[float] = [] + for x in all_table_x_positions: + if not global_columns or x - global_columns[-1] > adaptive_tolerance: + global_columns.append(x) + + # Adaptive max column check based on page characteristics + # Calculate average column width + if len(global_columns) > 1: + content_width = global_columns[-1] - global_columns[0] + avg_col_width = content_width / len(global_columns) + + # Forms with very narrow columns (< 30px) are likely dense text + if avg_col_width < 30: + return None + + # Compute adaptive max based on columns per inch + # Typical forms have 3-8 columns per inch + columns_per_inch = len(global_columns) / (content_width / 72) + + # If density is too high (> 10 cols/inch), likely not a form + if columns_per_inch > 10: + return None + + # Adaptive max: allow more columns for wider pages + # Standard letter is 612pt wide, so scale accordingly + adaptive_max_columns = int(20 * (page_width / 612)) + adaptive_max_columns = max(15, adaptive_max_columns) # At least 15 + + if len(global_columns) > adaptive_max_columns: + return None + else: + # Single column, not a form + return None + + # Now classify each row as table row or not + # A row is a table row if it has words that align with 2+ of the global columns + for info in row_info: + if info["is_paragraph"]: + info["is_table_row"] = False + continue + + # Rows with partial numbering (e.g., ".1", ".2") are list items, not table rows + if info["has_partial_numbering"]: + info["is_table_row"] = False + continue + + # Count how many global columns this row's words align with + aligned_columns: set[int] = set() + for word in info["words"]: + word_x = word["x0"] + for col_idx, col_x in enumerate(global_columns): + if abs(word_x - col_x) < 40: + aligned_columns.add(col_idx) + break + + # If row uses 2+ of the established columns, it's a table row + info["is_table_row"] = len(aligned_columns) >= 2 + + # Find table regions (consecutive table rows) + table_regions: list[tuple[int, int]] = [] # (start_idx, end_idx) + i = 0 + while i < len(row_info): + if row_info[i]["is_table_row"]: + start_idx = i + while i < len(row_info) and row_info[i]["is_table_row"]: + i += 1 + end_idx = i + table_regions.append((start_idx, end_idx)) + else: + i += 1 + + # Check if enough rows are table rows (at least 20%) + total_table_rows = sum(end - start for start, end in table_regions) + if len(row_info) > 0 and total_table_rows / len(row_info) < 0.2: + return None + + # Build output - collect table data first, then format with proper column widths + result_lines: list[str] = [] + num_cols = len(global_columns) + + # Helper function to extract cells from a row + def extract_cells(info: dict) -> list[str]: + cells: list[str] = ["" for _ in range(num_cols)] + for word in info["words"]: + word_x = word["x0"] + # Find the correct column using boundary ranges + assigned_col = num_cols - 1 # Default to last column + for col_idx in range(num_cols - 1): + col_end = global_columns[col_idx + 1] + if word_x < col_end - 20: + assigned_col = col_idx + break + if cells[assigned_col]: + cells[assigned_col] += " " + word["text"] + else: + cells[assigned_col] = word["text"] + return cells + + # Process rows, collecting table data for proper formatting + idx = 0 + while idx < len(row_info): + info = row_info[idx] + + # Check if this row starts a table region + table_region = None + for start, end in table_regions: + if idx == start: + table_region = (start, end) + break + + if table_region: + start, end = table_region + # Collect all rows in this table + table_data: list[list[str]] = [] + for table_idx in range(start, end): + cells = extract_cells(row_info[table_idx]) + table_data.append(cells) + + # Calculate column widths for this table + if table_data: + col_widths = [ + max(len(row[col]) for row in table_data) for col in range(num_cols) + ] + # Ensure minimum width of 3 for separator dashes + col_widths = [max(w, 3) for w in col_widths] + + # Format header row + header = table_data[0] + header_str = ( + "| " + + " | ".join( + cell.ljust(col_widths[i]) for i, cell in enumerate(header) + ) + + " |" + ) + result_lines.append(header_str) + + # Format separator row + separator = ( + "| " + + " | ".join("-" * col_widths[i] for i in range(num_cols)) + + " |" + ) + result_lines.append(separator) + + # Format data rows + for row in table_data[1:]: + row_str = ( + "| " + + " | ".join( + cell.ljust(col_widths[i]) for i, cell in enumerate(row) + ) + + " |" + ) + result_lines.append(row_str) + + idx = end # Skip to end of table region + else: + # Check if we're inside a table region (not at start) + in_table = False + for start, end in table_regions: + if start < idx < end: + in_table = True + break + + if not in_table: + # Non-table content + result_lines.append(info["text"]) + idx += 1 + + return "\n".join(result_lines) + + +def _extract_tables_from_words(page: Any) -> list[list[list[str]]]: + """ + Extract tables from a PDF page by analyzing word positions. + This handles borderless tables where words are aligned in columns. + + This function is designed for structured tabular data (like invoices), + not for multi-column text layouts in scientific documents. + """ + words = page.extract_words(keep_blank_chars=True, x_tolerance=3, y_tolerance=3) + if not words: + return [] + + # Group words by their Y position (rows) + y_tolerance = 5 + rows_by_y: dict[float, list[dict]] = {} + for word in words: + y_key = round(word["top"] / y_tolerance) * y_tolerance + if y_key not in rows_by_y: + rows_by_y[y_key] = [] + rows_by_y[y_key].append(word) + + # Sort rows by Y position + sorted_y_keys = sorted(rows_by_y.keys()) + + # Find potential column boundaries by analyzing x positions across all rows + all_x_positions = [] + for words_in_row in rows_by_y.values(): + for word in words_in_row: + all_x_positions.append(word["x0"]) + + if not all_x_positions: + return [] + + # Cluster x positions to find column starts + all_x_positions.sort() + x_tolerance_col = 20 + column_starts: list[float] = [] + for x in all_x_positions: + if not column_starts or x - column_starts[-1] > x_tolerance_col: + column_starts.append(x) + + # Need at least 3 columns but not too many (likely text layout, not table) + if len(column_starts) < 3 or len(column_starts) > 10: + return [] + + # Find rows that span multiple columns (potential table rows) + table_rows = [] + for y_key in sorted_y_keys: + words_in_row = sorted(rows_by_y[y_key], key=lambda w: w["x0"]) + + # Assign words to columns + row_data = [""] * len(column_starts) + for word in words_in_row: + # Find the closest column + best_col = 0 + min_dist = float("inf") + for i, col_x in enumerate(column_starts): + dist = abs(word["x0"] - col_x) + if dist < min_dist: + min_dist = dist + best_col = i + + if row_data[best_col]: + row_data[best_col] += " " + word["text"] + else: + row_data[best_col] = word["text"] + + # Only include rows that have content in multiple columns + non_empty = sum(1 for cell in row_data if cell.strip()) + if non_empty >= 2: + table_rows.append(row_data) + + # Validate table quality - tables should have: + # 1. Enough rows (at least 3 including header) + # 2. Short cell content (tables have concise data, not paragraphs) + # 3. Consistent structure across rows + if len(table_rows) < 3: + return [] + + # Check if cells contain short, structured data (not long text) + long_cell_count = 0 + total_cell_count = 0 + for row in table_rows: + for cell in row: + if cell.strip(): + total_cell_count += 1 + # If cell has more than 30 chars, it's likely prose text + if len(cell.strip()) > 30: + long_cell_count += 1 + + # If more than 30% of cells are long, this is probably not a table + if total_cell_count > 0 and long_cell_count / total_cell_count > 0.3: + return [] + + return [table_rows] + + class PdfConverter(DocumentConverter): """ - Converts PDFs to Markdown. Most style information is ignored, so the results are essentially plain-text. + Converts PDFs to Markdown. + Supports extracting tables into aligned Markdown format (via pdfplumber). + Falls back to pdfminer if pdfplumber is missing or fails. """ def accepts( self, file_stream: BinaryIO, stream_info: StreamInfo, - **kwargs: Any, # Options to pass to the converter + **kwargs: Any, ) -> bool: mimetype = (stream_info.mimetype or "").lower() extension = (stream_info.extension or "").lower() @@ -55,9 +521,8 @@ def convert( self, file_stream: BinaryIO, stream_info: StreamInfo, - **kwargs: Any, # Options to pass to the converter + **kwargs: Any, ) -> DocumentConverterResult: - # Check the dependencies if _dependency_exc_info is not None: raise MissingDependencyException( MISSING_DEPENDENCY_MESSAGE.format( @@ -65,13 +530,60 @@ def convert( extension=".pdf", feature="pdf", ) - ) from _dependency_exc_info[ - 1 - ].with_traceback( # type: ignore[union-attr] + ) from _dependency_exc_info[1].with_traceback( _dependency_exc_info[2] - ) + ) # type: ignore[union-attr] - assert isinstance(file_stream, io.IOBase) # for mypy - return DocumentConverterResult( - markdown=pdfminer.high_level.extract_text(file_stream), - ) + assert isinstance(file_stream, io.IOBase) + + # Read file stream into BytesIO for compatibility with pdfplumber + pdf_bytes = io.BytesIO(file_stream.read()) + + try: + # Single pass: check every page for form-style content. + # Pages with tables/forms get rich extraction; plain-text + # pages are collected separately. page.close() is called + # after each page to free pdfplumber's cached objects and + # keep memory usage constant regardless of page count. + markdown_chunks: list[str] = [] + form_page_count = 0 + plain_page_indices: list[int] = [] + + with pdfplumber.open(pdf_bytes) as pdf: + for page_idx, page in enumerate(pdf.pages): + page_content = _extract_form_content_from_words(page) + + if page_content is not None: + form_page_count += 1 + if page_content.strip(): + markdown_chunks.append(page_content) + else: + plain_page_indices.append(page_idx) + text = page.extract_text() + if text and text.strip(): + markdown_chunks.append(text.strip()) + + page.close() # Free cached page data immediately + + # If no pages had form-style content, use pdfminer for + # the whole document (better text spacing for prose). + if form_page_count == 0: + pdf_bytes.seek(0) + markdown = pdfminer.high_level.extract_text(pdf_bytes) + else: + markdown = "\n\n".join(markdown_chunks).strip() + + except Exception: + # Fallback if pdfplumber fails + pdf_bytes.seek(0) + markdown = pdfminer.high_level.extract_text(pdf_bytes) + + # Fallback if still empty + if not markdown: + pdf_bytes.seek(0) + markdown = pdfminer.high_level.extract_text(pdf_bytes) + + # Post-process to merge MasterFormat-style partial numbering with following text + markdown = _merge_partial_numbering_lines(markdown) + + return DocumentConverterResult(markdown=markdown) diff --git a/packages/markitdown/src/markitdown/converters/_pptx_converter.py b/packages/markitdown/src/markitdown/converters/_pptx_converter.py index 087da32c5..360f17706 100644 --- a/packages/markitdown/src/markitdown/converters/_pptx_converter.py +++ b/packages/markitdown/src/markitdown/converters/_pptx_converter.py @@ -168,11 +168,23 @@ def get_shape_content(shape, **kwargs): # Group Shapes if shape.shape_type == pptx.enum.shapes.MSO_SHAPE_TYPE.GROUP: - sorted_shapes = sorted(shape.shapes, key=attrgetter("top", "left")) + sorted_shapes = sorted( + shape.shapes, + key=lambda x: ( + float("-inf") if not x.top else x.top, + float("-inf") if not x.left else x.left, + ), + ) for subshape in sorted_shapes: get_shape_content(subshape, **kwargs) - sorted_shapes = sorted(slide.shapes, key=attrgetter("top", "left")) + sorted_shapes = sorted( + slide.shapes, + key=lambda x: ( + float("-inf") if not x.top else x.top, + float("-inf") if not x.left else x.left, + ), + ) for shape in sorted_shapes: get_shape_content(shape, **kwargs) diff --git a/packages/markitdown/tests/test_docintel_html.py b/packages/markitdown/tests/test_docintel_html.py new file mode 100644 index 000000000..d0b4caa3e --- /dev/null +++ b/packages/markitdown/tests/test_docintel_html.py @@ -0,0 +1,26 @@ +import io +from markitdown.converters._doc_intel_converter import ( + DocumentIntelligenceConverter, + DocumentIntelligenceFileType, +) +from markitdown._stream_info import StreamInfo + + +def _make_converter(file_types): + conv = DocumentIntelligenceConverter.__new__(DocumentIntelligenceConverter) + conv._file_types = file_types + return conv + + +def test_docintel_accepts_html_extension(): + conv = _make_converter([DocumentIntelligenceFileType.HTML]) + stream_info = StreamInfo(mimetype=None, extension=".html") + assert conv.accepts(io.BytesIO(b""), stream_info) + + +def test_docintel_accepts_html_mimetype(): + conv = _make_converter([DocumentIntelligenceFileType.HTML]) + stream_info = StreamInfo(mimetype="text/html", extension=None) + assert conv.accepts(io.BytesIO(b""), stream_info) + stream_info = StreamInfo(mimetype="application/xhtml+xml", extension=None) + assert conv.accepts(io.BytesIO(b""), stream_info) diff --git a/packages/markitdown/tests/test_files/MEDRPT-2024-PAT-3847_medical_report_scan.pdf b/packages/markitdown/tests/test_files/MEDRPT-2024-PAT-3847_medical_report_scan.pdf new file mode 100644 index 000000000..30e1960a0 Binary files /dev/null and b/packages/markitdown/tests/test_files/MEDRPT-2024-PAT-3847_medical_report_scan.pdf differ diff --git a/packages/markitdown/tests/test_files/RECEIPT-2024-TXN-98765_retail_purchase.pdf b/packages/markitdown/tests/test_files/RECEIPT-2024-TXN-98765_retail_purchase.pdf new file mode 100644 index 000000000..34842dc78 Binary files /dev/null and b/packages/markitdown/tests/test_files/RECEIPT-2024-TXN-98765_retail_purchase.pdf differ diff --git a/packages/markitdown/tests/test_files/REPAIR-2022-INV-001_multipage.pdf b/packages/markitdown/tests/test_files/REPAIR-2022-INV-001_multipage.pdf new file mode 100644 index 000000000..c795d9e1b Binary files /dev/null and b/packages/markitdown/tests/test_files/REPAIR-2022-INV-001_multipage.pdf differ diff --git a/packages/markitdown/tests/test_files/SPARSE-2024-INV-1234_borderless_table.pdf b/packages/markitdown/tests/test_files/SPARSE-2024-INV-1234_borderless_table.pdf new file mode 100644 index 000000000..e8ba29fe9 Binary files /dev/null and b/packages/markitdown/tests/test_files/SPARSE-2024-INV-1234_borderless_table.pdf differ diff --git a/packages/markitdown/tests/test_files/expected_outputs/MEDRPT-2024-PAT-3847_medical_report_scan.md b/packages/markitdown/tests/test_files/expected_outputs/MEDRPT-2024-PAT-3847_medical_report_scan.md new file mode 100644 index 000000000..e69de29bb diff --git a/packages/markitdown/tests/test_files/expected_outputs/RECEIPT-2024-TXN-98765_retail_purchase.md b/packages/markitdown/tests/test_files/expected_outputs/RECEIPT-2024-TXN-98765_retail_purchase.md new file mode 100644 index 000000000..379de4df7 --- /dev/null +++ b/packages/markitdown/tests/test_files/expected_outputs/RECEIPT-2024-TXN-98765_retail_purchase.md @@ -0,0 +1,81 @@ +TECHMART ELECTRONICS +4567 Innovation Blvd +San Francisco, CA 94103 +(415) 555-0199 + +=================================== + +Store #0342 - Downtown SF +11/23/2024 14:32:18 PST +TXN: TXN-98765-2024 +Cashier: Emily Rodriguez +Register: POS-07 + +----------------------------------- + +Wireless Noise-Cancelling +Headphones - Premium Black +AUDIO-5521 1 @ $349.99 +Member Discount $-50.00 +$299.99 +USB-C Hub 7-in-1 Adapter +with HDMI & Ethernet +ACC-8834 2 @ $79.99 +$159.98 +Portable SSD 2TB +Thunderbolt 3 Compatible +STOR-2241 1 @ $289.00 +Member Discount $-29.00 +$260.00 +Ergonomic Wireless Mouse +Rechargeable Battery +ACC-9012 1 @ $59.99 +$59.99 +Screen Cleaning Kit +Professional Grade +CARE-1156 3 @ $12.99 +$38.97 +HDMI 2.1 Cable 6ft +8K Resolution Support +CABLE-7789 2 @ $24.99 +Member Discount $-5.00 +$44.98 +----------------------------------- + +SUBTOTAL $863.91 +Member Discount (15%)-$84.00 +Sales Tax (8.5%) $66.23 +Rewards Applied -$25.00 +=================================== +TOTAL $821.14 +=================================== + +PAYMENT METHOD +Visa Card ending in 4782 +Auth: 847392 +Ref: REF-20241123-98765 + +----------------------------------- + +REWARDS MEMBER +Sarah Mitchell +ID: TM-447821 +Points Earned: 821 +Total Points: 3,247 +Next Reward: $50 gift card +at 5,000 pts (1,753 to go) + +----------------------------------- + +RETURN POLICY +Returns within 30 days +Receipt required +Electronics must be unopened + +*TXN98765202411231432* + +Thank you for shopping! +www.techmart.example.com + +=================================== + diff --git a/packages/markitdown/tests/test_files/expected_outputs/REPAIR-2022-INV-001_multipage.md b/packages/markitdown/tests/test_files/expected_outputs/REPAIR-2022-INV-001_multipage.md new file mode 100644 index 000000000..e80967b45 --- /dev/null +++ b/packages/markitdown/tests/test_files/expected_outputs/REPAIR-2022-INV-001_multipage.md @@ -0,0 +1,76 @@ +ZAVA AUTO REPAIR +Certified Collision Repair +123 Main Street, Redmond, WA 98052 +Phone: (425) 000-0000 +Preliminary Estimate (ID: EST-1008) +| Customer Information | | | Vehicle Information | | +| -------------------- | ------------------- | --- | ------------------- | ----------------- | +| Insured name | Gabriel Diaz | | Year | 2022 | +| Claim # | SF-1008 | | Make | Jeep | +| Policy # | POL-2022-555 | | Model | Grand Cherokee | +| Phone | (425) 111-1111 | | Trim | Limited | +| Email | gabriel@contoso.com | | VIN | 1C4RJFBG2NC123456 | +| | | | Color | White | +| | | | Odometer | 9,800 | +| Repair Order # | RO-20221108 | | Estimator | Ellis Turner | +Estimate Totals +| | | Hours | Rate | Cost | +| ---------------- | --- | ----- | ---- | ----- | +| Parts | | | | 2,100 | +| Body Labor | | 2 | 150 | 300 | +| Paint Labor | | 1.5 | 150 | 225 | +| Mechanical Labor | | - | - | - | +Supplies +| | Paint Supplies | | | 60 | +| ------------- | ------------------------ | --- | ------ | ------ | +| | Body Supplies | | | 30 | +| Other Charges | | | | 15 | +| Subtotal | | | | 2,730 | +| Sales Tax | | | 10.20% | 278.46 | +| GRAND TOTAL | | | | 5,738 | +| Note | Minor rear bumper repair | | | | +This is a preliminary estimate for the visible damage of the vehicle. Additional damage / repairs / parts may be found +after the vehicle has been disassembled and damaged parts have been removed. Suspension damages may be +present, but can not be determined until an alignment on the vehicle has been done. Parts Prices may vary due to +models and vehicle maker price updates. Please be advised if vehicle owner elects to have vehicle sent to service for +any mechanical concerns, ALL service departments charge a vehicle diagnostic charge. If the mechanical concern is +deemed not related to an insurance claim, vehicle owner will be reponsible for charges. + +ZAVA AUTO REPAIR +Certified Collision Repair +123 Main Street, Redmond, WA 98052 +Phone: (425) 000-0000 +Preliminary Estimate (ID: EST-1008) +Customer Information Vehicle Information +| Insured name | Bruce Wayne | | Year | 2025 | +| -------------- | -------------------------- | --- | --------- | ------------ | +| Claim # | | 999 | Make | Batman | +| Policy # | IM-BATMAN | | Model | Batmobile | +| Phone | (416) 555-1234 | | Trim | Limited | +| Email | batman@wayneindustries.com | | VIN | XXX | +| | | | Color | Black | +| | | | Odometer | 1 | +| Repair Order # | RO-20221108 | | Estimator | Ellis Turner | +Estimate Totals +| | | Hours | Rate | Cost | +| ---------------- | --- | ----- | ---- | ------ | +| Parts | | | | 99,999 | +| Body Labor | | 2 | 150 | 300 | +| Paint Labor | | 1.5 | 150 | 225 | +| Mechanical Labor | | - | - | - | +Supplies +| | Paint Supplies | | | 60 | +| ------------- | ------------------------ | --- | ------ | --------- | +| | Body Supplies | | | 30 | +| Other Charges | | | | 15 | +| Subtotal | | | | 100,629 | +| Sales Tax | | | 10.20% | 10264.158 | +| GRAND TOTAL | | | | 211,522 | +| Note | Minor rear bumper repair | | | | + +This is a preliminary estimate for the visible damage of the vehicle. Additional damage / repairs / parts may be found +after the vehicle has been disassembled and damaged parts have been removed. Suspension damages may be +present, but can not be determined until an alignment on the vehicle has been done. Parts Prices may vary due to +models and vehicle maker price updates. Please be advised if vehicle owner elects to have vehicle sent to service for +any mechanical concerns, ALL service departments charge a vehicle diagnostic charge. If the mechanical concern is +deemed not related to an insurance claim, vehicle owner will be reponsible for charges. \ No newline at end of file diff --git a/packages/markitdown/tests/test_files/expected_outputs/SPARSE-2024-INV-1234_borderless_table.md b/packages/markitdown/tests/test_files/expected_outputs/SPARSE-2024-INV-1234_borderless_table.md new file mode 100644 index 000000000..797496452 --- /dev/null +++ b/packages/markitdown/tests/test_files/expected_outputs/SPARSE-2024-INV-1234_borderless_table.md @@ -0,0 +1,44 @@ +INVENTORY RECONCILIATION REPORT +Report ID: SPARSE-2024-INV-1234 +Warehouse: Distribution Center East +Report Date: 2024-11-15 +Prepared By: Sarah Martinez +| Product Code | Location | Expected | Actual | Variance | Status | +| ------------ | -------- | -------- | ------ | -------- | -------- | +| SKU-8847 | A-12 | 450 | | | | +| | B-07 | | 289 | -23 | | +| SKU-9201 | | 780 | 778 | | OK | +| | C-15 | | | +15 | | +| SKU-4563 | D-22 | | 156 | | CRITICAL | +| | | 180 | | -24 | | +| SKU-7728 | A-08 | 920 | | | | +| | | | 935 | +15 | OK | +Variance Analysis: +Summary Statistics: +Total Variance Cost: $4,287.50 +Critical Items: 1 +Overall Accuracy: 97.2% +Detailed Analysis by Category: +The inventory reconciliation reveals several key findings. The primary variance driver is SKU-4563, +which shows a -24 unit discrepancy requiring immediate investigation. Location B-07 handling of +SKU-8847 also demonstrates significant variance. Cross-location verification protocols should be + +reviewed to prevent future discrepancies. The overall accuracy rate of 97.2% meets our target +threshold, but critical items require expedited resolution to maintain operational efficiency. +Extended Inventory Review: +| Product Code | Category | Unit Cost | Total Value | Last Audit | Notes | +| ------------ | ----------- | --------- | ----------- | ---------- | ---------- | +| SKU-8847 | Electronics | $45.00 | $13,005.00 | 2024-10-15 | | +| SKU-9201 | Hardware | $32.50 | $25,285.00 | 2024-10-22 | Verified | +| SKU-4563 | Software | $120.00 | $18,720.00 | | Critical | +| SKU-7728 | Accessories | $15.75 | $14,726.25 | 2024-11-01 | | +| SKU-3345 | Electronics | $67.00 | $22,445.00 | 2024-10-18 | | +| SKU-5512 | Hardware | $89.00 | $31,150.00 | | Pending | +| SKU-6678 | Software | $200.00 | $42,000.00 | 2024-10-25 | High Value | +| SKU-7789 | Accessories | $8.50 | $5,950.00 | 2024-11-05 | | +| SKU-2234 | Electronics | $125.00 | $35,000.00 | | | +| SKU-1123 | Hardware | $55.00 | $27,500.00 | 2024-10-30 | Verified | +Recommendations: +1. Immediate review of SKU-4563 handling procedures. 2. Implement additional verification for critical +items. 3. Schedule follow-up audit for high-value products (SKU-6678, SKU-2234). +Approval: \ No newline at end of file diff --git a/packages/markitdown/tests/test_files/expected_outputs/movie-theater-booking-2024.md b/packages/markitdown/tests/test_files/expected_outputs/movie-theater-booking-2024.md new file mode 100644 index 000000000..371cee776 --- /dev/null +++ b/packages/markitdown/tests/test_files/expected_outputs/movie-theater-booking-2024.md @@ -0,0 +1,62 @@ +BOOKING ORDER +Print Date 12/15/2024 14:30:22 +Page 1 of 1 +STARLIGHT CINEMAS +Orders +| Order / Rev: | 2024-12-5678 | | | Cinema: | | Downtown Multiplex | +| ------------ | -------------- | --- | --- | ---------------- | --- | ------------------ | +| Alt Order #: | SC-WINTER-2024 | | | Primary Contact: | | Sarah Johnson | +Product Desc: Holiday Movie Marathon Package Location: NYC-01 +| Estimate: | EST-456 | | | Region: | | NORTHEAST | +| -------------------- | ----------------------- | --- | --- | ------- | --- | --------- | +| Booking Dates: | 12/20/2024 - 12/31/2024 | | | | | | +| Original Date / Rev: | 12/01/24 / 12/10/24 | | | | | | +| Order Type: | Premium Package | | | | | | +Booking Agency +| Name: | Premier Entertainment Group | | | | | | +| ---------------- | --------------------------- | --- | --- | -------------- | --- | --------- | +| | | | | Billing Type: | | Net 30 | +| Contact: | Michael Chen | | | | | | +| | | | | Payment Terms: | | Corporate | +| Billing Contact: | accounting@premierent.com | | | | | | +| | | | | Commission: | | 10% | +555 Broadway Suite 1200 +New York, NY 10012 +Customer +| Name: | Universal Studios Distribution | | | | | | +| -------------- | ------------------------------ | --- | --- | --- | --- | --- | +| Category: | Film Distributor | | | | | | +| Contact Email: | bookings@universalstudios.com | | | | | | +| Customer ID: | CUST-98765 | | | | | | +| Revenue Code: | FILM-PREMIUM | | | | | | +Booking Summary +| Start Date | End Date | # Shows | Gross Amount | Net Amount | | | +| ---------- | -------- | ------- | ------------ | ---------- | --- | --- | +| 12/20/24 | 12/31/24 | 48 | $12,500.00 | $11,250.00 | | | +Totals +| Month | # Shows | Gross Amount | | Net Amount | | Occupancy | +| ------------- | ------- | ------------ | --- | ---------- | --- | --------- | +| December 2024 | 48 | $12,500.00 | | $11,250.00 | | 85% | +| Totals | 48 | $12,500.00 | | $11,250.00 | | 85% | +Account Representatives +Representative Territory Region Start Date / End Date Commission % +| Sarah Johnson | NYC Metro | NORTHEAST | 12/20/24 - 12/31/24 | | 100% | | +| ------------- | --------- | --------- | ------------------- | --- | ---- | --- | +Show Schedule Details +Ln Screen Start End Movie Title Format Showtime Days Shows Rate Type Total +1 SCR-1 12/20/24 12/25/24 Holiday Spectacular IMAX 3D 7:00 PM Daily 12 $250 PM $3,000 +(Runtime: 142 min); Holiday Season Premium +2 SCR-2 12/20/24 12/31/24 Winter Wonderland Standard 4:30 PM Daily 24 $150 MT $3,600 +(Runtime: 98 min); Matinee Special +3 SCR-1 12/26/24 12/31/24 New Year Mystery 4DX 9:30 PM Daily 12 $300 PM $3,600 +(Runtime: 116 min); Premium Experience +Show Details +| Show Screen | Date Range | Title | Showtime | Days Type | Rate | Revenue | +| ----------- | ---------- | ----- | -------- | --------- | ---- | ------- | +1 SCR-1 12/20-12/25 Holiday Spectacular 7:00 PM Daily PM $250 $3,000 +This booking order is subject to cinema availability and standard terms. +2 SCR-2 12/20-12/31 Winter Wonderland 4:30 PM Daily MT $150 $3,600 +All showtimes are approximate and subject to change. +3 SCR-1 12/26-12/31 New Year Mystery 9:30 PM Daily PM $300 $3,600 +| Total Revenue: | | | | | | $12,500.00 | +| -------------- | --- | --- | --- | --- | --- | ---------- | \ No newline at end of file diff --git a/packages/markitdown/tests/test_files/expected_outputs/test.md b/packages/markitdown/tests/test_files/expected_outputs/test.md new file mode 100644 index 000000000..2d9c90c07 --- /dev/null +++ b/packages/markitdown/tests/test_files/expected_outputs/test.md @@ -0,0 +1,65 @@ +1 + +Introduction + +Large language models (LLMs) are becoming a crucial building block in developing powerful agents +that utilize LLMs for reasoning, tool usage, and adapting to new observations (Yao et al., 2022; Xi +et al., 2023; Wang et al., 2023b) in many real-world tasks. Given the expanding tasks that could +benefit from LLMs and the growing task complexity, an intuitive approach to scale up the power of +agents is to use multiple agents that cooperate. Prior work suggests that multiple agents can help +encourage divergent thinking (Liang et al., 2023), improve factuality and reasoning (Du et al., 2023), +and provide validation (Wu et al., 2023). In light of the intuition and early evidence of promise, it is +intriguing to ask the following question: how can we facilitate the development of LLM applications +that could span a broad spectrum of domains and complexities based on the multi-agent approach? + +Our insight is to use multi-agent conversations to achieve it. There are at least three reasons con- +firming its general feasibility and utility thanks to recent advances in LLMs: First, because chat- +optimized LLMs (e.g., GPT-4) show the ability to incorporate feedback, LLM agents can cooperate +through conversations with each other or human(s), e.g., a dialog where agents provide and seek rea- +soning, observations, critiques, and validation. Second, because a single LLM can exhibit a broad +range of capabilities (especially when configured with the correct prompt and inference settings), +conversations between differently configured agents can help combine these broad LLM capabilities +in a modular and complementary manner. Third, LLMs have demonstrated ability to solve complex +tasks when the tasks are broken into simpler subtasks. Multi-agent conversations can enable this +partitioning and integration in an intuitive manner. How can we leverage the above insights and +support different applications with the common requirement of coordinating multiple agents, poten- +tially backed by LLMs, humans, or tools exhibiting different capacities? We desire a multi-agent +conversation framework with generic abstraction and effective implementation that has the flexibil- +ity to satisfy different application needs. Achieving this requires addressing two critical questions: +(1) How can we design individual agents that are capable, reusable, customizable, and effective in +multi-agent collaboration? (2) How can we develop a straightforward, unified interface that can +accommodate a wide range of agent conversation patterns? In practice, applications of varying +complexities may need distinct sets of agents with specific capabilities, and may require different +conversation patterns, such as single- or multi-turn dialogs, different human involvement modes, and +static vs. dynamic conversation. Moreover, developers may prefer the flexibility to program agent +interactions in natural language or code. Failing to adequately address these two questions would +limit the framework’s scope of applicability and generality. +While there is contemporaneous exploration of multi-agent approaches,3 we present AutoGen, a +generalized multi-agent conversation framework (Figure 1), based on the following new concepts. +1 Customizable and conversable agents. AutoGen uses a generic design of agents that can lever- +age LLMs, human inputs, tools, or a combination of them. The result is that developers can +easily and quickly create agents with different roles (e.g., agents to write code, execute code, +wire in human feedback, validate outputs, etc.) by selecting and configuring a subset of built-in +capabilities. The agent’s backend can also be readily extended to allow more custom behaviors. +To make these agents suitable for multi-agent conversation, every agent is made conversable – +they can receive, react, and respond to messages. When configured properly, an agent can hold +multiple turns of conversations with other agents autonomously or solicit human inputs at cer- +tain rounds, enabling human agency and automation. The conversable agent design leverages the +strong capability of the most advanced LLMs in taking feedback and making progress via chat +and also allows combining capabilities of LLMs in a modular fashion. (Section 2.1) + +2 Conversation programming. A fundamental insight of AutoGen is to simplify and unify com- +plex LLM application workflows as multi-agent conversations. So AutoGen adopts a program- +ming paradigm centered around these inter-agent conversations. We refer to this paradigm as +conversation programming, which streamlines the development of intricate applications via two +primary steps: (1) defining a set of conversable agents with specific capabilities and roles (as +described above); (2) programming the interaction behavior between agents via conversation- +centric computation and control. Both steps can be achieved via a fusion of natural and pro- +gramming languages to build applications with a wide range of conversation patterns and agent +behaviors. AutoGen provides ready-to-use implementations and also allows easy extension and +experimentation for both steps. (Section 2.2) + +3We refer to Appendix A for a detailed discussion. + +2 + diff --git a/packages/markitdown/tests/test_files/masterformat_partial_numbering.pdf b/packages/markitdown/tests/test_files/masterformat_partial_numbering.pdf new file mode 100644 index 000000000..246639a83 Binary files /dev/null and b/packages/markitdown/tests/test_files/masterformat_partial_numbering.pdf differ diff --git a/packages/markitdown/tests/test_files/movie-theater-booking-2024.pdf b/packages/markitdown/tests/test_files/movie-theater-booking-2024.pdf new file mode 100644 index 000000000..8555bb833 Binary files /dev/null and b/packages/markitdown/tests/test_files/movie-theater-booking-2024.pdf differ diff --git a/packages/markitdown/tests/test_files/rlink.docx b/packages/markitdown/tests/test_files/rlink.docx new file mode 100755 index 000000000..5afb49d21 Binary files /dev/null and b/packages/markitdown/tests/test_files/rlink.docx differ diff --git a/packages/markitdown/tests/test_module_misc.py b/packages/markitdown/tests/test_module_misc.py index 447e28ab0..8e3acc23d 100644 --- a/packages/markitdown/tests/test_module_misc.py +++ b/packages/markitdown/tests/test_module_misc.py @@ -4,6 +4,7 @@ import re import shutil import pytest +from unittest.mock import MagicMock from markitdown._uri_utils import parse_data_uri, file_uri_to_path @@ -287,6 +288,47 @@ def test_input_as_strings() -> None: assert "# Test" in result.text_content +def test_doc_rlink() -> None: + # Test for: CVE-2025-11849 + markitdown = MarkItDown() + + # Document with rlink + docx_file = os.path.join(TEST_FILES_DIR, "rlink.docx") + + # Directory containing the target rlink file + rlink_tmp_dir = os.path.abspath(os.sep + "tmp") + + # Ensure the tmp directory exists + if not os.path.exists(rlink_tmp_dir): + pytest.skip(f"Skipping rlink test; {rlink_tmp_dir} directory does not exist.") + return + + rlink_file_path = os.path.join(rlink_tmp_dir, "test_rlink.txt") + rlink_content = "de658225-569e-4e3d-9ed2-cfb6abf927fc" + b64_prefix = ( + "ZGU2NTgyMjUtNTY5ZS00ZTNkLTllZDItY2ZiNmFiZjk" # base64 prefix of rlink_content + ) + + if os.path.exists(rlink_file_path): + with open(rlink_file_path, "r", encoding="utf-8") as f: + existing_content = f.read() + if existing_content != rlink_content: + raise ValueError( + f"Existing {rlink_file_path} content does not match expected content." + ) + else: + with open(rlink_file_path, "w", encoding="utf-8") as f: + f.write(rlink_content) + + try: + result = markitdown.convert(docx_file, keep_data_uris=True).text_content + assert ( + b64_prefix not in result + ) # Make sure the target file was NOT embedded in the output + finally: + os.remove(rlink_file_path) + + @pytest.mark.skipif( skip_remote, reason="do not run tests that query external urls", @@ -300,9 +342,9 @@ def test_markitdown_remote() -> None: assert test_string in result.text_content # Youtube - result = markitdown.convert(YOUTUBE_TEST_URL) - for test_string in YOUTUBE_TEST_STRINGS: - assert test_string in result.text_content + # result = markitdown.convert(YOUTUBE_TEST_URL) + # for test_string in YOUTUBE_TEST_STRINGS: + # assert test_string in result.text_content @pytest.mark.skipif( @@ -370,6 +412,50 @@ def test_markitdown_exiftool() -> None: assert target in result.text_content +def test_markitdown_llm_parameters() -> None: + """Test that LLM parameters are correctly passed to the client.""" + mock_client = MagicMock() + mock_response = MagicMock() + mock_response.choices = [ + MagicMock( + message=MagicMock( + content="Test caption with red circle and blue square 5bda1dd6" + ) + ) + ] + mock_client.chat.completions.create.return_value = mock_response + + test_prompt = "You are a professional test prompt." + markitdown = MarkItDown( + llm_client=mock_client, llm_model="gpt-4o", llm_prompt=test_prompt + ) + + # Test image file + markitdown.convert(os.path.join(TEST_FILES_DIR, "test_llm.jpg")) + + # Verify the prompt was passed to the OpenAI API + assert mock_client.chat.completions.create.called + call_args = mock_client.chat.completions.create.call_args + messages = call_args[1]["messages"] + assert len(messages) == 1 + assert messages[0]["content"][0]["text"] == test_prompt + + # Reset the mock for the next test + mock_client.chat.completions.create.reset_mock() + + # TODO: may only use one test after the llm caption method duplicate has been removed: + # https://github.com/microsoft/markitdown/pull/1254 + # Test PPTX file + markitdown.convert(os.path.join(TEST_FILES_DIR, "test.pptx")) + + # Verify the prompt was passed to the OpenAI API for PPTX images too + assert mock_client.chat.completions.create.called + call_args = mock_client.chat.completions.create.call_args + messages = call_args[1]["messages"] + assert len(messages) == 1 + assert messages[0]["content"][0]["text"] == test_prompt + + @pytest.mark.skipif( skip_llm, reason="do not run llm tests without a key", @@ -407,7 +493,9 @@ def test_markitdown_llm() -> None: test_markitdown_remote, test_speech_transcription, test_exceptions, + test_doc_rlink, test_markitdown_exiftool, + test_markitdown_llm_parameters, test_markitdown_llm, ]: print(f"Running {test.__name__}...", end="") diff --git a/packages/markitdown/tests/test_pdf_masterformat.py b/packages/markitdown/tests/test_pdf_masterformat.py new file mode 100644 index 000000000..8d3eb0739 --- /dev/null +++ b/packages/markitdown/tests/test_pdf_masterformat.py @@ -0,0 +1,171 @@ +#!/usr/bin/env python3 -m pytest +"""Tests for MasterFormat-style partial numbering in PDF conversion.""" + +import os +import re +import pytest + +from markitdown import MarkItDown +from markitdown.converters._pdf_converter import PARTIAL_NUMBERING_PATTERN + +TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), "test_files") + + +class TestMasterFormatPartialNumbering: + """Test handling of MasterFormat-style partial numbering (.1, .2, etc.).""" + + def test_partial_numbering_pattern_regex(self): + """Test that the partial numbering regex pattern correctly matches.""" + + # Should match partial numbering patterns + assert PARTIAL_NUMBERING_PATTERN.match(".1") is not None + assert PARTIAL_NUMBERING_PATTERN.match(".2") is not None + assert PARTIAL_NUMBERING_PATTERN.match(".10") is not None + assert PARTIAL_NUMBERING_PATTERN.match(".99") is not None + + # Should NOT match other patterns + assert PARTIAL_NUMBERING_PATTERN.match("1.") is None + assert PARTIAL_NUMBERING_PATTERN.match("1.2") is None + assert PARTIAL_NUMBERING_PATTERN.match(".1.2") is None + assert PARTIAL_NUMBERING_PATTERN.match("text") is None + assert PARTIAL_NUMBERING_PATTERN.match(".a") is None + assert PARTIAL_NUMBERING_PATTERN.match("") is None + + def test_masterformat_partial_numbering_not_split(self): + """Test that MasterFormat partial numbering stays with associated text. + + MasterFormat documents use partial numbering like: + .1 The intent of this Request for Proposal... + .2 Available information relative to... + + These should NOT be split into separate table columns, but kept + as coherent text lines with the number followed by its description. + """ + pdf_path = os.path.join(TEST_FILES_DIR, "masterformat_partial_numbering.pdf") + + markitdown = MarkItDown() + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # Partial numberings should NOT appear isolated on their own lines + # If they're isolated, it means the parser incorrectly split them from their text + lines = text_content.split("\n") + isolated_numberings = [] + for line in lines: + stripped = line.strip() + # Check if line contains ONLY a partial numbering (with possible whitespace/pipes) + cleaned = stripped.replace("|", "").strip() + if cleaned in [".1", ".2", ".3", ".4", ".5", ".6", ".7", ".8", ".9", ".10"]: + isolated_numberings.append(stripped) + + assert len(isolated_numberings) == 0, ( + f"Partial numberings should not be isolated from their text. " + f"Found isolated: {isolated_numberings}" + ) + + # Verify that partial numberings appear WITH following text on the same line + # Look for patterns like ".1 The intent" or ".1 Some text" + partial_with_text = re.findall(r"\.\d+\s+\w+", text_content) + assert ( + len(partial_with_text) > 0 + ), "Expected to find partial numberings followed by text on the same line" + + def test_masterformat_content_preserved(self): + """Test that MasterFormat document content is fully preserved.""" + pdf_path = os.path.join(TEST_FILES_DIR, "masterformat_partial_numbering.pdf") + + markitdown = MarkItDown() + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # Verify key content from the MasterFormat document is preserved + expected_content = [ + "RFP for Construction Management Services", + "Section 00 00 43", + "Instructions to Respondents", + "Ken Sargent House", + "INTENT", + "Request for Proposal", + "KEN SARGENT HOUSE", + "GRANDE PRAIRIE, ALBERTA", + "Section 00 00 45", + ] + + for content in expected_content: + assert ( + content in text_content + ), f"Expected content '{content}' not found in extracted text" + + # Verify partial numbering is followed by text on the same line + # .1 should be followed by "The intent" on the same line + assert re.search( + r"\.1\s+The intent", text_content + ), "Partial numbering .1 should be followed by 'The intent' text" + + # .2 should be followed by "Available information" on the same line + assert re.search( + r"\.2\s+Available information", text_content + ), "Partial numbering .2 should be followed by 'Available information' text" + + # Ensure text content is not empty and has reasonable length + assert ( + len(text_content.strip()) > 100 + ), "MasterFormat document should have substantial text content" + + def test_merge_partial_numbering_with_empty_lines_between(self): + """Test that partial numberings merge correctly even with empty lines between. + + When PDF extractors produce output like: + .1 + + The intent of this Request... + + The merge logic should still combine them properly. + """ + pdf_path = os.path.join(TEST_FILES_DIR, "masterformat_partial_numbering.pdf") + + markitdown = MarkItDown() + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # The merged result should have .1 and .2 followed by text + # Check that we don't have patterns like ".1\n\nThe intent" (unmerged) + lines = text_content.split("\n") + + for i, line in enumerate(lines): + stripped = line.strip() + # If we find an isolated partial numbering, the merge failed + if stripped in [".1", ".2", ".3", ".4", ".5", ".6", ".7", ".8"]: + # Check if next non-empty line exists and wasn't merged + for j in range(i + 1, min(i + 3, len(lines))): + if lines[j].strip(): + pytest.fail( + f"Partial numbering '{stripped}' on line {i} was not " + f"merged with following text '{lines[j].strip()[:30]}...'" + ) + break + + def test_multiple_partial_numberings_all_merged(self): + """Test that all partial numberings in a document are properly merged.""" + pdf_path = os.path.join(TEST_FILES_DIR, "masterformat_partial_numbering.pdf") + + markitdown = MarkItDown() + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # Count occurrences of merged partial numberings (number followed by text) + merged_count = len(re.findall(r"\.\d+\s+[A-Za-z]", text_content)) + + # Count isolated partial numberings (number alone on a line) + isolated_count = 0 + for line in text_content.split("\n"): + stripped = line.strip() + if re.match(r"^\.\d+$", stripped): + isolated_count += 1 + + assert ( + merged_count >= 2 + ), f"Expected at least 2 merged partial numberings, found {merged_count}" + assert ( + isolated_count == 0 + ), f"Found {isolated_count} isolated partial numberings that weren't merged" diff --git a/packages/markitdown/tests/test_pdf_memory.py b/packages/markitdown/tests/test_pdf_memory.py new file mode 100644 index 000000000..1731dd63e --- /dev/null +++ b/packages/markitdown/tests/test_pdf_memory.py @@ -0,0 +1,364 @@ +#!/usr/bin/env python3 -m pytest +"""Tests for PDF converter memory optimization. + +Verifies that: +- page.close() is called after processing each page (frees cached data) +- Plain-text PDFs fall back to pdfminer when no form pages are found +- Mixed PDFs use form extraction only on form-style pages +- Memory stays constant regardless of page count +""" + +import gc +import io +import os +import tracemalloc + +import pytest +from unittest.mock import patch, MagicMock + +from markitdown import MarkItDown + +TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), "test_files") + + +def _has_fpdf2() -> bool: + try: + import fpdf # noqa: F401 + + return True + except ImportError: + return False + + +def _make_form_page(): + """Create a mock page with 3-column table-like word positions.""" + page = MagicMock() + page.width = 612 + page.close = MagicMock() + page.extract_words.return_value = [ + {"text": "Name", "x0": 50, "x1": 100, "top": 10, "bottom": 20}, + {"text": "Value", "x0": 250, "x1": 300, "top": 10, "bottom": 20}, + {"text": "Unit", "x0": 450, "x1": 500, "top": 10, "bottom": 20}, + {"text": "Alpha", "x0": 50, "x1": 100, "top": 30, "bottom": 40}, + {"text": "100", "x0": 250, "x1": 280, "top": 30, "bottom": 40}, + {"text": "kg", "x0": 450, "x1": 470, "top": 30, "bottom": 40}, + {"text": "Beta", "x0": 50, "x1": 100, "top": 50, "bottom": 60}, + {"text": "200", "x0": 250, "x1": 280, "top": 50, "bottom": 60}, + {"text": "lb", "x0": 450, "x1": 470, "top": 50, "bottom": 60}, + ] + return page + + +def _make_plain_page(): + """Create a mock page with single-line paragraph (no table structure).""" + page = MagicMock() + page.width = 612 + page.close = MagicMock() + page.extract_words.return_value = [ + { + "text": "This is a long paragraph of plain text.", + "x0": 50, + "x1": 550, + "top": 10, + "bottom": 20, + }, + ] + page.extract_text.return_value = "This is a long paragraph of plain text." + return page + + +def _mock_pdfplumber_open(pages): + """Return a mock pdfplumber.open that yields the given pages.""" + + def mock_open(stream): + mock_pdf = MagicMock() + mock_pdf.pages = pages + mock_pdf.__enter__ = MagicMock(return_value=mock_pdf) + mock_pdf.__exit__ = MagicMock(return_value=False) + return mock_pdf + + return mock_open + + +class TestPdfMemoryOptimization: + """Test that PDF conversion cleans up per-page caches to limit memory.""" + + def test_page_close_called_on_every_page(self): + """Verify page.close() is called on every page during conversion. + + This ensures cached word/layout data is freed after each page, + preventing O(n) memory growth with page count. + """ + num_pages = 20 + pages = [_make_form_page() for _ in range(num_pages)] + + with patch( + "markitdown.converters._pdf_converter.pdfplumber" + ) as mock_pdfplumber: + mock_pdfplumber.open.side_effect = _mock_pdfplumber_open(pages) + + md = MarkItDown() + buf = io.BytesIO(b"fake pdf content") + from markitdown import StreamInfo + + md.convert_stream( + buf, + stream_info=StreamInfo(extension=".pdf", mimetype="application/pdf"), + ) + + # page.close() must be called on ALL pages + for i, page in enumerate(pages): + assert page.close.called, ( + f"page.close() was NOT called on page {i} — " + "this would cause memory to accumulate" + ) + + def test_plain_text_pdf_falls_back_to_pdfminer(self): + """Verify all-plain-text PDFs fall back to pdfminer. + + When no page has form-style content, the converter should discard + pdfplumber results and use pdfminer for the whole document (better + text spacing for prose). + """ + num_pages = 50 + pages = [_make_plain_page() for _ in range(num_pages)] + + with patch( + "markitdown.converters._pdf_converter.pdfplumber" + ) as mock_pdfplumber, patch( + "markitdown.converters._pdf_converter.pdfminer" + ) as mock_pdfminer: + mock_pdfplumber.open.side_effect = _mock_pdfplumber_open(pages) + mock_pdfminer.high_level.extract_text.return_value = "Plain text content" + + md = MarkItDown() + buf = io.BytesIO(b"fake pdf content") + from markitdown import StreamInfo + + result = md.convert_stream( + buf, + stream_info=StreamInfo(extension=".pdf", mimetype="application/pdf"), + ) + + # pdfminer should be used for the final text extraction + assert mock_pdfminer.high_level.extract_text.called, ( + "pdfminer.high_level.extract_text was not called — " + "plain-text PDFs should fall back to pdfminer" + ) + assert result.text_content is not None + + def test_plain_text_pdf_still_closes_all_pages(self): + """Even for plain-text PDFs, page.close() must be called on every page.""" + num_pages = 30 + pages = [_make_plain_page() for _ in range(num_pages)] + + with patch( + "markitdown.converters._pdf_converter.pdfplumber" + ) as mock_pdfplumber, patch( + "markitdown.converters._pdf_converter.pdfminer" + ) as mock_pdfminer: + mock_pdfplumber.open.side_effect = _mock_pdfplumber_open(pages) + mock_pdfminer.high_level.extract_text.return_value = "text" + + md = MarkItDown() + buf = io.BytesIO(b"fake pdf content") + from markitdown import StreamInfo + + md.convert_stream( + buf, + stream_info=StreamInfo(extension=".pdf", mimetype="application/pdf"), + ) + + for i, page in enumerate(pages): + assert ( + page.close.called + ), f"page.close() was NOT called on plain-text page {i}" + + def test_mixed_pdf_uses_form_extraction_per_page(self): + """In a mixed PDF, form pages get table extraction while plain pages don't. + + Ensures we don't miss form-style pages and don't waste work + running form extraction on plain-text pages. + """ + # Pages 0,2,4 are form-style; pages 1,3 are plain text + pages = [ + _make_form_page(), # 0 - form + _make_plain_page(), # 1 - plain + _make_form_page(), # 2 - form + _make_plain_page(), # 3 - plain + _make_form_page(), # 4 - form + ] + + with patch( + "markitdown.converters._pdf_converter.pdfplumber" + ) as mock_pdfplumber: + mock_pdfplumber.open.side_effect = _mock_pdfplumber_open(pages) + + md = MarkItDown() + buf = io.BytesIO(b"fake pdf content") + from markitdown import StreamInfo + + result = md.convert_stream( + buf, + stream_info=StreamInfo(extension=".pdf", mimetype="application/pdf"), + ) + + # All pages should have close() called + for i, page in enumerate(pages): + assert page.close.called, f"page.close() not called on page {i}" + + # Form pages (0,2,4) should have extract_words called + for i in [0, 2, 4]: + assert pages[ + i + ].extract_words.called, f"extract_words not called on form page {i}" + + # Result should contain table content from form pages + assert result.text_content is not None + assert ( + "|" in result.text_content + ), "Expected markdown table pipes in output from form-style pages" + + def test_only_one_pdfplumber_open_call(self): + """Verify pdfplumber.open is called exactly once (single pass).""" + pages = [_make_form_page() for _ in range(10)] + + with patch( + "markitdown.converters._pdf_converter.pdfplumber" + ) as mock_pdfplumber: + mock_pdfplumber.open.side_effect = _mock_pdfplumber_open(pages) + + md = MarkItDown() + buf = io.BytesIO(b"fake pdf content") + from markitdown import StreamInfo + + md.convert_stream( + buf, + stream_info=StreamInfo(extension=".pdf", mimetype="application/pdf"), + ) + + assert mock_pdfplumber.open.call_count == 1, ( + f"Expected 1 pdfplumber.open call (single pass), " + f"got {mock_pdfplumber.open.call_count}" + ) + + @pytest.mark.skipif( + not os.path.exists(os.path.join(TEST_FILES_DIR, "test.pdf")), + reason="test.pdf not available", + ) + def test_real_pdf_page_cleanup(self): + """Integration test: verify page.close() is called with a real PDF.""" + import pdfplumber + + close_call_count = 0 + original_close = pdfplumber.page.Page.close + + def tracking_close(self): + nonlocal close_call_count + close_call_count += 1 + original_close(self) + + with patch.object(pdfplumber.page.Page, "close", tracking_close): + md = MarkItDown() + pdf_path = os.path.join(TEST_FILES_DIR, "test.pdf") + md.convert(pdf_path) + + assert ( + close_call_count > 0 + ), "page.close() was never called during PDF conversion" + + +def _generate_table_pdf(num_pages: int) -> bytes: + """Generate a PDF with table-like content on every page.""" + from fpdf import FPDF + + pdf = FPDF() + pdf.set_auto_page_break(auto=False) + for page_num in range(num_pages): + pdf.add_page() + pdf.set_font("Helvetica", size=10) + pdf.set_xy(10, 10) + pdf.cell(60, 8, "Parameter", border=1) + pdf.cell(60, 8, "Value", border=1) + pdf.cell(60, 8, "Unit", border=1) + pdf.ln() + for row in range(20): + y = 18 + row * 8 + if y > 270: + break + pdf.set_xy(10, y) + pdf.cell(60, 8, f"Param_{page_num}_{row}", border=1) + pdf.cell(60, 8, f"{(page_num * 100 + row) * 1.23:.2f}", border=1) + pdf.cell(60, 8, "kg/m2", border=1) + return pdf.output() + + +@pytest.mark.skipif( + not _has_fpdf2(), + reason="fpdf2 not installed", +) +class TestPdfMemoryBenchmark: + """Benchmark: verify memory stays constant with page.close() fix.""" + + def test_memory_does_not_grow_linearly(self): + """Peak memory for 200 pages should be far less than without the fix. + + Without page.close(), 200 pages uses ~225 MiB (linear growth). + With the fix, peak memory should stay under 30 MiB. + """ + from markitdown import StreamInfo + + num_pages = 200 + pdf_bytes = _generate_table_pdf(num_pages) + + gc.collect() + tracemalloc.start() + + md = MarkItDown() + buf = io.BytesIO(pdf_bytes) + md.convert_stream(buf, stream_info=StreamInfo(extension=".pdf")) + + _, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + peak_mib = peak / 1024 / 1024 + # Without the fix this would be ~225 MiB. With the fix it should + # be well under 30 MiB. Use a generous threshold to avoid flaky + # failures on different machines. + assert peak_mib < 30, ( + f"Peak memory {peak_mib:.1f} MiB for {num_pages} pages is too high. " + f"Expected < 30 MiB with page.close() fix." + ) + + def test_memory_constant_across_page_counts(self): + """Peak memory should not scale linearly with page count. + + Converts 50-page and 200-page PDFs and asserts the peak memory + ratio is much less than the 4x page count ratio. + """ + from markitdown import StreamInfo + + results = {} + for num_pages in [50, 200]: + pdf_bytes = _generate_table_pdf(num_pages) + + gc.collect() + tracemalloc.start() + + md = MarkItDown() + buf = io.BytesIO(pdf_bytes) + md.convert_stream(buf, stream_info=StreamInfo(extension=".pdf")) + + _, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + results[num_pages] = peak + + ratio = results[200] / results[50] + # With O(n) memory growth the ratio would be ~4x. + # With the fix it should be close to 1x (well under 2x). + assert ratio < 2.0, ( + f"Memory ratio 200p/50p = {ratio:.2f}x — " + f"expected < 2.0x (constant memory). " + f"50p={results[50] / 1024 / 1024:.1f} MiB, " + f"200p={results[200] / 1024 / 1024:.1f} MiB" + ) diff --git a/packages/markitdown/tests/test_pdf_tables.py b/packages/markitdown/tests/test_pdf_tables.py new file mode 100644 index 000000000..d26de0cb9 --- /dev/null +++ b/packages/markitdown/tests/test_pdf_tables.py @@ -0,0 +1,1198 @@ +#!/usr/bin/env python3 -m pytest +"""Tests for PDF table extraction functionality.""" + +import os +import re +import pytest + +from markitdown import MarkItDown + +TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), "test_files") + + +# --- Helper Functions --- +def validate_strings(result, expected_strings, exclude_strings=None): + """Validate presence or absence of specific strings.""" + text_content = result.text_content.replace("\\", "") + for string in expected_strings: + assert string in text_content, f"Expected string not found: {string}" + if exclude_strings: + for string in exclude_strings: + assert string not in text_content, f"Excluded string found: {string}" + + +def validate_markdown_table(result, expected_headers, expected_data_samples): + """Validate that a markdown table exists with expected headers and data.""" + text_content = result.text_content + + # Check for markdown table structure (| header | header |) + assert "|" in text_content, "No markdown table markers found" + + # Check headers are present + for header in expected_headers: + assert header in text_content, f"Expected table header not found: {header}" + + # Check some data values are present + for data in expected_data_samples: + assert data in text_content, f"Expected table data not found: {data}" + + +def extract_markdown_tables(text_content): + """ + Extract all markdown tables from text content. + Returns a list of tables, where each table is a list of rows, + and each row is a list of cell values. + """ + tables = [] + lines = text_content.split("\n") + current_table = [] + in_table = False + + for line in lines: + line = line.strip() + if line.startswith("|") and line.endswith("|"): + # Skip separator rows (contain only dashes and pipes) + if re.match(r"^\|[\s\-|]+\|$", line): + continue + # Parse cells from the row + cells = [cell.strip() for cell in line.split("|")[1:-1]] + current_table.append(cells) + in_table = True + else: + if in_table and current_table: + tables.append(current_table) + current_table = [] + in_table = False + + # Don't forget the last table + if current_table: + tables.append(current_table) + + return tables + + +def validate_table_structure(table): + """ + Validate that a table has consistent structure: + - All rows have the same number of columns + - Has at least a header row and one data row + """ + if not table: + return False, "Table is empty" + + if len(table) < 2: + return False, "Table should have at least header and one data row" + + num_cols = len(table[0]) + if num_cols < 2: + return False, f"Table should have at least 2 columns, found {num_cols}" + + for i, row in enumerate(table): + if len(row) != num_cols: + return False, f"Row {i} has {len(row)} columns, expected {num_cols}" + + return True, "Table structure is valid" + + +class TestPdfTableExtraction: + """Test PDF table extraction with various PDF types.""" + + @pytest.fixture + def markitdown(self): + """Create MarkItDown instance.""" + return MarkItDown() + + def test_borderless_table_extraction(self, markitdown): + """Test extraction of borderless tables from SPARSE inventory PDF. + + Expected output structure: + - Header: INVENTORY RECONCILIATION REPORT with Report ID, Warehouse, Date, Prepared By + - Pipe-separated rows with inventory data + - Text section: Variance Analysis with Summary Statistics + - More pipe-separated rows with extended inventory review + - Footer: Recommendations section + """ + pdf_path = os.path.join( + TEST_FILES_DIR, "SPARSE-2024-INV-1234_borderless_table.pdf" + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # Validate document header content + expected_strings = [ + "INVENTORY RECONCILIATION REPORT", + "Report ID: SPARSE-2024-INV-1234", + "Warehouse: Distribution Center East", + "Report Date: 2024-11-15", + "Prepared By: Sarah Martinez", + ] + validate_strings(result, expected_strings) + + # Validate pipe-separated format is used + assert "|" in text_content, "Should have pipe separators for form-style data" + + # --- Validate First Table Data (Inventory Variance) --- + # Validate table headers are present + first_table_headers = [ + "Product Code", + "Location", + "Expected", + "Actual", + "Variance", + "Status", + ] + for header in first_table_headers: + assert header in text_content, f"Should contain header '{header}'" + + # Validate first table has all expected SKUs + first_table_skus = ["SKU-8847", "SKU-9201", "SKU-4563", "SKU-7728"] + for sku in first_table_skus: + assert sku in text_content, f"Should contain {sku}" + + # Validate first table has correct status values + expected_statuses = ["OK", "CRITICAL"] + for status in expected_statuses: + assert status in text_content, f"Should contain status '{status}'" + + # Validate first table has location codes + expected_locations = ["A-12", "B-07", "C-15", "D-22", "A-08"] + for loc in expected_locations: + assert loc in text_content, f"Should contain location '{loc}'" + + # --- Validate Second Table Data (Extended Inventory Review) --- + # Validate second table headers + second_table_headers = [ + "Category", + "Unit Cost", + "Total Value", + "Last Audit", + "Notes", + ] + for header in second_table_headers: + assert header in text_content, f"Should contain header '{header}'" + + # Validate second table has all expected SKUs (10 products) + second_table_skus = [ + "SKU-8847", + "SKU-9201", + "SKU-4563", + "SKU-7728", + "SKU-3345", + "SKU-5512", + "SKU-6678", + "SKU-7789", + "SKU-2234", + "SKU-1123", + ] + for sku in second_table_skus: + assert sku in text_content, f"Should contain {sku}" + + # Validate second table has categories + expected_categories = ["Electronics", "Hardware", "Software", "Accessories"] + for category in expected_categories: + assert category in text_content, f"Should contain category '{category}'" + + # Validate second table has cost values (spot check) + expected_costs = ["$45.00", "$32.50", "$120.00", "$15.75"] + for cost in expected_costs: + assert cost in text_content, f"Should contain cost '{cost}'" + + # Validate second table has note values + expected_notes = ["Verified", "Critical", "Pending"] + for note in expected_notes: + assert note in text_content, f"Should contain note '{note}'" + + # --- Validate Analysis Text Section --- + analysis_strings = [ + "Variance Analysis:", + "Summary Statistics:", + "Total Variance Cost: $4,287.50", + "Critical Items: 1", + "Overall Accuracy: 97.2%", + "Recommendations:", + ] + validate_strings(result, analysis_strings) + + # --- Validate Document Structure Order --- + # Verify sections appear in correct order + # Note: Using flexible patterns since column merging may occur based on gap detection + import re + + header_pos = text_content.find("INVENTORY RECONCILIATION REPORT") + # Look for Product Code header - may be in same column as Location or separate + first_table_match = re.search(r"\|\s*Product Code", text_content) + variance_pos = text_content.find("Variance Analysis:") + extended_review_pos = text_content.find("Extended Inventory Review:") + # Second table - look for SKU entries after extended review section + # The table may not have pipes on every row due to paragraph detection + second_table_pos = -1 + if extended_review_pos != -1: + # Look for either "| Product Code" or "Product Code" as table header + second_table_match = re.search( + r"Product Code.*Category", text_content[extended_review_pos:] + ) + if second_table_match: + # Adjust position to be relative to full text + second_table_pos = extended_review_pos + second_table_match.start() + recommendations_pos = text_content.find("Recommendations:") + + positions = { + "header": header_pos, + "first_table": first_table_match.start() if first_table_match else -1, + "variance_analysis": variance_pos, + "extended_review": extended_review_pos, + "second_table": second_table_pos, + "recommendations": recommendations_pos, + } + + # All sections should be found + for name, pos in positions.items(): + assert pos != -1, f"Section '{name}' not found in output" + + # Verify correct order + assert ( + positions["header"] < positions["first_table"] + ), "Header should come before first table" + assert ( + positions["first_table"] < positions["variance_analysis"] + ), "First table should come before Variance Analysis" + assert ( + positions["variance_analysis"] < positions["extended_review"] + ), "Variance Analysis should come before Extended Review" + assert ( + positions["extended_review"] < positions["second_table"] + ), "Extended Review should come before second table" + assert ( + positions["second_table"] < positions["recommendations"] + ), "Second table should come before Recommendations" + + def test_borderless_table_no_duplication(self, markitdown): + """Test that borderless table content is not duplicated excessively.""" + pdf_path = os.path.join( + TEST_FILES_DIR, "SPARSE-2024-INV-1234_borderless_table.pdf" + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # Count occurrences of unique table data - should not be excessively duplicated + # SKU-8847 appears in both tables, plus possibly once in summary text + sku_count = text_content.count("SKU-8847") + # Should appear at most 4 times (2 tables + minor text references), not more + assert ( + sku_count <= 4 + ), f"SKU-8847 appears too many times ({sku_count}), suggests duplication issue" + + def test_borderless_table_correct_position(self, markitdown): + """Test that tables appear in correct positions relative to text.""" + pdf_path = os.path.join( + TEST_FILES_DIR, "SPARSE-2024-INV-1234_borderless_table.pdf" + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # Verify content order - header should come before table content, which should come before analysis + header_pos = text_content.find("Prepared By: Sarah Martinez") + # Look for Product Code in any pipe-separated format + product_code_pos = text_content.find("Product Code") + variance_pos = text_content.find("Variance Analysis:") + + assert header_pos != -1, "Header should be found" + assert product_code_pos != -1, "Product Code should be found" + assert variance_pos != -1, "Variance Analysis should be found" + + assert ( + header_pos < product_code_pos < variance_pos + ), "Product data should appear between header and Variance Analysis" + + # Second table content should appear after "Extended Inventory Review" + extended_review_pos = text_content.find("Extended Inventory Review:") + # Look for Category header which is in second table + category_pos = text_content.find("Category") + recommendations_pos = text_content.find("Recommendations:") + + if ( + extended_review_pos != -1 + and category_pos != -1 + and recommendations_pos != -1 + ): + # Find Category position after Extended Inventory Review + category_after_review = text_content.find("Category", extended_review_pos) + if category_after_review != -1: + assert ( + extended_review_pos < category_after_review < recommendations_pos + ), "Extended review table should appear between Extended Inventory Review and Recommendations" + + def test_receipt_pdf_extraction(self, markitdown): + """Test extraction of receipt PDF (no tables, formatted text). + + Expected output structure: + - Store header: TECHMART ELECTRONICS with address + - Transaction info: Store #, date, TXN, Cashier, Register + - Line items: 6 products with prices and member discounts + - Totals: Subtotal, Member Discount, Sales Tax, Rewards, TOTAL + - Payment info: Visa Card, Auth, Ref + - Rewards member info: Name, ID, Points + - Return policy and footer + """ + pdf_path = os.path.join( + TEST_FILES_DIR, "RECEIPT-2024-TXN-98765_retail_purchase.pdf" + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # --- Validate Store Header --- + store_header = [ + "TECHMART ELECTRONICS", + "4567 Innovation Blvd", + "San Francisco, CA 94103", + "(415) 555-0199", + ] + validate_strings(result, store_header) + + # --- Validate Transaction Info --- + transaction_info = [ + "Store #0342 - Downtown SF", + "11/23/2024", + "TXN: TXN-98765-2024", + "Cashier: Emily Rodriguez", + "Register: POS-07", + ] + validate_strings(result, transaction_info) + + # --- Validate Line Items (6 products) --- + line_items = [ + # Product 1: Headphones + "Wireless Noise-Cancelling", + "Headphones - Premium Black", + "AUDIO-5521", + "$349.99", + "$299.99", + # Product 2: USB-C Hub + "USB-C Hub 7-in-1 Adapter", + "ACC-8834", + "$79.99", + "$159.98", + # Product 3: Portable SSD + "Portable SSD 2TB", + "STOR-2241", + "$289.00", + "$260.00", + # Product 4: Wireless Mouse + "Ergonomic Wireless Mouse", + "ACC-9012", + "$59.99", + # Product 5: Screen Cleaning Kit + "Screen Cleaning Kit", + "CARE-1156", + "$12.99", + "$38.97", + # Product 6: HDMI Cable + "HDMI 2.1 Cable 6ft", + "CABLE-7789", + "$24.99", + "$44.98", + ] + validate_strings(result, line_items) + + # --- Validate Totals --- + totals = [ + "SUBTOTAL", + "$863.91", + "Member Discount", + "Sales Tax (8.5%)", + "$66.23", + "Rewards Applied", + "-$25.00", + "TOTAL", + "$821.14", + ] + validate_strings(result, totals) + + # --- Validate Payment Info --- + payment_info = [ + "PAYMENT METHOD", + "Visa Card ending in 4782", + "Auth: 847392", + "REF-20241123-98765", + ] + validate_strings(result, payment_info) + + # --- Validate Rewards Member Info --- + rewards_info = [ + "REWARDS MEMBER", + "Sarah Mitchell", + "ID: TM-447821", + "Points Earned: 821", + "Total Points: 3,247", + ] + validate_strings(result, rewards_info) + + # --- Validate Return Policy & Footer --- + footer_info = [ + "RETURN POLICY", + "Returns within 30 days", + "Receipt required", + "Thank you for shopping!", + "www.techmart.example.com", + ] + validate_strings(result, footer_info) + + # --- Validate Document Structure Order --- + positions = { + "store_header": text_content.find("TECHMART ELECTRONICS"), + "transaction": text_content.find("TXN: TXN-98765-2024"), + "first_item": text_content.find("Wireless Noise-Cancelling"), + "subtotal": text_content.find("SUBTOTAL"), + "total": text_content.find("TOTAL"), + "payment": text_content.find("PAYMENT METHOD"), + "rewards": text_content.find("REWARDS MEMBER"), + "return_policy": text_content.find("RETURN POLICY"), + } + + # All sections should be found + for name, pos in positions.items(): + assert pos != -1, f"Section '{name}' not found in output" + + # Verify correct order + assert ( + positions["store_header"] < positions["transaction"] + ), "Store header should come before transaction" + assert ( + positions["transaction"] < positions["first_item"] + ), "Transaction should come before items" + assert ( + positions["first_item"] < positions["subtotal"] + ), "Items should come before subtotal" + assert ( + positions["subtotal"] < positions["total"] + ), "Subtotal should come before total" + assert ( + positions["total"] < positions["payment"] + ), "Total should come before payment" + assert ( + positions["payment"] < positions["rewards"] + ), "Payment should come before rewards" + assert ( + positions["rewards"] < positions["return_policy"] + ), "Rewards should come before return policy" + + def test_multipage_invoice_extraction(self, markitdown): + """Test extraction of multipage invoice PDF with form-style layout. + + Expected output: Pipe-separated format with clear cell boundaries. + Form data should be extracted with pipes indicating column separations. + """ + pdf_path = os.path.join(TEST_FILES_DIR, "REPAIR-2022-INV-001_multipage.pdf") + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # Validate basic content is extracted + expected_strings = [ + "ZAVA AUTO REPAIR", + "Collision Repair", + "Redmond, WA", + "Gabriel Diaz", + "Jeep", + "Grand Cherokee", + "Parts", + "Body Labor", + "Paint Labor", + "GRAND TOTAL", + # Second page content + "Bruce Wayne", + "Batmobile", + ] + validate_strings(result, expected_strings) + + # Validate pipe-separated table format + # Form-style documents should use pipes to separate cells + assert "|" in text_content, "Form-style PDF should contain pipe separators" + + # Validate key form fields are properly separated + # These patterns check that label and value are in separate cells + # Note: cells may have padding spaces for column alignment + import re + + assert re.search( + r"\| Insured name\s*\|", text_content + ), "Insured name should be in its own cell" + assert re.search( + r"\| Gabriel Diaz\s*\|", text_content + ), "Gabriel Diaz should be in its own cell" + assert re.search( + r"\| Year\s*\|", text_content + ), "Year label should be in its own cell" + assert re.search( + r"\| 2022\s*\|", text_content + ), "Year value should be in its own cell" + + # Validate table structure for estimate totals + assert ( + re.search(r"\| Hours\s*\|", text_content) or "Hours |" in text_content + ), "Hours column header should be present" + assert ( + re.search(r"\| Rate\s*\|", text_content) or "Rate |" in text_content + ), "Rate column header should be present" + assert ( + re.search(r"\| Cost\s*\|", text_content) or "Cost |" in text_content + ), "Cost column header should be present" + + # Validate numeric values are extracted + assert "2,100" in text_content, "Parts cost should be extracted" + assert "300" in text_content, "Body labor cost should be extracted" + assert "225" in text_content, "Paint labor cost should be extracted" + assert "5,738" in text_content, "Grand total should be extracted" + + # Validate second page content (Bruce Wayne invoice) + assert "Bruce Wayne" in text_content, "Second page customer name" + assert "Batmobile" in text_content, "Second page vehicle model" + assert "211,522" in text_content, "Second page grand total" + + # Validate disclaimer text is NOT in table format (long paragraph) + # The disclaimer should be extracted as plain text, not pipe-separated + assert ( + "preliminary estimate" in text_content.lower() + ), "Disclaimer text should be present" + + def test_academic_pdf_extraction(self, markitdown): + """Test extraction of academic paper PDF (scientific document). + + Expected output: Plain text without tables or pipe characters. + Scientific documents should be extracted as flowing text with proper spacing, + not misinterpreted as tables. + """ + pdf_path = os.path.join(TEST_FILES_DIR, "test.pdf") + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # Validate academic paper content with proper spacing + expected_strings = [ + "Introduction", + "Large language models", # Should have proper spacing, not "Largelanguagemodels" + "agents", + "multi-agent", # Should be properly hyphenated + ] + validate_strings(result, expected_strings) + + # Validate proper text formatting (words separated by spaces) + assert "LLMs" in text_content, "Should contain 'LLMs' acronym" + assert "reasoning" in text_content, "Should contain 'reasoning'" + assert "observations" in text_content, "Should contain 'observations'" + + # Ensure content is not empty and has proper length + assert len(text_content) > 1000, "Academic PDF should have substantial content" + + # Scientific documents should NOT have tables or pipe characters + assert ( + "|" not in text_content + ), "Scientific document should not contain pipe characters (no tables)" + + # Verify no markdown tables were extracted + tables = extract_markdown_tables(text_content) + assert ( + len(tables) == 0 + ), f"Scientific document should have no tables, found {len(tables)}" + + # Verify text is properly formatted with spaces between words + # Check that common phrases are NOT joined together (which would indicate bad extraction) + assert ( + "Largelanguagemodels" not in text_content + ), "Text should have proper spacing, not joined words" + assert ( + "multiagentconversations" not in text_content.lower() + ), "Text should have proper spacing between words" + + def test_scanned_pdf_handling(self, markitdown): + """Test handling of scanned/image-based PDF (no text layer). + + Expected output: Empty - scanned PDFs without OCR have no text layer. + """ + pdf_path = os.path.join( + TEST_FILES_DIR, "MEDRPT-2024-PAT-3847_medical_report_scan.pdf" + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + + # Scanned PDFs without OCR have no text layer, so extraction should be empty + assert ( + result is not None + ), "Converter should return a result even for scanned PDFs" + assert result.text_content is not None, "text_content should not be None" + + # Verify extraction is empty (no text layer in scanned PDF) + assert ( + result.text_content.strip() == "" + ), f"Scanned PDF should have empty extraction, got: '{result.text_content[:100]}...'" + + def test_movie_theater_booking_pdf_extraction(self, markitdown): + """Test extraction of movie theater booking PDF with complex tables. + + Expected output: Pipe-separated format with booking details, agency info, + customer details, and show schedules in structured tables. + """ + pdf_path = os.path.join(TEST_FILES_DIR, "movie-theater-booking-2024.pdf") + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # Validate pipe-separated table format + assert "|" in text_content, "Booking order should contain pipe separators" + + # Validate key booking information + expected_strings = [ + "BOOKING ORDER", + "2024-12-5678", # Order number + "Holiday Movie Marathon Package", # Product description + "12/20/2024 - 12/31/2024", # Booking dates + "SC-WINTER-2024", # Alt order number + "STARLIGHT CINEMAS", # Cinema brand + ] + validate_strings(result, expected_strings) + + # Validate agency information + agency_strings = [ + "Premier Entertainment Group", # Agency name + "Michael Chen", # Contact + "Sarah Johnson", # Primary contact + "Downtown Multiplex", # Cinema name + ] + validate_strings(result, agency_strings) + + # Validate customer information + customer_strings = [ + "Universal Studios Distribution", # Customer name + "Film Distributor", # Category + "CUST-98765", # Customer ID + ] + validate_strings(result, customer_strings) + + # Validate booking summary totals + booking_strings = [ + "$12,500.00", # Gross amount + "$11,250.00", # Net amount + "December 2024", # Month + "48", # Number of shows + ] + validate_strings(result, booking_strings) + + # Validate show schedule details + show_strings = [ + "Holiday Spectacular", # Movie title + "Winter Wonderland", # Movie title + "New Year Mystery", # Movie title + "IMAX 3D", # Format + "$250", # Rate + "$300", # Rate + "$3,000", # Revenue + "$3,600", # Revenue + ] + validate_strings(result, show_strings) + + +class TestPdfFullOutputComparison: + """Test that PDF extraction produces expected complete outputs.""" + + @pytest.fixture + def markitdown(self): + """Create MarkItDown instance.""" + return MarkItDown() + + def test_movie_theater_full_output(self, markitdown): + """Test complete output for movie theater booking PDF.""" + pdf_path = os.path.join(TEST_FILES_DIR, "movie-theater-booking-2024.pdf") + expected_path = os.path.join( + TEST_FILES_DIR, "expected_outputs", "movie-theater-booking-2024.md" + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + if not os.path.exists(expected_path): + pytest.skip(f"Expected output not found: {expected_path}") + + result = markitdown.convert(pdf_path) + actual_output = result.text_content + + with open(expected_path, "r", encoding="utf-8") as f: + expected_output = f.read() + + # Compare outputs + actual_lines = [line.rstrip() for line in actual_output.split("\n")] + expected_lines = [line.rstrip() for line in expected_output.split("\n")] + + # Check line count + assert abs(len(actual_lines) - len(expected_lines)) <= 2, ( + f"Line count mismatch: actual={len(actual_lines)}, " + f"expected={len(expected_lines)}" + ) + + # Check structural elements + assert actual_output.count("|") > 80, "Should have many pipe separators" + assert actual_output.count("---") > 8, "Should have table separators" + + # Validate critical sections + for section in [ + "BOOKING ORDER", + "STARLIGHT CINEMAS", + "2024-12-5678", + "Holiday Spectacular", + "$12,500.00", + ]: + assert section in actual_output, f"Missing section: {section}" + + # Check table structure + table_rows = [line for line in actual_lines if line.startswith("|")] + assert ( + len(table_rows) > 15 + ), f"Should have >15 table rows, got {len(table_rows)}" + + def test_sparse_borderless_table_full_output(self, markitdown): + """Test complete output for SPARSE borderless table PDF.""" + pdf_path = os.path.join( + TEST_FILES_DIR, "SPARSE-2024-INV-1234_borderless_table.pdf" + ) + expected_path = os.path.join( + TEST_FILES_DIR, + "expected_outputs", + "SPARSE-2024-INV-1234_borderless_table.md", + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + if not os.path.exists(expected_path): + pytest.skip(f"Expected output not found: {expected_path}") + + result = markitdown.convert(pdf_path) + actual_output = result.text_content + + with open(expected_path, "r", encoding="utf-8") as f: + expected_output = f.read() + + # Compare outputs + actual_lines = [line.rstrip() for line in actual_output.split("\n")] + expected_lines = [line.rstrip() for line in expected_output.split("\n")] + + # Check line count is close + assert abs(len(actual_lines) - len(expected_lines)) <= 2, ( + f"Line count mismatch: actual={len(actual_lines)}, " + f"expected={len(expected_lines)}" + ) + + # Check structural elements + assert actual_output.count("|") > 50, "Should have many pipe separators" + + # Validate critical sections + for section in [ + "INVENTORY RECONCILIATION REPORT", + "SPARSE-2024-INV-1234", + "SKU-8847", + "SKU-9201", + "Variance Analysis", + ]: + assert section in actual_output, f"Missing section: {section}" + + def test_repair_multipage_full_output(self, markitdown): + """Test complete output for REPAIR multipage invoice PDF.""" + pdf_path = os.path.join(TEST_FILES_DIR, "REPAIR-2022-INV-001_multipage.pdf") + expected_path = os.path.join( + TEST_FILES_DIR, "expected_outputs", "REPAIR-2022-INV-001_multipage.md" + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + if not os.path.exists(expected_path): + pytest.skip(f"Expected output not found: {expected_path}") + + result = markitdown.convert(pdf_path) + actual_output = result.text_content + + with open(expected_path, "r", encoding="utf-8") as f: + expected_output = f.read() + + # Compare outputs + actual_lines = [line.rstrip() for line in actual_output.split("\n")] + expected_lines = [line.rstrip() for line in expected_output.split("\n")] + + # Check line count is close + assert abs(len(actual_lines) - len(expected_lines)) <= 2, ( + f"Line count mismatch: actual={len(actual_lines)}, " + f"expected={len(expected_lines)}" + ) + + # Check structural elements + assert actual_output.count("|") > 40, "Should have many pipe separators" + + # Validate critical sections + for section in [ + "ZAVA AUTO REPAIR", + "Gabriel Diaz", + "Jeep", + "Grand Cherokee", + "GRAND TOTAL", + ]: + assert section in actual_output, f"Missing section: {section}" + + def test_receipt_full_output(self, markitdown): + """Test complete output for RECEIPT retail purchase PDF.""" + pdf_path = os.path.join( + TEST_FILES_DIR, "RECEIPT-2024-TXN-98765_retail_purchase.pdf" + ) + expected_path = os.path.join( + TEST_FILES_DIR, + "expected_outputs", + "RECEIPT-2024-TXN-98765_retail_purchase.md", + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + if not os.path.exists(expected_path): + pytest.skip(f"Expected output not found: {expected_path}") + + result = markitdown.convert(pdf_path) + actual_output = result.text_content + + with open(expected_path, "r", encoding="utf-8") as f: + expected_output = f.read() + + # Compare outputs + actual_lines = [line.rstrip() for line in actual_output.split("\n")] + expected_lines = [line.rstrip() for line in expected_output.split("\n")] + + # Check line count is close + assert abs(len(actual_lines) - len(expected_lines)) <= 2, ( + f"Line count mismatch: actual={len(actual_lines)}, " + f"expected={len(expected_lines)}" + ) + + # Validate critical sections + for section in [ + "TECHMART ELECTRONICS", + "TXN-98765-2024", + "Sarah Mitchell", + "$821.14", + "RETURN POLICY", + ]: + assert section in actual_output, f"Missing section: {section}" + + def test_academic_paper_full_output(self, markitdown): + """Test complete output for academic paper PDF.""" + pdf_path = os.path.join(TEST_FILES_DIR, "test.pdf") + expected_path = os.path.join(TEST_FILES_DIR, "expected_outputs", "test.md") + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + if not os.path.exists(expected_path): + pytest.skip(f"Expected output not found: {expected_path}") + + result = markitdown.convert(pdf_path) + actual_output = result.text_content + + with open(expected_path, "r", encoding="utf-8") as f: + expected_output = f.read() + + # Compare outputs + actual_lines = [line.rstrip() for line in actual_output.split("\n")] + expected_lines = [line.rstrip() for line in expected_output.split("\n")] + + # Check line count is close + assert abs(len(actual_lines) - len(expected_lines)) <= 2, ( + f"Line count mismatch: actual={len(actual_lines)}, " + f"expected={len(expected_lines)}" + ) + + # Academic paper should not have pipe separators + assert ( + actual_output.count("|") == 0 + ), "Academic paper should not have pipe separators" + + # Validate critical sections + for section in [ + "Introduction", + "Large language models", + "agents", + "multi-agent", + ]: + assert section in actual_output, f"Missing section: {section}" + + def test_medical_scan_full_output(self, markitdown): + """Test complete output for medical report scan PDF (empty, no text layer).""" + pdf_path = os.path.join( + TEST_FILES_DIR, "MEDRPT-2024-PAT-3847_medical_report_scan.pdf" + ) + expected_path = os.path.join( + TEST_FILES_DIR, + "expected_outputs", + "MEDRPT-2024-PAT-3847_medical_report_scan.md", + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + if not os.path.exists(expected_path): + pytest.skip(f"Expected output not found: {expected_path}") + + result = markitdown.convert(pdf_path) + actual_output = result.text_content + + with open(expected_path, "r", encoding="utf-8") as f: + expected_output = f.read() + + # Both should be empty (scanned PDF with no text layer) + assert actual_output.strip() == "", "Scanned PDF should produce empty output" + assert ( + expected_output.strip() == "" + ), "Expected output should be empty for scanned PDF" + + +class TestPdfTableMarkdownFormat: + """Test that extracted tables have proper markdown formatting.""" + + @pytest.fixture + def markitdown(self): + """Create MarkItDown instance.""" + return MarkItDown() + + def test_markdown_table_has_pipe_format(self, markitdown): + """Test that form-style PDFs have pipe-separated format.""" + pdf_path = os.path.join( + TEST_FILES_DIR, "SPARSE-2024-INV-1234_borderless_table.pdf" + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # Find rows with pipes + lines = text_content.split("\n") + pipe_rows = [ + line for line in lines if line.startswith("|") and line.endswith("|") + ] + + assert len(pipe_rows) > 0, "Should have pipe-separated rows" + + # Check that Product Code appears in a pipe-separated row + product_code_found = any("Product Code" in row for row in pipe_rows) + assert product_code_found, "Product Code should be in pipe-separated format" + + def test_markdown_table_columns_have_pipes(self, markitdown): + """Test that form-style PDF columns are separated with pipes.""" + pdf_path = os.path.join( + TEST_FILES_DIR, "SPARSE-2024-INV-1234_borderless_table.pdf" + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # Find table rows and verify column structure + lines = text_content.split("\n") + table_rows = [ + line for line in lines if line.startswith("|") and line.endswith("|") + ] + + assert len(table_rows) > 0, "Should have markdown table rows" + + # Check that at least some rows have multiple columns (pipes) + multi_col_rows = [row for row in table_rows if row.count("|") >= 3] + assert ( + len(multi_col_rows) > 5 + ), f"Should have rows with multiple columns, found {len(multi_col_rows)}" + + +class TestPdfTableStructureConsistency: + """Test that extracted tables have consistent structure across all PDF types.""" + + @pytest.fixture + def markitdown(self): + """Create MarkItDown instance.""" + return MarkItDown() + + def test_borderless_table_structure(self, markitdown): + """Test that borderless table PDF has pipe-separated structure.""" + pdf_path = os.path.join( + TEST_FILES_DIR, "SPARSE-2024-INV-1234_borderless_table.pdf" + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # Should have pipe-separated content + assert "|" in text_content, "Borderless table PDF should have pipe separators" + + # Check that key content is present + assert "Product Code" in text_content, "Should contain Product Code" + assert "SKU-8847" in text_content, "Should contain first SKU" + assert "SKU-9201" in text_content, "Should contain second SKU" + + def test_multipage_invoice_table_structure(self, markitdown): + """Test that multipage invoice PDF has pipe-separated format.""" + pdf_path = os.path.join(TEST_FILES_DIR, "REPAIR-2022-INV-001_multipage.pdf") + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + text_content = result.text_content + + # Should have pipe-separated content + assert "|" in text_content, "Invoice PDF should have pipe separators" + + # Find rows with pipes + lines = text_content.split("\n") + pipe_rows = [ + line for line in lines if line.startswith("|") and line.endswith("|") + ] + + assert ( + len(pipe_rows) > 10 + ), f"Should have multiple pipe-separated rows, found {len(pipe_rows)}" + + # Check that some rows have multiple columns + multi_col_rows = [row for row in pipe_rows if row.count("|") >= 4] + assert len(multi_col_rows) > 5, "Should have rows with 3+ columns" + + def test_receipt_has_no_tables(self, markitdown): + """Test that receipt PDF doesn't incorrectly extract tables from formatted text.""" + pdf_path = os.path.join( + TEST_FILES_DIR, "RECEIPT-2024-TXN-98765_retail_purchase.pdf" + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + tables = extract_markdown_tables(result.text_content) + + # Receipt should not have markdown tables extracted + # (it's formatted text, not tabular data) + # If tables are extracted, they should be minimal/empty + total_table_rows = sum(len(t) for t in tables) + assert ( + total_table_rows < 5 + ), f"Receipt should not have significant tables, found {total_table_rows} rows" + + def test_scanned_pdf_no_tables(self, markitdown): + """Test that scanned PDF has empty extraction and no tables.""" + pdf_path = os.path.join( + TEST_FILES_DIR, "MEDRPT-2024-PAT-3847_medical_report_scan.pdf" + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + + # Scanned PDF with no text layer should have empty extraction + assert ( + result.text_content.strip() == "" + ), "Scanned PDF should have empty extraction" + + tables = extract_markdown_tables(result.text_content) + + # Scanned PDF with no text layer should have no tables + assert len(tables) == 0, "Scanned PDF should have no extracted tables" + + def test_all_pdfs_table_rows_consistent(self, markitdown): + """Test that all PDF tables have rows with pipe-separated content. + + Note: With gap-based column detection, rows may have different column counts + depending on how content is spaced in the PDF. What's important is that each + row has pipe separators and the content is readable. + """ + pdf_files = [ + "SPARSE-2024-INV-1234_borderless_table.pdf", + "REPAIR-2022-INV-001_multipage.pdf", + "RECEIPT-2024-TXN-98765_retail_purchase.pdf", + "test.pdf", + ] + + for pdf_file in pdf_files: + pdf_path = os.path.join(TEST_FILES_DIR, pdf_file) + if not os.path.exists(pdf_path): + continue + + result = markitdown.convert(pdf_path) + tables = extract_markdown_tables(result.text_content) + + for table_idx, table in enumerate(tables): + if not table: + continue + + # Verify each row has at least one column (pipe-separated content) + for row_idx, row in enumerate(table): + assert ( + len(row) >= 1 + ), f"{pdf_file}: Table {table_idx}, row {row_idx} has no columns" + + # Verify the row has non-empty content + row_content = " ".join(cell.strip() for cell in row) + assert ( + len(row_content.strip()) > 0 + ), f"{pdf_file}: Table {table_idx}, row {row_idx} is empty" + + def test_borderless_table_data_integrity(self, markitdown): + """Test that borderless table extraction preserves data integrity.""" + pdf_path = os.path.join( + TEST_FILES_DIR, "SPARSE-2024-INV-1234_borderless_table.pdf" + ) + + if not os.path.exists(pdf_path): + pytest.skip(f"Test file not found: {pdf_path}") + + result = markitdown.convert(pdf_path) + tables = extract_markdown_tables(result.text_content) + + assert len(tables) >= 2, "Should have at least 2 tables" + + # Check first table has expected SKU data + first_table = tables[0] + table_text = str(first_table) + assert "SKU-8847" in table_text, "First table should contain SKU-8847" + assert "SKU-9201" in table_text, "First table should contain SKU-9201" + + # Check second table has expected category data + second_table = tables[1] + table_text = str(second_table) + assert "Electronics" in table_text, "Second table should contain Electronics" + assert "Hardware" in table_text, "Second table should contain Hardware"