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

Add OneNote support #55

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ It presently supports:
- Audio (EXIF metadata, and speech transcription)
- HTML (special handling of Wikipedia, etc.)
- Various other text-based formats (csv, json, xml, etc.)
- OneNote (.one)

Note: OneNote is not supported.
Copy link
Contributor

Choose a reason for hiding this comment

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

is this a typo? the comment doesn't seem consistent?


# Installation

Expand Down Expand Up @@ -51,6 +54,18 @@ result = md.convert("example.jpg")
print(result.text_content)
```

To convert OneNote files, you can use the following example:
Copy link
Member

Choose a reason for hiding this comment

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

This example is redundant, there’s one already for converting files.


```python
from markitdown import MarkItDown

markitdown = MarkItDown()
result = markitdown.convert("example.one")
print(result.text_content)
```

Note: For OneNote support, the `one-extract` package is used.

## Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies = [
"youtube-transcript-api",
"SpeechRecognition",
"pathvalidate",
"onenote",
Copy link
Member

@lalo lalo Dec 20, 2024

Choose a reason for hiding this comment

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

is this the correct package name? could not find it in pip registry, but found this one instead https://pypi.org/project/one-extract/ can you link which is the appropriate one?

]

[project.urls]
Expand Down Expand Up @@ -76,3 +77,5 @@ exclude_lines = [
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
]

# Note: OneNote is not supported.
28 changes: 28 additions & 0 deletions src/markitdown/_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import pdfminer
import pdfminer.high_level
import pptx
import one_extract as onenote

# File-format detection
import puremagic
Expand Down Expand Up @@ -617,6 +618,32 @@ def _is_table(self, shape):
return False


class OneNoteConverter(HtmlConverter):
"""
Converts OneNote files to Markdown. Supports heading, tables and images with alt text.
"""

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

md_content = ""

notebook = onenote.Notebook(local_path)
for section in notebook.sections:
md_content += f"\n\n# {section.name}\n"
for page in section.pages:
md_content += f"\n\n## {page.name}\n"
md_content += self._convert(page.content).text_content.strip() + "\n\n"

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


class MediaConverter(DocumentConverter):
"""
Abstract class for multi-modal media (e.g., images and audio)
Expand Down Expand Up @@ -880,6 +907,7 @@ def __init__(
self.register_page_converter(Mp3Converter())
self.register_page_converter(ImageConverter())
self.register_page_converter(PdfConverter())
self.register_page_converter(OneNoteConverter())

def convert(
self, source: Union[str, requests.Response], **kwargs: Any
Expand Down
17 changes: 16 additions & 1 deletion tests/test_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@
"data:image/svg+xml,%3Csvg%20width%3D",
]

ONENOTE_TEST_STRINGS = [
"# Section 1",
"## Page 1",
"This is a test OneNote page.",
"# Section 2",
"## Page 2",
"Another test OneNote page.",
]


@pytest.mark.skipif(
skip_remote,
Expand Down Expand Up @@ -164,6 +173,12 @@ def test_markitdown_local() -> None:
for test_string in SERP_TEST_STRINGS:
assert test_string in text_content

# Test OneNote processing
result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.one"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you forget to add the test file, test.one?

text_content = result.text_content.replace("\\", "")
for test_string in ONENOTE_TEST_STRINGS:
assert test_string in text_content


@pytest.mark.skipif(
skip_exiftool,
Expand All @@ -179,7 +194,7 @@ def test_markitdown_exiftool() -> None:
assert target in result.text_content


if __name__ == "__main__":
if __name__main__":
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like there's a small typo in the if __name__main__": line

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for your checking. I have already fix it.

"""Runs this file's tests from the command line."""
test_markitdown_remote()
test_markitdown_local()
Expand Down