Created
July 18, 2024 10:15
-
-
Save hlorand/b1062261f858f30ad711ac3b8885b200 to your computer and use it in GitHub Desktop.
Converts Kindle E-reader's My Clippings.txt file into a Markdown file.
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
""" | |
Converts Kindle E-reader's My Clippings.txt file into a Markdown file. | |
Usage: place My Clippings.txt next to python file, run python file | |
Output format: | |
Book title | |
========== | |
Highlight1... | |
Highlight2... | |
... | |
""" | |
file = open("My Clippings.txt") | |
clippings = file.read().replace("\ufeff","").split("==========\n") | |
books = {} | |
for clipping in clippings: | |
parts = clipping.split("\n") | |
parts = list(filter(None, parts)) # remove empty lines | |
if len(parts) == 0: | |
break | |
title = parts[0] | |
location = parts[1] | |
clipping = parts[2:] | |
if title not in books: | |
books[title] = {} | |
if len(clipping) > 0: | |
books[title][location] = clipping | |
# helper functions | |
import re | |
def atoi(text): | |
return int(text) if text.isdigit() else text | |
def natural_keys(text): | |
return [ atoi(c) for c in re.split(r'(\d+)', text) ] | |
################## | |
for title in books: | |
print(title) | |
print("=============\n") | |
for location in sorted(books[title].keys() ,key=natural_keys): | |
if "Bookmark" in location: # skip bookmarks | |
continue | |
print( *books[title][location], end = "\n\n") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment