Skip to content

Instantly share code, notes, and snippets.

@rodrigorgs
Created January 5, 2022 11:22
Show Gist options
  • Save rodrigorgs/121e02cb3f8a0d194af024fb16ae2b0e to your computer and use it in GitHub Desktop.
Save rodrigorgs/121e02cb3f8a0d194af024fb16ae2b0e to your computer and use it in GitHub Desktop.
Highlight clippings on an ebook
#!/usr/bin/env python3
# Highlight clippings on an ebook
# Input:
# - clippings file (generated by Calibre --
# see https://www.mobileread.com/forums/showthread.php?t=76069)
# - ebook in HTML format (exported by Calibre)
#
# Output:
# - HTML ebook with highlights in yellow
#
import re
BOOK_PATH = 'book/index.html'
CLIPPINGS_PATH = 'clippings.txt'
OUTPUT_PATH = 'book-annotated.html'
# Read book
with open(BOOK_PATH) as f:
html = f.read()
html = re.sub('<a id="calibre_link-.+?"></a>', '', html)
# Read clippings
with open(CLIPPINGS_PATH) as f:
clippings = f.read()
items = clippings.split('==========\n')
highlights = []
for item in items:
highlights.append(item.split('\n')[3])
# Highlight
for highlight in highlights:
html = html.replace(highlight, f'<span style="background: yellow;">{highlight}</span>')
# Output
with open(OUTPUT_PATH, 'w') as f:
f.write(html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment