-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
tungsten106
wants to merge
11
commits into
microsoft:main
Choose a base branch
from
tungsten106:dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+67
−9
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b3f7e00
update: change pdf text parser to pymupdf4llm
tungsten106 df5f14e
Merge remote-tracking branch 'origin/main' into dev
tungsten106 263e0b5
update: add parameter "method" for PdfConverter
tungsten106 d4d11a8
Merge branch 'main' into dev
tungsten106 797e0d4
Merge branch 'dev' of https://github.com/tungsten106/markitdown into dev
tungsten106 ba5df9b
update: changed "method" parameter fro PdfConverter to "pdf_engine" f…
tungsten106 e808548
update: adding named parameter `pdf_engine` to .conver(); adding tes…
tungsten106 565ef05
update: Add`engine_kwargs` for customize parameters. Update PdfConver…
tungsten106 bd95fb0
update: adding `tests/out` folder to save test files
tungsten106 8482477
update: use smaller test-pdf size
tungsten106 f07ea3e
bugfix: fixing test_markitdown.py; updating exception messages
tungsten106 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
import pandas as pd | ||
import pdfminer | ||
import pdfminer.high_level | ||
import pymupdf4llm | ||
import pptx | ||
|
||
# File-format detection | ||
|
@@ -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]: | ||
""" | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 viakwargs
. This provides more clarity and ensures better handling of default valuesThere was a problem hiding this comment.
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.There was a problem hiding this comment.
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: