Skip to content

Instantly share code, notes, and snippets.

@adithyabsk
Last active December 30, 2023 06:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save adithyabsk/eb5e6fab7974a57ae52dbde5537790f4 to your computer and use it in GitHub Desktop.
Save adithyabsk/eb5e6fab7974a57ae52dbde5537790f4 to your computer and use it in GitHub Desktop.
Convert iBook EPUBs to standard EPUBs

How to Convert iBook EPUBs to standard EPUBs

iBooks EPUBs actually show up on macOS as folders. If you need the actual file you can use the above script to convert your iBook directory of files to a destination directory.

The path of iBook files as of macOS 10.15.7 is: /Users/${USER}/Library/Mobile Documents/iCloud~com~apple~iBooks/Documents

Usage

mkdir converted
python convert_ibook.py "/Users/${USER}/Library/Mobile Documents/iCloud~com~apple~iBooks/Documents" converted

Sources

#!/usr/bin/env python
from pathlib import Path
import argparse
import subprocess
import os
from pprint import pprint
parser = argparse.ArgumentParser()
parser.add_argument("input", help="Directory path to search for epub files.", type=Path)
parser.add_argument("output", help="Directory to write epub files to.", type=Path)
args = parser.parse_args()
# Check input and output exist
if not args.input.is_dir() or not args.output.is_dir():
raise Exception("Either input or output does not exist.")
book_paths = list(args.input.glob("*.epub"))
print(f"Found {len(book_paths)} epub files.")
output_dir = args.output.resolve()
for book_path in book_paths:
out_path = "\""+str(output_dir / book_path.name)+"\""
print(f"Converted {book_path.name}")
os.chdir(book_path)
subprocess.call(
f"zip -X -r {out_path} mimetype *",
shell=True,
stdout=subprocess.DEVNULL
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment