storizzi / notes-exporter

Export Apple Notes to html, plain text, Markdown (eg for Obsidian), PDF, and DOCX (word) including images / attachments
MIT License
36 stars 3 forks source link

Apple Notes Export Tool

Notes Exporter

This tool facilitates the export of Apple Notes into various formats including HTML (.html) attachment-embedded, Plain Text, Markdown, PDF, and Word (DOCX) with images / attachments and most formatting left intact.

You can use it either as a basic backup, or as a conversion tool - e.g. the Markdown format could be used in a note-taking tool like Obsidian which uses Markdown as standard.

This tool works on a Mac (OSX) not on a Windows or Linux machine because it uses AppleScript to extract the data from notes. You need to have the Notes app installed.

This allows for you to keep a local copy of your apple notes in case of failure, and also to use them in other markdown note-taking apps such as Obsidian, and quickly grab copies if you wish to send PDF versions to anyone, or work on a copy of the document from a Word document with all the images intact and inline in the right place.

It also extracts images from the notes, so they can also be referenced from local HTML (.htm) documents, Markdown, PDF, or Word (DOCX) documents.

Setup

Install homebrew

If Homebrew is not installed, open the Terminal and run this command:

   /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install Git

If Git is not already installed on your Mac, you can install it using Homebrew (or get a more up-to-date version!)

brew install git

Decide where to download the script

Change directory in terminal to wherever you want the scripts to be downloaded to - e.g.

cd $HOME/Downloads

Clone the Repository

Clone the notes-exporter repository from GitHub:

mkdir -p ~/bin/notes-exporter
cd ~/bin/notes-exporter
git clone https://github.com/storizzi/notes-exporter.git .
cd notes-exporter

Install Python and Dependencies

Ensure Python 3 is installed on your Mac. Python 3 can be installed via Homebrew:

brew install python

or alternatively if you want to use conda to install python environments you can use:

brew install --cask miniconda

If you are using conda, then you can get the script to do the next bit for you (see later) in which case you can skip the 'pip install' here - this is only recommended if you are installing python libraries globally.

After installing Python, install the required Python libraries using pip (markdownify is not required if you do not wish to generate markdown files and pypandoc is not required if you do not wish to generate docx / word files):

pip install beautifulsoup4  # For parsing HTML files.
pip install markdownify     # For converting HTML to Markdown.
pip install pypandoc        # For converting HTML to DOCX. Requires Pandoc.

or alternatively pip install -r ./requirements.txt

Note: pypandoc requires Pandoc for generating docx (word documents). If you need this functionality, install it using Homebrew if not already installed:

brew install pandoc

Note: To convert to PDF, Google Chrome needs to be installed

Additional Dependencies

Basic Usage

The exportnotes.zsh script is used to export Apple Notes. It accepts various command-line parameters to customize the export process.

Default Behavior

By default, the script will:

Running the Script

Ensure the script has execution permissions:

cd ~/bin/notes-exporter
chmod +x exportnotes.zsh

Run the script from the directory where it is located or add the script's directory to your PATH in your .zshrc or .bashrc file for easy access. For example, if you place the scripts in $HOME/bin/notes-exporter:

echo 'export PATH="$HOME/bin/notes-exporter:$PATH"' >> ~/.zshrc
source ~/.zshrc

Then, you can run the script from anywhere by just typing exportnotes.zsh.

Examples

Export notes with default settings (backup to user's Documents/NotesExport directory):

exportnotes.zsh

Export notes with default settings in a python conda environment called notesexport auto-creating and activating the environment if it doesn't exist, and auto-installing the required pip dependencies - or just activating that environment if it already exists:

exportnotes.zsh --conda-env notesexport

Export notes to a specific directory and convert them to Markdown:

mkdir $HOME/Documents/NotesExport
exportnotes.zsh --root-dir $HOME/Documents/NotesExport --convert-markdown true

Use existing imported files from a specific directory and convert to PDF

mkdir $HOME/Documents/NotesExport
exportnotes.zsh --root-dir $HOME/Documents/NotesExport --extract-data false --convert-pdf true

Suppress headers and footers in PDF and also convert to Word:

exportnotes.zsh -s true -w true

Convert notes in a specific directory to Markdown without getting any apple notes, creating a conda environment at the start, and cleaning up that environment afterwards

exportnotes.zsh --root-dir $HOME/AppleNotesExport --conda-env exportnotes --extract-data false --convert-markdown true --remove-conda-env true

Import notes using filenames starting Note- then with the title, then another - and the internal ID of the note at the end

exportnotes.zsh  --filename-format "Note-&title-&id"

Import notes using filenames using the account name and the internal apple note ID and the folder ID without having subdirectories for the folder name

exportnotes.zsh --filename-format "&account-&folder-&id" --use-subdirs false

Advanced Usage

Command-Line Parameters

Environment Variables

Instead of using command line parameters, you can set up environment variables which will be used by default (and which can be overridden by command line parameters) if for example you want to set up a standard way of working when running the script but sometimes override the behavior.

The available environment variables are:

You could add these to your .zshrc file for example to set up defaults so you don't have to use command-line parameters if you want to set up a specific location to export to, for example - e.g.

export NOTES_EXPORT_ROOT_DIR=$HOME/Documents/NotesExport

Configure Zsh

To make the scripts easily accessible, add the script directory to your PATH in the .zshrc file so you can just run the command when you open the terminal:

  1. Open .zshrc in a text editor (e.g., nano, vim):

    nano ~/.zshrc
  2. Add the following line to the file: Replace /path/to/notes-exporter with the actual path to the notes-exporter directory - e.g. $HOME/bin if you have copied the files to the bin directory for your user's mac account.

    export PATH="/path/to/notes-exporter:$PATH"
  3. Save and close the file: If using nano, press CTRL + X, then Y to save, and Enter to exit.
  4. Reload the .zshrc file:

    source ~/.zshrc

    or just start a new Terminal window or tab.