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 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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 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
21 changes: 15 additions & 6 deletions src/markitdown/_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,27 @@ 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.
"""

def convert(self, local_path, **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, pdf_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),
)
pdf_engine = kwargs.get("pdf_engine", "pdfminer")
if pdf_engine == "pdfminer":
text_content = pdfminer.high_level.extract_text(local_path)
elif pdf_engine == "pymupdf4llm":
text_content = pymupdf4llm.to_markdown(local_path, show_progress=False)
else:
return None # unknown method
Copy link
Contributor

Choose a reason for hiding this comment

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

This part doesn't return anything. Could you update it? Maybe add a warning message?

return DocumentConverterResult(title=None, text_content=text_content)


class DocxConverter(HtmlConverter):
Expand Down