import os
from ebooklib import epub

# Set your directory path
html_dir = r"COPY FOLDER PATH TO HTML HERE"
output_dir = os.path.join(html_dir, "epub")
os.makedirs(output_dir, exist_ok=True)

# Author name
author_name = "Becca Bellamy"

# Loop through HTML files
for filename in os.listdir(html_dir):
    if filename.lower().endswith(".html"):
        base_name = os.path.splitext(filename)[0]
        file_path = os.path.join(html_dir, filename)

        # Read HTML content
        with open(file_path, 'r', encoding='utf-8') as f:
            html_content = f.read()

        # Create EPUB book
        book = epub.EpubBook()
        book.set_title(base_name)
        book.add_author(author_name)

        # Create EPUB chapter
        chapter = epub.EpubHtml(title=base_name, file_name=base_name + '.xhtml', content=html_content)
        book.add_item(chapter)
        book.spine = ['nav', chapter]
        book.add_item(epub.EpubNcx())
        book.add_item(epub.EpubNav())

        # Output EPUB file
        output_path = os.path.join(output_dir, base_name + ".epub")
        epub.write_epub(output_path, book)
        print(f"✅ Converted: {filename} → {base_name}.epub")
