# MarkItDownマークダウン変換プログラム # README: https://www.kkaneko.jp/ai/markitdown/README.md import argparse import sys from pathlib import Path from typing import Optional from markitdown import MarkItDown def parse_arguments(): parser = argparse.ArgumentParser(description='Convert various file formats to Markdown') parser.add_argument('input_file', help='Input file path') parser.add_argument('-o', '--output', help='Output markdown file path') parser.add_argument('--verbose', action='store_true', help='Enable verbose output') return parser.parse_args() def process_markdown(input_path: str, output_path: Optional[str] = None, verbose: bool = False): try: md = MarkItDown() if verbose: print(f"Converting {input_path}...") result = md.convert(input_path) if output_path: with open(output_path, 'w', encoding='utf-8') as f: f.write(result.text_content) if verbose: print(f"Successfully saved to {output_path}") else: print(result.text_content) return True except Exception as e: print(f"Error processing file: {e}", file=sys.stderr) return False def main(): args = parse_arguments() input_path = Path(args.input_file) if not input_path.exists(): print(f"Error: Input file '{args.input_file}' does not exist", file=sys.stderr) sys.exit(1) if not process_markdown(str(input_path), args.output, args.verbose): sys.exit(1) if __name__ == "__main__": main()