Jump to content

Python script to fix ENEX files when exporting to Apple Notes


Recommended Posts

Hello all,

If you (maybe based on the recent price increase) have tried to move from Evernote to Apple Notes, you might have noticed that ordered and unordered lists have double line spacing when imported into Apple Notes, like:

  • First item
     
  • Second item
     
  • Third item

This is due to list item contents being encapsulated within DIV tags, which causes Apple Notes ENEX-importer to add extra lines.

To fix this I wrote together a quick Python script to fix the issue. To run it, just install something like PyCharm, then:

  1. Start a new project
  2. Paste the text below
  3. Change the root folder on the last line to the folder where you have all your ENEX files. The script searches recursively in subfolders.
  4. import the library dependencies (just right click pathlib, re and glob at the top
  5. Then run it

Please note that I take no responsibility for any issues this script might cause, by copying the code below you acknowledge that you take full responsibility for running it on your computer.

from pathlib import Path
import re
import glob


def remove_nested_div_tags(text):
    # Regex pattern for nested DIV tags within LI tags
    pattern = r'(<li[^>]*>)(.*?)(<\/li>)'

    # Function to process each match found
    def process_match(match):
        list_start, list_content, list_end = match.groups()
        list_content = re.sub(r'<div[^>]*>(.*?)<\/div>', r'\1', list_content)
        return list_start + list_content + list_end

    # Replace the nested DIV tags with their content
    output_str = re.sub(pattern, process_match, text)

    return output_str


def process_files(root_dir):
    # root_dir needs a trailing slash (i.e. /root/dir/)
    for filename in glob.iglob(root_dir + '**/*.enex', recursive=True):
        print(filename)
        with open(filename, "r+") as enex_file:
            content = enex_file.readlines()
            # Combine the lines in the list into a string
            content = "".join(content)

            content = remove_nested_div_tags(content)

            p = Path(filename)
            path = str(p.parent)
            name = p.stem
            extension = p.suffix
            outname = path + '/_' + name + extension
            with open(outname, 'w') as file:
                file.write(content)
                file.close()

            enex_file.close()


if __name__ == '__main__':
    process_files('/Users/johan/Desktop/Evernote 20230430/')

 

  • Thanks 3
Link to comment
2 hours ago, johansan said:

Hello all,

If you (maybe based on the recent price increase) have tried to move from Evernote to Apple Notes, you might have noticed that ordered and unordered lists have double line spacing when imported into Apple Notes, like:

  • First item
     
  • Second item
     
  • Third item

This is due to list item contents being encapsulated within DIV tags, which causes Apple Notes ENEX-importer to add extra lines.

To fix this I wrote together a quick Python script to fix the issue. To run it, just install something like PyCharm, then:

  1. Start a new project
  2. Paste the text below
  3. Change the root folder on the last line to the folder where you have all your ENEX files. The script searches recursively in subfolders.
  4. import the library dependencies (just right click pathlib, re and glob at the top
  5. Then run it

Please note that I take no responsibility for any issues this script might cause, by copying the code below you acknowledge that you take full responsibility for running it on your computer.

from pathlib import Path
import re
import glob


def remove_nested_div_tags(text):
    # Regex pattern for nested DIV tags within LI tags
    pattern = r'(<li[^>]*>)(.*?)(<\/li>)'

    # Function to process each match found
    def process_match(match):
        list_start, list_content, list_end = match.groups()
        list_content = re.sub(r'<div[^>]*>(.*?)<\/div>', r'\1', list_content)
        return list_start + list_content + list_end

    # Replace the nested DIV tags with their content
    output_str = re.sub(pattern, process_match, text)

    return output_str


def process_files(root_dir):
    # root_dir needs a trailing slash (i.e. /root/dir/)
    for filename in glob.iglob(root_dir + '**/*.enex', recursive=True):
        print(filename)
        with open(filename, "r+") as enex_file:
            content = enex_file.readlines()
            # Combine the lines in the list into a string
            content = "".join(content)

            content = remove_nested_div_tags(content)

            p = Path(filename)
            path = str(p.parent)
            name = p.stem
            extension = p.suffix
            outname = path + '/_' + name + extension
            with open(outname, 'w') as file:
                file.write(content)
                file.close()

            enex_file.close()


if __name__ == '__main__':
    process_files('/Users/johan/Desktop/Evernote 20230430/')

 

Does Apple Notes have note links? If so, how will you port them?

Link to comment
1 hour ago, PinkElephant said:

The only links I am aware of in Apple notes are links to the internet, the usual URL type.

There are no note2note links, and no backlinks.

Yes, no note-2-note links, no backlinks. No highlight color, no code block. And pretty poor text formatting as well (no extra line spacing before headings etc).

Apple Notes is pretty ***** unfortunately, which means I will probably stay with Evernote myself despite the price increase.

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...