Creating an application to extract formatted notes, highlights, and images from your Kindle books (or their EPUB/MOBI/AZW3 versions in your Calibre library) is a complex but very achievable project. Here's a structured breakdown of how you can approach this:
🧠 Problem Overview
You want:
- To extract text highlights and notes with formatting preserved (e.g., bullet points).
- To extract highlighted images that are marked in the book.
- To compile these into a new "clippings" document in HTML or similar format.
The challenge lies in:
- Parsing different ebook formats (EPUB, MOBI, AZW3).
- Finding user highlights/notes, which may be stored separately or embedded in the file.
- Preserving formatting like lists, italics, bold, etc.
- Extracting images, especially those that were manually highlighted or bookmarked.
🛠️ Step-by-Step Technical Approach
1. Understand Where Highlights Are Stored
For Kindle Clippings:
- The standard
My Clippings.txt file does not preserve formatting or images.
- You'll need to work directly with the original ebook files (EPUB/MOBI/AZW3).
For Calibre Library:
- Calibre stores annotations/highlights in its own database (
metadata.db) if synced via Calibre Companion or Kindle plugin.
- Alternatively, some devices export highlights as
.sdr folders next to the .mobi file on Kindle devices.
We’ll focus on extracting highlights directly from the ebook file structure.
2. Choose Ebook Format Support Strategy
| Format | Notes |
|---|
| EPUB | Open standard, XML-based, easy to parse. |
| MOBI / AZW3 | Proprietary; use tools like kindleunpack or Calibre to convert to EPUB first. |
👉 Recommendation: Convert all MOBI/AZW3 files to EPUB using Calibre CLI before processing.
ebook-convert input.mobi output.epub
3. Extract Text Highlights & Formatting
a. Parse EPUB Structure
An EPUB consists of:
OEBPS/content.opf: Metadata and spine.
OEBPS/toc.ncx: Table of contents.
OEBPS/xhtml/*.xhtml: Main content files.
- Optional:
OEBPS/annotations.xhtml or similar for highlights (depends on reading app).
b. Find Highlighted Text
Kindle annotations aren't embedded in EPUB by default. However, if you're syncing through Calibre + Kindle plugin, annotations will be in the Calibre DB.
If working directly from the ebook:
- Some apps store highlights via
<mark> tags or custom classes like <span class="highlight">.
- Use XPath or CSS selectors to find these elements in the XHTML files.
Example:
from lxml import etree
# Load xhtml file
tree = etree.parse("chapter.xhtml")
ns = {"h": "http://www.w3.org/1999/xhtml"}
highlights = tree.xpath("//h:span[@class='highlight']", namespaces=ns)
for hl in highlights:
print(etree.tostring(hl, pretty_print=True))
4. Preserve Formatting
When extracting text, keep the original HTML fragment around the highlight.
Instead of just extracting raw text:
etree.tostring(hl.getparent(), method="html") # Full paragraph with formatting
This preserves:
- Lists (
<ul>, <ol>)
- Italics (
<i>, <em>)
- Bold (
<b>, <strong>)
5. Extract Images (Including Highlighted Ones)
a. Extract All Images from EPUB
EPUBs store images in OEBPS/images/.
You can extract them easily:
import zipfile
import os
with zipfile.ZipFile("book.epub") as epub:
for name in epub.namelist():
if name.startswith("OEBPS/images/"):
epub.extract(name, "output_folder/")
b. Detect Highlighted Images
This is tricky because there's no standard way to mark image highlights.
However, if you use apps like Kindle app or Libby, sometimes they generate annotation data that includes image IDs or positions.
Alternatively:
- Use OCR on image files to detect annotations (like circles, boxes, arrows).
- Or match image filenames against annotation metadata.
You could also build a UI where users tag which images they want to include.
6. Aggregate Everything into a New Book
Create a new EPUB or HTML document containing:
- Title page
- TOC of clippings
- Each clipping with context, formatting, and images
Use libraries like:
- Python:
ebooklib, BeautifulSoup, lxml
- Node.js:
epub-gen, puppeteer (for HTML-to-PDF)
- Go:
go-epub
Example using ebooklib:
from ebooklib import epub
book = epub.EpubBook()
book.set_title('My Clippings')
# Add CSS for styling
style = 'BODY { font-family: Times, serif; }'
nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)
book.add_item(nav_css)
# Add each clipping as a chapter
chapter = epub.EpubHtml(title='Chapter 1', file_name='chap_01.xhtml', lang='en')
chapter.content = '<h1>Clipping</h1><p>This is a <b>highlighted</b> list:</p><ul><li>First item</li></ul>'
book.add_item(chapter)
# Define Table of Contents
book.toc = (epub.Link('chap_01.xhtml', 'Clipping Chapter', 'id1'),)
# Add default NCX and Nav file
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
# Define spine
book.spine = ['nav', chapter]
# Write to file
epub.write_epub('my_clippings.epub', book, {})
7. Putting It All Together in an App
Tech Stack Suggestion:
- Backend: Python (Flask or FastAPI)
- Frontend: Web or desktop GUI (Tkinter, PyQt, Electron)
- Ebook Processing: Use
ebooklib, lxml, Pillow, calibre CLI
Features to Implement:
- Select folder of ebooks (EPUB/MOBI/AZW3)
- Convert MOBI/AZW3 to EPUB
- Parse highlights (via class names or Calibre DB)
- Preserve formatting
- Extract relevant images
- Generate new formatted clippings book (HTML/EPUB/PDF)
✅ Summary Checklist
| Task | Status |
|---|
| ✅ Convert MOBI/AZW3 to EPUB | ✔️ |
| ✅ Parse EPUB XHTML files | ✔️ |
| ✅ Locate and extract highlights | ✔️ |
| ✅ Preserve formatting (HTML fragments) | ✔️ |
| ✅ Extract embedded images | ✔️ |
| ✅ Match images to annotations (if possible) | ⚠️ (optional) |
| ✅ Assemble into new EPUB/HTML clippings book | ✔️ |
📦 Tools & Libraries
🧪 Sample Project Folder Structure
clippings_app/
│
├── input_books/
├── processed_epubs/
├── extracted_clippings/
├── output_clippings/
│ └── my_clippings.epub
├── main.py
├── parser.py
├── converter.py
└── builder.py
Would you like me to help you write a basic prototype script in Python to get started?