Learn how to Convert EPUB to PDF in Python
Last active
April 18, 2025 03:51
-
-
Save aspose-com-gists/c856dd5788c2f9be851799b32ed3a13e to your computer and use it in GitHub Desktop.
Convert EPUB to PDF in Python
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
import aspose.pdf as ap | |
# Instantiate LoadOption object using EPUB load option | |
option = ap.EpubLoadOptions() | |
# Load an EPUB file | |
document = ap.Document("sample.epub", option) | |
# Save the document in PDF format | |
document.save("epub-to-pdf.pdf") |
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
import aspose.pdf as ap | |
import os | |
input_dir = "D:\\Files\\" | |
output_dir = "D:\\Files\\sample_out\\" | |
# Ensure output directory exists | |
os.makedirs(output_dir, exist_ok=True) | |
# Loop through all EPUB files and convert each to PDF | |
for filename in os.listdir(input_dir): | |
if filename.endswith(".epub"): | |
epub_path = os.path.join(input_dir, filename) | |
pdf_output = os.path.join(output_dir, filename.replace(".epub", ".pdf")) | |
print(epub_path) | |
option = ap.EpubLoadOptions() | |
doc = ap.Document(epub_path, option) | |
doc.save(pdf_output) |
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
import aspose.pdf as ap | |
# Instantiate LoadOption object using EPUB load option | |
option = ap.EpubLoadOptions() | |
# Load an EPUB file | |
document = ap.Document("sample.epub", option) | |
# Set custom page margins (in points) | |
for page in document.pages: | |
page.page_info.margin = ap.MarginInfo(50, 50, 50, 50) # left, right, top, bottom | |
# Font embedding | |
document.embed_standard_fonts = True | |
# Add metadata like title, author, and secure the PDF: | |
document.info.title = "Converted eBook" | |
document.info.author = "Generated with Aspose.PDF" | |
document.encrypt("userpass", "ownerpass", ap.Permissions.PRINT_DOCUMENT, ap.CryptoAlgorithm.AE_SX128) | |
# Save the document in PDF format | |
document.save("epub-to-pdf2.pdf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment