From 8b7ffce478c64897a4211ed0ff7a3ddaed5aa849 Mon Sep 17 00:00:00 2001 From: Matt Speer Date: Sat, 20 Dec 2025 12:27:54 -0600 Subject: [PATCH] initial commit --- README.MD | 14 +++++++++++ markdown-to-enex.py | 61 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 README.MD create mode 100644 markdown-to-enex.py diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..d5ff011 --- /dev/null +++ b/README.MD @@ -0,0 +1,14 @@ +# File Conversion Scripts + +A collection of scripts used to convert files from one format to another. + +## Markdown to Evernote ENEX + +This script is used to convert markdown files into Evernote ENEX files. Pass the directory containing the markdown files you would like to convert as a command line argument. It will convert all markdown files in the directory and any subdirectory into html, preserving the directory structure in place. The converted files are saved to `~/mdConverted`. + +You can use the ENEX files to import into Apple Notes or Evernote. + +### Prerequisites +* Python 3 +* Pandoc +* Pypandoc \ No newline at end of file diff --git a/markdown-to-enex.py b/markdown-to-enex.py new file mode 100644 index 0000000..6c51954 --- /dev/null +++ b/markdown-to-enex.py @@ -0,0 +1,61 @@ +import os +from pathlib import Path +import pypandoc +from xml.sax.saxutils import escape + + +def convert_markdown_to_enex(markdown_file_path, output_file_path): + # Convert Markdown to HTML + html_content = pypandoc.convert_file(str(markdown_file_path), 'html', format='markdown') + + # Extract title from file name + title = markdown_file_path.stem + + # Create Evernote ENEX structure + enex_content = f''' + + + + {escape(title)} + + + {html_content} + ]]> + + ''' + + with open(output_file_path, 'w', encoding='utf-8') as f: + f.write(enex_content) + + +def process_folder(input_folder, output_folder): + input_folder = Path(input_folder).resolve() + output_folder = Path(output_folder).expanduser().resolve() + + for root, dirs, files in os.walk(input_folder): + rel_path = Path(root).relative_to(input_folder) + target_dir = output_folder / rel_path + target_dir.mkdir(parents=True, exist_ok=True) + + for file in files: + if file.lower().endswith(".md"): + md_file_path = Path(root) / file + + enex_filename = md_file_path.stem + ".enex" + enex_file_path = target_dir / enex_filename + + convert_markdown_to_enex(md_file_path, enex_file_path) + + +if __name__ == "__main__": + import sys + + if len(sys.argv) < 2: + print("Usage: python convert_md_to_enex.py ") + sys.exit(1) + + input_dir = sys.argv[1] + output_dir = "~/mdConverted" + + process_folder(input_dir, output_dir) + print(f"Conversion completed. ENEX files saved in {output_dir}")