Skip to content

Commit

Permalink
combine xlsx and xls to excel, replace openpxyl/xlrd with calamine
Browse files Browse the repository at this point in the history
  • Loading branch information
yeungadrian committed Jan 3, 2025
1 parent 731b39e commit b953121
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 36 deletions.
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ dependencies = [
"numpy",
"python-pptx",
"pandas",
"openpyxl",
"xlrd",
"python-calamine",
"pdfminer.six",
"puremagic",
"pydub",
Expand Down
37 changes: 6 additions & 31 deletions src/markitdown/_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,42 +715,18 @@ def convert(self, local_path, **kwargs) -> Union[None, DocumentConverterResult]:
return result


class XlsxConverter(HtmlConverter):
class ExcelConverter(HtmlConverter):
"""
Converts XLSX files to Markdown, with each sheet presented as a separate Markdown table.
Converts Excel (XLSX, XLS, XLSM or XLSB) files to Markdown, with each sheet presented as a separate Markdown table.
"""

def convert(self, local_path, **kwargs) -> Union[None, DocumentConverterResult]:
# Bail if not a XLSX
# Bail if not a XLSX, XLS, XLSM or XLSB
extension = kwargs.get("file_extension", "")
if extension.lower() != ".xlsx":
if extension.lower() not in [".xlsx", ".xls", ".xlsb", ".xlsm"]:
return None

sheets = pd.read_excel(local_path, sheet_name=None, engine="openpyxl")
md_content = ""
for s in sheets:
md_content += f"## {s}\n"
html_content = sheets[s].to_html(index=False)
md_content += self._convert(html_content).text_content.strip() + "\n\n"

return DocumentConverterResult(
title=None,
text_content=md_content.strip(),
)


class XlsConverter(HtmlConverter):
"""
Converts XLS files to Markdown, with each sheet presented as a separate Markdown table.
"""

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

sheets = pd.read_excel(local_path, sheet_name=None, engine="xlrd")
sheets = pd.read_excel(local_path, sheet_name=None, engine="calamine")
md_content = ""
for s in sheets:
md_content += f"## {s}\n"
Expand Down Expand Up @@ -1376,8 +1352,7 @@ def __init__(
self.register_page_converter(YouTubeConverter())
self.register_page_converter(BingSerpConverter())
self.register_page_converter(DocxConverter())
self.register_page_converter(XlsxConverter())
self.register_page_converter(XlsConverter())
self.register_page_converter(ExcelConverter())
self.register_page_converter(PptxConverter())
self.register_page_converter(WavConverter())
self.register_page_converter(Mp3Converter())
Expand Down
Binary file added tests/test_files/test.xlsb
Binary file not shown.
Binary file added tests/test_files/test.xlsm
Binary file not shown.
12 changes: 9 additions & 3 deletions tests/test_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,17 @@ def test_markitdown_local() -> None:
result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.xlsx"))
validate_strings(result, XLSX_TEST_STRINGS)

# Test XLSB processing
result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.xlsb"))
validate_strings(result, XLSX_TEST_STRINGS)

# Test XLSM processing
result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.xlsm"))
validate_strings(result, XLSX_TEST_STRINGS)

# Test XLS processing
result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.xls"))
for test_string in XLS_TEST_STRINGS:
text_content = result.text_content.replace("\\", "")
assert test_string in text_content
validate_strings(result, XLSX_TEST_STRINGS)

# Test DOCX processing
result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.docx"))
Expand Down

0 comments on commit b953121

Please sign in to comment.