├── README.md ├── .gitignore └── converter.py /README.md: -------------------------------------------------------------------------------- 1 | # onenote_to_markdown 2 | Script to convert MS OneNote for Windows 10 notebooks to directories of markdown files. This can be helpful when moving from MS OneNote to Joplin. 3 | 4 | # How to use 5 | 1. Select and copy all the notes in the OneNote notebooks you would like to convert to markdown files 6 | 2. Save each notebook in a single directory as its own text file with the notebook name as the name of the text file 7 | 3. Make sure Python 3 is installed on your machine 8 | 4. Open command prompt and run `pip install win32_setctime` and `pip install pytz` 9 | 5. run `python converter.py '/path/to/your/text/files'` 10 | 6. Onenote notes are now converted to markdown files in the supplied path 11 | 7. To import the files to Joplin. Open Joplin and go to File->Import->MD - Markdown Directory and select the directories you would like to import 12 | 13 | # Notes 14 | - The modified and created dates of the output markdown files are set as the created date of the OneNote notes 15 | - This script was only tested on Microsoft OneNote for Windows 10 and Python 3.7 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /converter.py: -------------------------------------------------------------------------------- 1 | ''' 2 | python executable that takes command line arguments for in_dir, out_dir 3 | ''' 4 | 5 | import argparse 6 | from collections import deque 7 | import datetime as dt 8 | from os import utime 9 | import pathlib 10 | from pytz import timezone 11 | import time 12 | from typing import Set 13 | from win32_setctime import setctime 14 | 15 | EASTERN = timezone('US/Eastern') 16 | DOW = {"Monday", "Tuesday", "Wednesday", 17 | "Thursday", "Friday", "Saturday", "Sunday"} 18 | INVALID_CHARS = ['<', '>', ':', '"', '/', '\\', '|', '?', '*'] 19 | 20 | 21 | def isTimeFormat(i): 22 | try: 23 | time.strptime(i, '%H:%M') 24 | return True 25 | except ValueError: 26 | return False 27 | 28 | 29 | def init_argparse() -> argparse.ArgumentParser: 30 | parser = argparse.ArgumentParser( 31 | description="Produce directories of markdown files from a copy-and-pasted directory of onenote notebooks" 32 | ) 33 | parser.add_argument("input_dir", action="store", 34 | help="The absolute unix-style path where the copy-and-pasted onenote notebooks are stored as text. For example 'C:/Users/Me/Documents' or '/usr/me'") 35 | return parser 36 | 37 | 38 | def parse_path(input_dir: str) -> pathlib.Path: 39 | path = pathlib.Path(input_dir) 40 | 41 | if not path.exists(): 42 | raise ValueError("Input path is not a valid path") 43 | elif not path.is_dir(): 44 | raise ValueError("Input path is not a directory") 45 | 46 | return path 47 | 48 | 49 | def get_notebook_lines(notebook_file: pathlib.PosixPath) -> Set[str]: 50 | note_lines = set() 51 | prev_lines = deque([], maxlen=4) 52 | 53 | with notebook_file.open("r", errors="ignore") as file: 54 | for line_num, curr_line in enumerate(file, 1): 55 | prev_lines.appendleft(curr_line) 56 | if line_num < 6: 57 | if line_num == 1: 58 | note_lines.add(line_num) 59 | continue 60 | 61 | if (":" in curr_line and isTimeFormat(curr_line.split()[0]) and 62 | prev_lines[1].split(",")[0] in DOW): 63 | note_lines.add(line_num - 3) 64 | return note_lines 65 | 66 | 67 | def produce_markdown_files(notebook_file: pathlib.PosixPath, notebook_lines: Set[str]): 68 | curr_file_loc = None 69 | curr_title = None 70 | curr_datetime = None 71 | lines = [] 72 | 73 | def write_file(): 74 | if curr_file_loc.exists(): 75 | raise ValueError(curr_file_loc.name + " already exists") 76 | file = curr_file_loc.open("w", errors="strict", encoding='utf-8') 77 | file.writelines(lines) 78 | file.close() 79 | setctime(curr_file_loc, curr_datetime) 80 | utime(curr_file_loc, (curr_datetime, curr_datetime)) 81 | 82 | with notebook_file.open("r", encoding="utf-8") as file: 83 | for line_num, curr_line in enumerate(file, 1): 84 | # title found 85 | if line_num in notebook_lines: 86 | if curr_file_loc is not None: 87 | print("writing to", curr_file_loc) 88 | write_file() 89 | lines.clear() 90 | curr_file_loc = None 91 | 92 | curr_title = curr_line 93 | # Date found 94 | elif line_num - 2 in notebook_lines: 95 | curr_datetime = dt.datetime.strptime( 96 | curr_line.rstrip(), '%A, %B %d, %Y') 97 | # Time found 98 | elif line_num - 3 in notebook_lines: 99 | curr_datetime = dt.datetime.combine(curr_datetime, 100 | dt.datetime.strptime(curr_line.split()[0], '%H:%M').time()) 101 | curr_datetime = EASTERN.localize(curr_datetime) 102 | curr_datetime = time.mktime(curr_datetime.timetuple()) 103 | else: 104 | if curr_file_loc is None and not curr_line.isspace(): 105 | if curr_title.isspace(): 106 | curr_title = ' '.join(curr_line.split()[:7]) 107 | else: 108 | curr_title = curr_title[0:-1] 109 | 110 | for char in INVALID_CHARS: 111 | curr_title = curr_title.replace(char, " ") 112 | 113 | curr_file_loc = notebook_file.parent / \ 114 | (notebook_file.name[:-4] + "_notes") 115 | curr_file_loc.mkdir(exist_ok=True) 116 | curr_file_loc = curr_file_loc / (curr_title + ".md") 117 | 118 | if curr_file_loc is not None: 119 | lines.append(curr_line) 120 | 121 | # Write the last file once all 122 | write_file() 123 | 124 | 125 | def process_text_files(in_dir: pathlib.Path) -> None: 126 | for notebook_text_file in in_dir.iterdir(): 127 | try: 128 | notebook_lines = get_notebook_lines(notebook_text_file) 129 | produce_markdown_files(notebook_text_file, notebook_lines) 130 | except (PermissionError, ValueError) as e: 131 | print("ERROR creating notebook from text file " + 132 | notebook_text_file.name + ". It will be skipped. error=", e) 133 | 134 | 135 | def main() -> None: 136 | parser = init_argparse() 137 | args = parser.parse_args() 138 | in_path = parse_path(args.input_dir) 139 | process_text_files(in_path) 140 | 141 | 142 | if __name__ == "__main__": 143 | main() 144 | --------------------------------------------------------------------------------