Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hlorand/b1062261f858f30ad711ac3b8885b200 to your computer and use it in GitHub Desktop.
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.
"""
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