Last active
December 10, 2023 19:41
-
-
Save hernamesbarbara/60a8ea2be1e52efd2eda69f9f15e5d9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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