Skip to content

Instantly share code, notes, and snippets.

@hernamesbarbara
Last active December 10, 2023 19:41
Show Gist options
  • Save hernamesbarbara/60a8ea2be1e52efd2eda69f9f15e5d9f to your computer and use it in GitHub Desktop.
Save hernamesbarbara/60a8ea2be1e52efd2eda69f9f15e5d9f to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import subprocess
import os
# need to install Calibre e-book mgmt tool dependencies
# https://calibre-ebook.com/
# https://www.epubor.com/calibre-drm-removal-plugins.html
def kindle2pdf(kindle_file, output_directory):
"""
Convert Kindle ebook to PDF
"""
if not os.path.exists(kindle_file):
raise FileNotFoundError(f"{kindle_file} does not exist.")
base_name = os.path.splitext(os.path.basename(kindle_file))[0]
pdf_file = os.path.join(output_directory, base_name + '.pdf')
# call `ebook-convert` CLI tool from calibre
try:
subprocess.run(['ebook-convert', kindle_file, pdf_file], check=True)
except subprocess.CalledProcessError as e:
raise RuntimeError(f"Conversion failed: {e}")
return pdf_file
# Example usage
kindle = 'ebook.azw'
output_dir = './'
pdf = kindle2pdf(kindle, output_dir)
print(f"saved {pdf}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment