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

feat: add DOC converter to MarkItDown #153

Closed
wants to merge 3 commits into from
Closed
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
48 changes: 48 additions & 0 deletions src/markitdown/_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import Any, Dict, List, Optional, Union
from urllib.parse import parse_qs, quote, unquote, urlparse, urlunparse
from warnings import warn, resetwarnings, catch_warnings
from pathlib import Path

import mammoth
import markdownify
Expand Down Expand Up @@ -712,6 +713,52 @@ def convert(self, local_path, **kwargs) -> Union[None, DocumentConverterResult]:
return result


class DocConverter(HtmlConverter):
"""
Converts DOC files to Markdown.
"""

def convert(self, local_path, **kwargs) -> Union[None, DocumentConverterResult]:
# Bail if not a DOC
extension = kwargs.get("file_extension", "")
if extension.lower() != ".doc":
return None

if not (soffice := shutil.which("soffice")):
return DocumentConverterResult(
title=None,
text_content="[ERROR] LibreOffice not found. Please install LibreOffice and try again.",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
text_content="[ERROR] LibreOffice not found. Please install LibreOffice and try again.",
text_content="[ERROR] This feature requires the 'soffice' command from LibreOffice. Please install LibreOffice. https://www.libreoffice.org/get-help/install-howto/.",

)

local_path = Path(local_path)
outdir = local_path.parent
result = None

libreoffice_command = [
gagb marked this conversation as resolved.
Show resolved Hide resolved
soffice,
"--headless",
"--convert-to",
"txt:Text",
"--outdir",
outdir,
local_path,
]
try:
result = subprocess.run(libreoffice_command, capture_output=True, text=True)
output_file = outdir / (local_path.stem + ".txt")
with open(output_file, "r", encoding="utf-8") as f:
output_content = f.read()
output_file.unlink(missing_ok=True)
result = self._convert(output_content)
return result

except Exception as e:
return DocumentConverterResult(
title=None,
text_content=f"[ERROR] Failed to process DOC file {local_path}: {str(e)}",
)


class XlsxConverter(HtmlConverter):
"""
Converts XLSX files to Markdown, with each sheet presented as a separate Markdown table.
Expand Down Expand Up @@ -1276,6 +1323,7 @@ def __init__(
self.register_page_converter(YouTubeConverter())
self.register_page_converter(BingSerpConverter())
self.register_page_converter(DocxConverter())
self.register_page_converter(DocConverter())
self.register_page_converter(XlsxConverter())
self.register_page_converter(PptxConverter())
self.register_page_converter(WavConverter())
Expand Down
Binary file added tests/test_files/test.doc
Binary file not shown.
34 changes: 34 additions & 0 deletions tests/test_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
# Skip exiftool tests if not installed
skip_exiftool = shutil.which("exiftool") is None

# Skip soffice tests if not installed
skip_soffice = shutil.which("soffice") is None

TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), "test_files")

JPG_TEST_EXIFTOOL = {
Expand Down Expand Up @@ -74,6 +77,15 @@
"Yet another comment in the doc. 55yiyi-asd09",
]

DOC_TEST_STRINGS = [
"314b0a30-5b04-470b-b9f7-eed2c2bec74a",
"49e168b7-d2ae-407f-a055-2167576f39a1",
"d666f1f7-46cb-42bd-9a39-9a39cf2a509f",
"Abstract",
"Introduction",
"AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation",
]

PPTX_TEST_STRINGS = [
"2cdda5c8-e50e-4db4-b5f0-9722a649f455",
"04191ea8-5c73-4215-a1d3-1cfb43aaaf12",
Expand Down Expand Up @@ -257,6 +269,28 @@ def test_markitdown_exiftool() -> None:
target = f"{key}: {JPG_TEST_EXIFTOOL[key]}"
assert target in result.text_content

@pytest.mark.skipif(
skip_soffice,
reason="do not run if soffice is not installed",
)
def test_markitdown_soffice() -> None:
markitdown = MarkItDown()

# Test DOC processing
result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.doc"))
for test_string in DOC_TEST_STRINGS:
text_content = result.text_content.replace("\\", "")
assert test_string in text_content

def test_markitdown_soffice_mocked(mocker) -> None:
mocker.patch("shutil.which", return_value=None)
markitdown = MarkItDown()

# Test DOC processing
result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.doc"))
text_content = result.text_content.replace("\\", "")
assert text_content == "[ERROR] LibreOffice not found. Please install LibreOffice and try again."


def test_markitdown_deprecation() -> None:
try:
Expand Down