tomd-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tomd/__init__.py +5 -0
- tomd/cli.py +55 -0
- tomd/converter.py +76 -0
- tomd_cli-0.1.0.dist-info/METADATA +78 -0
- tomd_cli-0.1.0.dist-info/RECORD +8 -0
- tomd_cli-0.1.0.dist-info/WHEEL +4 -0
- tomd_cli-0.1.0.dist-info/entry_points.txt +2 -0
- tomd_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
tomd/__init__.py
ADDED
tomd/cli.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Command-line interface for tomd."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
from tomd.converter import SUPPORTED_EXTENSIONS, convert_dir, convert_file
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _build_parser() -> argparse.ArgumentParser:
|
|
12
|
+
parser = argparse.ArgumentParser(
|
|
13
|
+
prog="tomd",
|
|
14
|
+
description="Convert Office files to Markdown using MarkItDown.",
|
|
15
|
+
)
|
|
16
|
+
parser.add_argument(
|
|
17
|
+
"input",
|
|
18
|
+
help="file or directory to convert",
|
|
19
|
+
)
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
"-o", "--output",
|
|
22
|
+
help="output file or directory path",
|
|
23
|
+
)
|
|
24
|
+
parser.add_argument(
|
|
25
|
+
"--dir",
|
|
26
|
+
action="store_true",
|
|
27
|
+
dest="is_dir",
|
|
28
|
+
help="treat input as a directory and convert all supported files",
|
|
29
|
+
)
|
|
30
|
+
fmt = ", ".join(sorted(SUPPORTED_EXTENSIONS))
|
|
31
|
+
parser.epilog = f"Supported formats: {fmt}"
|
|
32
|
+
return parser
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def main(argv: list[str] | None = None) -> None:
|
|
36
|
+
parser = _build_parser()
|
|
37
|
+
args = parser.parse_args(argv)
|
|
38
|
+
|
|
39
|
+
try:
|
|
40
|
+
if args.is_dir:
|
|
41
|
+
results = convert_dir(args.input, args.output)
|
|
42
|
+
if not results:
|
|
43
|
+
print(f"No convertible files found in {args.input}")
|
|
44
|
+
else:
|
|
45
|
+
for dest in results:
|
|
46
|
+
print(f"Converted: {dest}")
|
|
47
|
+
else:
|
|
48
|
+
dest = convert_file(args.input, args.output)
|
|
49
|
+
print(f"Converted: {dest}")
|
|
50
|
+
except (FileNotFoundError, NotADirectoryError) as e:
|
|
51
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
52
|
+
sys.exit(1)
|
|
53
|
+
except Exception as e:
|
|
54
|
+
print(f"Conversion failed: {e}", file=sys.stderr)
|
|
55
|
+
sys.exit(1)
|
tomd/converter.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Core conversion logic."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from markitdown import MarkItDown
|
|
9
|
+
|
|
10
|
+
SUPPORTED_EXTENSIONS = {".pptx", ".docx", ".xlsx", ".pdf"}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def strip_base64_images(text: str) -> str:
|
|
14
|
+
"""Replace embedded base64 image data with a placeholder."""
|
|
15
|
+
return re.sub(
|
|
16
|
+
r'!\[([^\]]*)\]\(data:image/[^)]+\)',
|
|
17
|
+
lambda m: f'![{m.group(1)}]()',
|
|
18
|
+
text,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def convert_file(
|
|
23
|
+
input_path: str | Path,
|
|
24
|
+
output_path: str | Path | None = None,
|
|
25
|
+
) -> Path:
|
|
26
|
+
"""Convert a single file to Markdown.
|
|
27
|
+
|
|
28
|
+
Returns the path to the generated Markdown file.
|
|
29
|
+
Raises FileNotFoundError if input does not exist.
|
|
30
|
+
"""
|
|
31
|
+
src = Path(input_path).resolve()
|
|
32
|
+
if not src.is_file():
|
|
33
|
+
raise FileNotFoundError(f"File not found: {input_path}")
|
|
34
|
+
|
|
35
|
+
dest = Path(output_path).resolve() if output_path else src.with_suffix(".md")
|
|
36
|
+
|
|
37
|
+
md = MarkItDown()
|
|
38
|
+
result = md.convert(str(src))
|
|
39
|
+
content = strip_base64_images(result.text_content)
|
|
40
|
+
|
|
41
|
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
42
|
+
dest.write_text(content, encoding="utf-8")
|
|
43
|
+
return dest
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def convert_dir(
|
|
47
|
+
input_dir: str | Path,
|
|
48
|
+
output_dir: str | Path | None = None,
|
|
49
|
+
) -> list[Path]:
|
|
50
|
+
"""Convert all supported files in a directory to Markdown.
|
|
51
|
+
|
|
52
|
+
Returns a list of generated Markdown file paths.
|
|
53
|
+
Raises NotADirectoryError if input is not a directory.
|
|
54
|
+
"""
|
|
55
|
+
src_dir = Path(input_dir).resolve()
|
|
56
|
+
if not src_dir.is_dir():
|
|
57
|
+
raise NotADirectoryError(f"Directory not found: {input_dir}")
|
|
58
|
+
|
|
59
|
+
dest_dir = Path(output_dir).resolve() if output_dir else src_dir / "output"
|
|
60
|
+
dest_dir.mkdir(parents=True, exist_ok=True)
|
|
61
|
+
|
|
62
|
+
files = sorted(
|
|
63
|
+
f for f in src_dir.iterdir() if f.suffix.lower() in SUPPORTED_EXTENSIONS
|
|
64
|
+
)
|
|
65
|
+
if not files:
|
|
66
|
+
return []
|
|
67
|
+
|
|
68
|
+
md = MarkItDown()
|
|
69
|
+
results: list[Path] = []
|
|
70
|
+
for f in files:
|
|
71
|
+
dest = dest_dir / f.with_suffix(".md").name
|
|
72
|
+
result = md.convert(str(f))
|
|
73
|
+
dest.write_text(strip_base64_images(result.text_content), encoding="utf-8")
|
|
74
|
+
results.append(dest)
|
|
75
|
+
|
|
76
|
+
return results
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tomd-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Convert Office files (.docx, .pptx, .xlsx, .pdf) to Markdown using MarkItDown
|
|
5
|
+
Project-URL: Homepage, https://github.com/nakamurakohki/tomd-cli
|
|
6
|
+
Project-URL: Repository, https://github.com/nakamurakohki/tomd-cli
|
|
7
|
+
Project-URL: Issues, https://github.com/nakamurakohki/tomd-cli/issues
|
|
8
|
+
Author: Kohki Nakamura
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: converter,docx,markdown,pdf,pptx,xlsx
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: MacOS
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Topic :: Text Processing :: Markup :: Markdown
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Requires-Dist: markitdown>=0.0.2
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# tomd-cli
|
|
28
|
+
|
|
29
|
+
Convert Office files to Markdown using [MarkItDown](https://github.com/microsoft/markitdown).
|
|
30
|
+
|
|
31
|
+
## Supported formats
|
|
32
|
+
|
|
33
|
+
- `.docx` (Word)
|
|
34
|
+
- `.pptx` (PowerPoint)
|
|
35
|
+
- `.xlsx` (Excel)
|
|
36
|
+
- `.pdf`
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install tomd-cli
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
### CLI
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
# Convert a single file
|
|
50
|
+
tomd document.docx
|
|
51
|
+
|
|
52
|
+
# Specify output path
|
|
53
|
+
tomd document.docx -o output.md
|
|
54
|
+
|
|
55
|
+
# Convert all files in a directory
|
|
56
|
+
tomd --dir ./documents/
|
|
57
|
+
|
|
58
|
+
# Convert directory with custom output
|
|
59
|
+
tomd --dir ./documents/ -o ./markdown/
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Python API
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from tomd import convert_file, convert_dir
|
|
66
|
+
|
|
67
|
+
# Single file
|
|
68
|
+
output_path = convert_file("document.docx")
|
|
69
|
+
output_path = convert_file("document.docx", "output.md")
|
|
70
|
+
|
|
71
|
+
# Directory
|
|
72
|
+
output_paths = convert_dir("./documents/")
|
|
73
|
+
output_paths = convert_dir("./documents/", "./markdown/")
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
MIT
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
tomd/__init__.py,sha256=Ac-PrtkEuP0iDn0JOW0p2hGoWkP-3Y2rouI18_zo7r4,208
|
|
2
|
+
tomd/cli.py,sha256=tuBtF9xATgmb2yaL8ltV1Qg1KKDtH-kC4Qv7jWB_I9U,1602
|
|
3
|
+
tomd/converter.py,sha256=E1k8H5Ts04BkZSW9d-Lu5Mn8WbnPYgXgUDw3gpaiwL8,2123
|
|
4
|
+
tomd_cli-0.1.0.dist-info/METADATA,sha256=0wgheRF0k117esqNuLuMRzHQpoIbhzf2re5iWGVvV2E,1916
|
|
5
|
+
tomd_cli-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
6
|
+
tomd_cli-0.1.0.dist-info/entry_points.txt,sha256=SRsPlcMLYXLwyR7AtdVhClEpmcPCBa3eyeY869SmPrA,39
|
|
7
|
+
tomd_cli-0.1.0.dist-info/licenses/LICENSE,sha256=oEfiqyCkmXNw2hNSHZIJzxCkZEc-9j8kJpU4s6AQTXk,1071
|
|
8
|
+
tomd_cli-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kohki Nakamura
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|