Skip to content

Instantly share code, notes, and snippets.

@dcalacci
Created August 10, 2022 16:05
Show Gist options
  • Save dcalacci/9e70fd6ea662680c3bbbbcd9cac43055 to your computer and use it in GitHub Desktop.
Save dcalacci/9e70fd6ea662680c3bbbbcd9cac43055 to your computer and use it in GitHub Desktop.
Convert iBooks to normal pub packages, maintaining metadata
import os
import subprocess
filenames = []
# old path with ibooks
oldpath = "/path/to/ibooks/"
# Path to store new epub files in
newpath = "/new/path/for/epubs/"
# Function to store all filenames in a list
def extract_filename(path_to_files=oldpath):
os.chdir(path_to_files)
books = os.getcwd()
path_to_files = path_to_files
for f in os.listdir(books):
f_name, f_ext = os.path.splitext(f)
if f_ext == ".epub":
filenames.append(f_name)
filenames.sort()
# Function to generate new epub files
def create_epub(path_to_new_files=newpath):
for n,f in enumerate(filenames):
cd_cmd = f"cd '{oldpath}{f}.epub'"
rm_plist_cmd = f"rm {oldpath}iTunesMetadata.plist"
zip_cmd = f"zip -X -r '{path_to_new_files}{f}.epub' mimetype *"
all_cmd = "; ".join([cd_cmd, rm_plist_cmd, zip_cmd])
p1 = subprocess.run(all_cmd, capture_output = True, text = True, shell = True)
success = p1.returncode
if success == 0:
print(f'File {f[:10]}...{f[-5:]}.epub has been processed successfully. \n> {len(filenames)-n} to go.')
else:
print("Error:", p1.stdout)
print(p1.stderr)
# Enter the paths
extract_filename()
print(f"Converting {len(filenames)} epubs:", filenames)
create_epub()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment