initial commit
This commit is contained in:
14
README.MD
Normal file
14
README.MD
Normal file
@@ -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
|
||||||
61
markdown-to-enex.py
Normal file
61
markdown-to-enex.py
Normal file
@@ -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'''<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export.dtd">
|
||||||
|
<en-export export-date="20250706T000000Z" application="PythonScript" version="1.0">
|
||||||
|
<note>
|
||||||
|
<title>{escape(title)}</title>
|
||||||
|
<content><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
|
||||||
|
<en-note>{html_content}</en-note>
|
||||||
|
]]></content>
|
||||||
|
</note>
|
||||||
|
</en-export>'''
|
||||||
|
|
||||||
|
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 <input_folder>")
|
||||||
|
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}")
|
||||||
Reference in New Issue
Block a user