Skip to content

Instantly share code, notes, and snippets.

@goghvanmr
Created August 7, 2018 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goghvanmr/f8591635869b4cbeda7380c21854db16 to your computer and use it in GitHub Desktop.
Save goghvanmr/f8591635869b4cbeda7380c21854db16 to your computer and use it in GitHub Desktop.
Export Kindle Notes from Kindle exported html file to Anki imported compatible csv file
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import unicodecsv as csv
def export_notes_from(htmlFileName, csvFileName):
with open (htmlFileName) as html:
soup = BeautifulSoup(html, 'html.parser')
notes = [note.contents[0].strip('\r\n\ ') for note in soup.find_all('div', {'class', 'noteText'})]
title = soup.find(class_='bookTitle').contents[0].strip('\r\n\ ')
authors = soup.find(class_='authors').contents[0].strip('\r\n\ ')
titleAndAuthors = title + '\n' + authors
with open(csvFileName, 'wb') as csvFile:
writer = csv.writer(csvFile, delimiter=',')
for note in notes:
writer.writerow([note, titleAndAuthors])
htmlFile = 'notes.html'
ankiCSV = 'exported.csv'
if __name__ == '__main__':
export_notes_from(htmlFile, ankiCSV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment