Skip to content
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
2 changes: 1 addition & 1 deletion packages/markitdown/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pptx = ["python-pptx"]
docx = ["mammoth", "lxml"]
xlsx = ["pandas", "openpyxl"]
xls = ["pandas", "xlrd"]
pdf = ["pdfminer.six"]
pdf = ["pdfminer.six", "pytesseract", "pdf2image"]
outlook = ["olefile"]
audio-transcription = ["pydub", "SpeechRecognition"]
youtube-transcription = ["youtube-transcript-api"]
Expand Down
40 changes: 29 additions & 11 deletions packages/markitdown/src/markitdown/converters/_pdf_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
# Preserve the error and stack trace for later
_dependency_exc_info = sys.exc_info()

# Try to import pytesseract and pdf2image for OCR
_ocr_dependency_exc_info = None
try:
import pytesseract
from pdf2image import convert_from_bytes
except ImportError:
_ocr_dependency_exc_info = sys.exc_info()

ACCEPTED_MIME_TYPE_PREFIXES = [
"application/pdf",
Expand Down Expand Up @@ -57,21 +64,32 @@ def convert(
stream_info: StreamInfo,
**kwargs: Any, # Options to pass to the converter
) -> DocumentConverterResult:
# Check the dependencies
# Check dependencies
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( # type: ignore[union-attr]
_dependency_exc_info[2]
)

assert isinstance(file_stream, io.IOBase) # for mypy
return DocumentConverterResult(
markdown=pdfminer.high_level.extract_text(file_stream),
)
) from _dependency_exc_info[1].with_traceback(_dependency_exc_info[2])

# Try to extract text with pdfminer
file_stream.seek(0)
text = pdfminer.high_level.extract_text(file_stream)
if text and text.strip():
return DocumentConverterResult(markdown=text)

# If no text found, fall back to OCR
if _ocr_dependency_exc_info is not None:
raise MissingDependencyException(
"OCR dependencies are missing. Please install pytesseract and pdf2image for OCR support."
) from _ocr_dependency_exc_info[1].with_traceback(_ocr_dependency_exc_info[2])

file_stream.seek(0)
images = convert_from_bytes(file_stream.read())
ocr_text = []
for img in images:
ocr_text.append(pytesseract.image_to_string(img))
ocr_output = "\n\n".join(ocr_text)
return DocumentConverterResult(markdown=ocr_output)
Comment on lines +78 to +95
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is an issue of PDFs containing both mineable text and images that contain text. It would be nice to have a more sophisticated branching mechanism that accounts for this and/or allowing an API to override by the markitdown caller.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

related discussion #1285 (comment)