-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathbatch_converter.py
More file actions
33 lines (25 loc) · 905 Bytes
/
batch_converter.py
File metadata and controls
33 lines (25 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from pathlib import Path
from markitdown import MarkItDown
def main(
input_dir,
output_dir="output",
target_formats=(".docx", ".xlsx", ".pdf"),
):
input_path = Path(input_dir)
output_path = Path(output_dir)
output_path.mkdir(parents=True, exist_ok=True)
md = MarkItDown()
for file_path in input_path.rglob("*"):
if file_path.suffix in target_formats:
try:
result = md.convert(file_path)
except Exception as e:
print(f"✗ Error converting {file_path.name}: {e}")
continue
output_file = (
output_path / f"{file_path.stem}{file_path.suffix}.md"
)
output_file.write_text(result.markdown, encoding="utf-8")
print(f"✓ Converted {file_path.name} → {output_file.name}")
if __name__ == "__main__":
main("data", "output")