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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ At the moment, the following optional dependencies are available:
* `[xls]` Installs dependencies for older Excel files
* `[pdf]` Installs dependencies for PDF files
* `[outlook]` Installs dependencies for Outlook messages
* `[hwp]` Installs dependencies for HWP and HWPX files
* `[az-doc-intel]` Installs dependencies for Azure Document Intelligence
* `[az-content-understanding]` Installs dependencies for Azure Content Understanding
* `[audio-transcription]` Installs dependencies for audio transcription of wav and mp3 files
Expand Down
2 changes: 2 additions & 0 deletions packages/markitdown/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ all = [
"azure-ai-documentintelligence",
"azure-ai-contentunderstanding>=1.2.0b1",
"azure-identity",
"rhwp-python>=0.8.1",
]
pptx = ["python-pptx"]
docx = ["mammoth~=1.11.0", "lxml"]
xlsx = ["pandas", "openpyxl"]
xls = ["pandas", "xlrd"]
pdf = ["pdfminer.six>=20251230", "pdfplumber>=0.11.9"]
outlook = ["olefile"]
hwp = ["rhwp-python>=0.8.1"]
audio-transcription = ["pydub", "SpeechRecognition"]
youtube-transcription = ["youtube-transcript-api~=1.2.3"]
az-doc-intel = ["azure-ai-documentintelligence", "azure-identity"]
Expand Down
2 changes: 2 additions & 0 deletions packages/markitdown/src/markitdown/_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
DocumentIntelligenceConverter,
ContentUnderstandingConverter,
CsvConverter,
HwpConverter,
)

from ._base_converter import DocumentConverter, DocumentConverterResult
Expand Down Expand Up @@ -250,6 +251,7 @@ def enable_builtins(self, **kwargs) -> None:
self.register_converter(OutlookMsgConverter())
self.register_converter(EpubConverter())
self.register_converter(CsvConverter())
self.register_converter(HwpConverter())

# Register Document Intelligence converter at the top of the stack if endpoint is provided
docintel_endpoint = kwargs.get("docintel_endpoint")
Expand Down
2 changes: 2 additions & 0 deletions packages/markitdown/src/markitdown/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
)
from ._epub_converter import EpubConverter
from ._csv_converter import CsvConverter
from ._hwp_converter import HwpConverter

__all__ = [
"PlainTextConverter",
Expand All @@ -51,4 +52,5 @@
"ContentUnderstandingFileType",
"EpubConverter",
"CsvConverter",
"HwpConverter",
]
83 changes: 83 additions & 0 deletions packages/markitdown/src/markitdown/converters/_hwp_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os
from pathlib import Path
import tempfile
from typing import Any, BinaryIO

from .._base_converter import DocumentConverter, DocumentConverterResult
from .._exceptions import MissingDependencyException, MISSING_DEPENDENCY_MESSAGE
from .._stream_info import StreamInfo

_dependency_exc_info = None
rhwp: Any = None
try:
import rhwp
except ImportError:
import sys

_dependency_exc_info = sys.exc_info()

ACCEPTED_MIME_TYPE_PREFIXES = [
"application/x-hwp",
"application/vnd.hancom.hwp",
"application/x-hwp-v5",
"application/haansofthwp",
"application/vnd.hancom.hwpx",
"application/hwpx",
"application/x-hwpx",
]

ACCEPTED_FILE_EXTENSIONS = [".hwp", ".hwpx"]


class HwpConverter(DocumentConverter):
"""Converts HWP and HWPX files to Markdown with rhwp-python."""

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 in ACCEPTED_FILE_EXTENSIONS:
return True

return any(
mimetype.startswith(prefix) for prefix in ACCEPTED_MIME_TYPE_PREFIXES
)

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=".hwp or .hwpx",
feature="hwp",
)
) from _dependency_exc_info[1].with_traceback( # type: ignore[union-attr]
_dependency_exc_info[2]
)

extension = (stream_info.extension or "").lower()
if extension not in ACCEPTED_FILE_EXTENSIONS:
extension = ".hwp"

with tempfile.TemporaryDirectory(prefix="markitdown-rhwp-") as temp_dir:
temp_path = Path(temp_dir)
input_path = temp_path / f"input{extension}"

current_position = file_stream.tell()
try:
input_path.write_bytes(file_stream.read())
finally:
file_stream.seek(current_position)

document = rhwp.parse(os.fspath(input_path))
return DocumentConverterResult(markdown=document.to_ir().to_markdown())
Loading