Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: change pdf text parser to pymupdf4llm #139

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ coverage.xml
.hypothesis/
.pytest_cache/
cover/
tests/out

# Translations
*.mo
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies = [
"pandas",
"openpyxl",
"pdfminer.six",
"pymupdf4llm",
"puremagic",
"pydub",
"youtube-transcript-api",
Expand Down
13 changes: 12 additions & 1 deletion src/markitdown/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,26 @@ def main():
"--output",
help="Output file name. If not provided, output is written to stdout.",
)
# adding CLI option for extra parameters for PdfConverter
parser.add_argument(
"-e",
"--engine",
help="Engine name for converters. If not provided will use default.",
)

args = parser.parse_args()

kwargs = {}
if args.engine:
kwargs.update({"engine": args.engine})

if args.filename is None:
markitdown = MarkItDown()
result = markitdown.convert_stream(sys.stdin.buffer)
_handle_output(args, result)
else:
markitdown = MarkItDown()
result = markitdown.convert(args.filename)
result = markitdown.convert(args.filename, **kwargs)
_handle_output(args, result)


Expand Down
36 changes: 28 additions & 8 deletions src/markitdown/_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import traceback
import zipfile
from xml.dom import minidom
from typing import Any, Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Union, Literal, Mapping
from pathlib import Path
from urllib.parse import parse_qs, quote, unquote, urlparse, urlunparse
from warnings import warn, resetwarnings, catch_warnings
Expand All @@ -24,6 +24,7 @@
import pandas as pd
import pdfminer
import pdfminer.high_level
import pymupdf4llm
import pptx

# File-format detection
Expand Down Expand Up @@ -676,19 +677,38 @@ def convert(self, local_path, **kwargs) -> Union[None, DocumentConverterResult]:

class PdfConverter(DocumentConverter):
"""
Converts PDFs to Markdown. Most style information is ignored, so the results are essentially plain-text.
Converts PDFs to Markdown. Most style information is ignored, so the results are essentially plain-text.
"""
_engines: Mapping[str, Any] = {
"pdfminer": pdfminer.high_level.extract_text,
"pymupdf4llm": pymupdf4llm.to_markdown,
}

def convert(self, local_path, **kwargs) -> Union[None, DocumentConverterResult]:
def convert(
self,
local_path,
engine: Literal["pdfminer", "pymupdf4llm"] = "pdfminer",
engine_kwargs={},
**kwargs,
) -> Union[None, DocumentConverterResult]:
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think pdf_engine should be a named parameter in the function instead of being accessed via kwargs. This provides more clarity and ensures better handling of default values

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for you suggestion. I have added it to a named parameters ( I was meant to align with other converters' parameter definition), and added the exception case when pdf_engine is not valid. The new test cases could be seen on my new commit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is optional, but adding one more named parameter could make this more customizable, like:

def convert(self, local_path, engine: Literal['pdfminer', 'pymupdf4llm']='pdfminer', engine_kwargs=None, **kwargs) -> Union[None, DocumentConverterResult]:

Example:
>>> source = "https://arxiv.org/pdf/2308.08155v2.pdf"
>>> markitdown.convert(source, engine="pymupdf4llm")
"""
# Bail if not a PDF
extension = kwargs.get("file_extension", "")
if extension.lower() != ".pdf":
return None

return DocumentConverterResult(
title=None,
text_content=pdfminer.high_level.extract_text(local_path),
)
if engine is not None and engine not in self._engines:
raise FileConversionException(
"'engine' not valid for {} files. Please choose between {}.".format(
extension, list(self._engines.keys())
)
)
else:
text_content = self._engines[engine](local_path, **engine_kwargs)
return DocumentConverterResult(title=None, text_content=text_content)


class DocxConverter(HtmlConverter):
Expand Down
Binary file added tests/test_files/2308.08155v2.pdf
Binary file not shown.
38 changes: 37 additions & 1 deletion tests/test_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import requests

from warnings import catch_warnings, resetwarnings

from markitdown import MarkItDown

skip_remote = (
Expand Down Expand Up @@ -299,6 +298,42 @@ def test_markitdown_llm() -> None:
for test_string in ["red", "circle", "blue", "square"]:
assert test_string in result.text_content.lower()

def test_markitdown_pdf() -> None:
markitdown = MarkItDown()

# I test by local pdf, using PDF_TEST_URL may also be fine.

# By pymupdf4llm
result = markitdown.convert(
os.path.join(TEST_FILES_DIR, "2308.08155v2.pdf"),
engine="pymupdf4llm",

engine_kwargs={"show_progress": False, "pages": range(10),}, # additional kwargs
)
for test_string in PDF_TEST_STRINGS:
assert test_string in result.text_content

# By pymupdf4llm and extract images
result = markitdown.convert(
os.path.join(TEST_FILES_DIR, "2308.08155v2.pdf"),
engine="pymupdf4llm",
engine_kwargs={
"show_progress": False,
"write_images": True,
"image_path": "tests/out",
"pages": range(10),
}, # `write_images` must be True, setting `image_path` for images saving dir.
)
for test_string in PDF_TEST_STRINGS:
assert test_string in result.text_content

# By pdfminer
result = markitdown.convert(
os.path.join(TEST_FILES_DIR, "2308.08155v2.pdf"), engine="pdfminer",
enging_kwargs={"page_numbers": range(10),}
)
for test_string in PDF_TEST_STRINGS:
assert test_string in result.text_content

if __name__ == "__main__":
"""Runs this file's tests from the command line."""
Expand All @@ -307,3 +342,4 @@ def test_markitdown_llm() -> None:
test_markitdown_exiftool()
test_markitdown_deprecation()
test_markitdown_llm()
test_markitdown_pdf()