ai-book-generator / utils.py
ashhadahsan's picture
Upload 7 files
c060b6c
import pandas as pd
import random
import markdown
import pdfkit
import platform
def get_book_genre(book_name: str) -> str:
df = pd.read_csv("Goodreads_books_with_genres.csv")
genre = df[df.Title.str.contains(book_name)]["genres"].values.tolist()[0].split(";")
genre = genre[random.randint(0, len(genre))]
return genre
def create_md(text, title):
with open(f"{title}.md", "w") as f:
f.write(f"# Title: {title}")
f.write(r"\n")
for x in range(len(text)):
f.write("\n")
f.write(f"## Chapter {x+1}")
f.write("\n")
f.write(text[x])
return title
def convert(filename):
with open(f"{filename}.md", "r") as f:
text = f.read()
html = markdown.markdown(text)
with open(f"{filename}.html", "w", encoding="utf-8") as f:
f.write(html)
return f"{filename}"
def to_pdf(filename):
if platform.system() == "Windows":
path_to_wkhtmltopdf = r"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"
# Point pdfkit configuration to wkhtmltopdf.exe
config = pdfkit.configuration(wkhtmltopdf=path_to_wkhtmltopdf)
# Convert HTML file to PDF
pdfkit.from_file(
f"{filename}.html", output_path=f"{filename}.pdf", configuration=config
)
return f"{filename}.pdf"
else:
pdfkit.from_file(
f"{filename}.html", output_path=f"{filename}.pdf", configuration=config
)
return f"{filename}.pdf"