File size: 1,508 Bytes
c060b6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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"