tomd-cli 0.1.0__tar.gz
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_cli-0.1.0/.claude/settings.local.json +30 -0
- tomd_cli-0.1.0/.gitignore +26 -0
- tomd_cli-0.1.0/LICENSE +21 -0
- tomd_cli-0.1.0/PKG-INFO +78 -0
- tomd_cli-0.1.0/README.md +52 -0
- tomd_cli-0.1.0/draft.md +135 -0
- tomd_cli-0.1.0/pyproject.toml +45 -0
- tomd_cli-0.1.0/src/tomd/__init__.py +5 -0
- tomd_cli-0.1.0/src/tomd/cli.py +55 -0
- tomd_cli-0.1.0/src/tomd/converter.py +76 -0
- tomd_cli-0.1.0/tests/__init__.py +0 -0
- tomd_cli-0.1.0/tests/test_cli.py +28 -0
- tomd_cli-0.1.0/tests/test_converter.py +39 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"permissions": {
|
|
3
|
+
"allow": [
|
|
4
|
+
"Bash(pip3 show markitdown)",
|
|
5
|
+
"Bash(pip3 install markitdown)",
|
|
6
|
+
"Bash(pip3 install markitdown --break-system-packages)",
|
|
7
|
+
"Read(//usr/local/bin/**)",
|
|
8
|
+
"Bash(ln -sf /Users/nakamurakohki/workspace/private_dev/convert-md/convert_md.py /usr/local/bin/convert-md)",
|
|
9
|
+
"Bash(convert-md --help)",
|
|
10
|
+
"Bash(ln -sf /Users/nakamurakohki/workspace/private_dev/convert-md/convert_md.py /usr/local/bin/tomd)",
|
|
11
|
+
"Bash(tomd --help)",
|
|
12
|
+
"Bash(ln -sf /Users/nakamurakohki/workspace/private_dev/convert-md/tomd.py /usr/local/bin/tomd)",
|
|
13
|
+
"Bash(sudo ln -sf /Users/nakamurakohki/workspace/private_dev/tomd/tomd.py /usr/local/bin/tomd)",
|
|
14
|
+
"Bash(python3 /Users/nakamurakohki/workspace/private_dev/tomd/tomd.py \"バックオフィス業務システム化_提案資料.docx\" -o /tmp/test_output.md)",
|
|
15
|
+
"Read(//tmp/**)",
|
|
16
|
+
"Bash(python3 /Users/nakamurakohki/workspace/private_dev/tomd/tomd.py \"/Users/nakamurakohki/Downloads/バックオフィス業務システム化_提案資料.docx\" -o /tmp/test_output2.md)",
|
|
17
|
+
"Bash(pip3 index:*)",
|
|
18
|
+
"Bash(pip3 install:*)",
|
|
19
|
+
"Bash(for name:*)",
|
|
20
|
+
"Bash(do echo:*)",
|
|
21
|
+
"WebFetch(domain:pypi.org)",
|
|
22
|
+
"Bash(python3 -m pytest tests/ -v)",
|
|
23
|
+
"Read(//Users/nakamurakohki/**)",
|
|
24
|
+
"Bash(python3 -m build)"
|
|
25
|
+
],
|
|
26
|
+
"additionalDirectories": [
|
|
27
|
+
"/Users/nakamurakohki/workspace/private_dev/tomd/src"
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
*.egg
|
|
6
|
+
dist/
|
|
7
|
+
build/
|
|
8
|
+
|
|
9
|
+
# Virtual environments
|
|
10
|
+
.venv/
|
|
11
|
+
venv/
|
|
12
|
+
|
|
13
|
+
# Environment variables
|
|
14
|
+
.env
|
|
15
|
+
|
|
16
|
+
# Testing
|
|
17
|
+
.pytest_cache/
|
|
18
|
+
.coverage
|
|
19
|
+
htmlcov/
|
|
20
|
+
|
|
21
|
+
# IDE
|
|
22
|
+
.vscode/
|
|
23
|
+
.idea/
|
|
24
|
+
|
|
25
|
+
# OS
|
|
26
|
+
.DS_Store
|
tomd_cli-0.1.0/LICENSE
ADDED
|
@@ -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.
|
tomd_cli-0.1.0/PKG-INFO
ADDED
|
@@ -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
|
tomd_cli-0.1.0/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# tomd-cli
|
|
2
|
+
|
|
3
|
+
Convert Office files to Markdown using [MarkItDown](https://github.com/microsoft/markitdown).
|
|
4
|
+
|
|
5
|
+
## Supported formats
|
|
6
|
+
|
|
7
|
+
- `.docx` (Word)
|
|
8
|
+
- `.pptx` (PowerPoint)
|
|
9
|
+
- `.xlsx` (Excel)
|
|
10
|
+
- `.pdf`
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install tomd-cli
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### CLI
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
# Convert a single file
|
|
24
|
+
tomd document.docx
|
|
25
|
+
|
|
26
|
+
# Specify output path
|
|
27
|
+
tomd document.docx -o output.md
|
|
28
|
+
|
|
29
|
+
# Convert all files in a directory
|
|
30
|
+
tomd --dir ./documents/
|
|
31
|
+
|
|
32
|
+
# Convert directory with custom output
|
|
33
|
+
tomd --dir ./documents/ -o ./markdown/
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Python API
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from tomd import convert_file, convert_dir
|
|
40
|
+
|
|
41
|
+
# Single file
|
|
42
|
+
output_path = convert_file("document.docx")
|
|
43
|
+
output_path = convert_file("document.docx", "output.md")
|
|
44
|
+
|
|
45
|
+
# Directory
|
|
46
|
+
output_paths = convert_dir("./documents/")
|
|
47
|
+
output_paths = convert_dir("./documents/", "./markdown/")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
MIT
|
tomd_cli-0.1.0/draft.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
はい、**MarkItDown**というMicrosoft製のPythonライブラリがまさにそれです。 [note](https://note.com/kiyo_ai_note/n/n351a9018e99b)
|
|
2
|
+
|
|
3
|
+
完全にローカルPCで動作し、一括変換も可能です。以下に具体的なセットアップとコードを示します。
|
|
4
|
+
|
|
5
|
+
## MarkItDown ライブラリのインストールと確認
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install markitdown
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
これでPPTX、DOCX、XLSX、PDFなど多様な形式が一括でMarkdownに変換できます。 [okumuralab](https://okumuralab.org/~okumura/python/markitdown.html)
|
|
12
|
+
|
|
13
|
+
## 単一ファイル変換(最もシンプル)
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from markitdown import MarkItDown
|
|
17
|
+
|
|
18
|
+
# インスタンス作成
|
|
19
|
+
md = MarkItDown()
|
|
20
|
+
|
|
21
|
+
# PPTXをMarkdownに変換
|
|
22
|
+
result = md.convert("presentation.pptx")
|
|
23
|
+
|
|
24
|
+
# Markdownファイルとして保存
|
|
25
|
+
with open("presentation.md", "w", encoding="utf-8") as f:
|
|
26
|
+
f.write(result.text_content)
|
|
27
|
+
|
|
28
|
+
print("変換完了: presentation.md")
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
出力例(スライドが適切に`<!-- Slide number: 1 -->`形式で区切られる) [note](https://note.com/kiyo_ai_note/n/n351a9018e99b)
|
|
32
|
+
```
|
|
33
|
+
<!-- Slide number: 1 -->
|
|
34
|
+
# オープンデータに取り組む地方公共団体数の推移
|
|
35
|
+
- 2020年: 100団体
|
|
36
|
+
- 2021年: 150団体
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## フォルダ内全PPTX一括変換(実務向け)
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
import glob
|
|
43
|
+
import os
|
|
44
|
+
from pathlib import Path
|
|
45
|
+
from markitdown import MarkItDown
|
|
46
|
+
|
|
47
|
+
def batch_convert_pptx(input_folder="input", output_folder="output"):
|
|
48
|
+
# 出力フォルダ作成
|
|
49
|
+
os.makedirs(output_folder, exist_ok=True)
|
|
50
|
+
|
|
51
|
+
md = MarkItDown()
|
|
52
|
+
|
|
53
|
+
# inputフォルダ内の全PPTXを処理
|
|
54
|
+
pptx_files = glob.glob(f"{input_folder}/*.pptx")
|
|
55
|
+
|
|
56
|
+
for pptx_path in pptx_files:
|
|
57
|
+
print(f"変換中: {pptx_path}")
|
|
58
|
+
|
|
59
|
+
result = md.convert(pptx_path)
|
|
60
|
+
|
|
61
|
+
# ファイル名から拡張子を除いて.md生成
|
|
62
|
+
base_name = Path(pptx_path).stem
|
|
63
|
+
md_path = f"{output_folder}/{base_name}.md"
|
|
64
|
+
|
|
65
|
+
with open(md_path, "w", encoding="utf-8") as f:
|
|
66
|
+
f.write(result.text_content)
|
|
67
|
+
|
|
68
|
+
print(f"完了: {md_path}")
|
|
69
|
+
|
|
70
|
+
# 実行
|
|
71
|
+
batch_convert_pptx()
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## ディレクトリ構造例
|
|
75
|
+
```
|
|
76
|
+
project/
|
|
77
|
+
├── input/ # PPTXファイルをここに放り込む
|
|
78
|
+
│ ├── slide1.pptx
|
|
79
|
+
│ └── slide2.pptx
|
|
80
|
+
├── output/ # 自動でMarkdown生成
|
|
81
|
+
│ ├── slide1.md
|
|
82
|
+
│ └── slide2.md
|
|
83
|
+
└── convert.py
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## VSCode + ターミナルでワンクリック運用
|
|
87
|
+
|
|
88
|
+
1. 上記スクリプトを`convert.py`として保存
|
|
89
|
+
2. VSCodeで作業フォルダを開く
|
|
90
|
+
3. `Ctrl+`` `でターミナルを開く
|
|
91
|
+
4. `python convert.py`で一括変換
|
|
92
|
+
|
|
93
|
+
## 高度な使い方(Azure OpenAI連携)
|
|
94
|
+
|
|
95
|
+
画像内の文字や複雑なレイアウトも読み取りたい場合:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from openai import OpenAI
|
|
99
|
+
from markitdown import MarkItDown
|
|
100
|
+
|
|
101
|
+
# Azure OpenAIクライアント
|
|
102
|
+
client = OpenAI(
|
|
103
|
+
api_key="your-azure-openai-key",
|
|
104
|
+
azure_endpoint="your-endpoint"
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
# LLM強化版MarkItDown
|
|
108
|
+
md = MarkItDown(
|
|
109
|
+
llm_client=client,
|
|
110
|
+
llm_model="gpt-4o"
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
result = md.convert("complex-slides.pptx")
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## 注意点と精度向上のコツ
|
|
117
|
+
|
|
118
|
+
1. **スライドデザインをシンプルに**
|
|
119
|
+
- テキストはテキストボックス内に
|
|
120
|
+
- 画像に文字を埋め込まない
|
|
121
|
+
|
|
122
|
+
2. **出力確認と微調整**
|
|
123
|
+
```python
|
|
124
|
+
# 変換前にプレビュー
|
|
125
|
+
print(result.text_content[:1000]) # 最初の1000文字確認
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
3. **大容量ファイル対応**
|
|
129
|
+
```python
|
|
130
|
+
md = MarkItDown(max_memory_mb=2048) # メモリ使用量指定
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
この方法なら、**ローカル完結・一括変換・高精度**の3点を満たします。 [zenn](https://zenn.dev/acntechjp/articles/e794ed9d524812)
|
|
134
|
+
|
|
135
|
+
中村さんのAzure/Python環境なら、すぐに運用開始できそうですね。試してみたい具体的なユースケースがあれば、さらにカスタマイズしたスクリプトも作れます!
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tomd-cli"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Convert Office files (.docx, .pptx, .xlsx, .pdf) to Markdown using MarkItDown"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Kohki Nakamura" },
|
|
14
|
+
]
|
|
15
|
+
keywords = ["markdown", "docx", "pptx", "xlsx", "pdf", "converter"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 3 - Alpha",
|
|
18
|
+
"Environment :: Console",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Operating System :: MacOS",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.10",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Programming Language :: Python :: 3.13",
|
|
27
|
+
"Topic :: Text Processing :: Markup :: Markdown",
|
|
28
|
+
]
|
|
29
|
+
dependencies = [
|
|
30
|
+
"markitdown>=0.0.2",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.scripts]
|
|
34
|
+
tomd = "tomd.cli:main"
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Homepage = "https://github.com/nakamurakohki/tomd-cli"
|
|
38
|
+
Repository = "https://github.com/nakamurakohki/tomd-cli"
|
|
39
|
+
Issues = "https://github.com/nakamurakohki/tomd-cli/issues"
|
|
40
|
+
|
|
41
|
+
[tool.hatch.build.targets.wheel]
|
|
42
|
+
packages = ["src/tomd"]
|
|
43
|
+
|
|
44
|
+
[tool.pytest.ini_options]
|
|
45
|
+
testpaths = ["tests"]
|
|
@@ -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)
|
|
@@ -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
|
|
File without changes
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Tests for tomd.cli."""
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from tomd.cli import main
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_help_flag(capsys):
|
|
8
|
+
with pytest.raises(SystemExit) as exc_info:
|
|
9
|
+
main(["--help"])
|
|
10
|
+
assert exc_info.value.code == 0
|
|
11
|
+
captured = capsys.readouterr()
|
|
12
|
+
assert "Convert Office files" in captured.out
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_missing_file(capsys):
|
|
16
|
+
with pytest.raises(SystemExit) as exc_info:
|
|
17
|
+
main(["nonexistent.docx"])
|
|
18
|
+
assert exc_info.value.code == 1
|
|
19
|
+
captured = capsys.readouterr()
|
|
20
|
+
assert "Error" in captured.err
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_missing_dir(capsys):
|
|
24
|
+
with pytest.raises(SystemExit) as exc_info:
|
|
25
|
+
main(["--dir", "nonexistent_dir"])
|
|
26
|
+
assert exc_info.value.code == 1
|
|
27
|
+
captured = capsys.readouterr()
|
|
28
|
+
assert "Error" in captured.err
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Tests for tomd.converter."""
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from tomd.converter import strip_base64_images
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TestStripBase64Images:
|
|
8
|
+
def test_replaces_base64_png(self):
|
|
9
|
+
text = ''
|
|
10
|
+
result = strip_base64_images(text)
|
|
11
|
+
assert result == '![alt text]()'
|
|
12
|
+
|
|
13
|
+
def test_replaces_base64_jpeg(self):
|
|
14
|
+
text = ''
|
|
15
|
+
result = strip_base64_images(text)
|
|
16
|
+
assert result == '![photo]()'
|
|
17
|
+
|
|
18
|
+
def test_preserves_empty_alt(self):
|
|
19
|
+
text = ''
|
|
20
|
+
result = strip_base64_images(text)
|
|
21
|
+
assert result == '![]()'
|
|
22
|
+
|
|
23
|
+
def test_preserves_normal_images(self):
|
|
24
|
+
text = ''
|
|
25
|
+
result = strip_base64_images(text)
|
|
26
|
+
assert result == ''
|
|
27
|
+
|
|
28
|
+
def test_preserves_plain_text(self):
|
|
29
|
+
text = 'Hello world'
|
|
30
|
+
result = strip_base64_images(text)
|
|
31
|
+
assert result == 'Hello world'
|
|
32
|
+
|
|
33
|
+
def test_multiple_images(self):
|
|
34
|
+
text = (
|
|
35
|
+
' middle '
|
|
36
|
+
''
|
|
37
|
+
)
|
|
38
|
+
result = strip_base64_images(text)
|
|
39
|
+
assert result == '![a]() middle ![b]()'
|