Created
November 26, 2024 09:50
-
-
Save selcukcihan/59f74fa4319c0ec352493fe6fd9b1501 to your computer and use it in GitHub Desktop.
Python script that parses your Kindle notes (My Clippings.txt on the device) and creates a separate .md file for each book.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import pathlib | |
def parse_kindle_notes(input_file): | |
with open(input_file, "r") as file: | |
lines = [x.strip() for x in file.readlines()] | |
notes_dict = {} | |
current_book = None | |
current_notes = [] | |
current_index = 0 | |
while True: | |
current_book = lines[current_index] | |
if current_book not in notes_dict: | |
notes_dict[current_book] = [] | |
current_notes = [] | |
current_index += 2 | |
while current_index < len(lines) and lines[current_index] != "==========": | |
if lines[current_index]: | |
current_notes.append(lines[current_index]) | |
current_index += 1 | |
notes_dict[current_book] += current_notes | |
current_index += 1 | |
if current_index >= len(lines): | |
break | |
pathlib.Path("notes").mkdir(exist_ok=True) | |
for book, notes in notes_dict.items(): | |
file_name = f"{book.replace(' ', '_')}.md" | |
with open(f"notes/{file_name}", "w") as md_file: | |
md_file.write(f"# {book}\n\n") | |
md_file.write("\n\n".join(notes)) | |
print("reading " + sys.argv[1]) | |
parse_kindle_notes(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment