-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebook_functions.py
81 lines (64 loc) · 2.61 KB
/
ebook_functions.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
from ebooklib import epub
from sys import platform
def createbook(book_path):
book = epub.EpubBook()
if platform == "linux" or platform == "linux2" or platform == "darwin":
book_title = book_path.split("/")[-1]
elif platform == "win32":
book_title = book_path.split("\\")[-1]
# set metadata
book.set_identifier('id123456')
book.set_title(book_title + ' - EPUB generator by AkaHitsuji')
book.set_language('en')
# set cover image
image_path = os.path.join(book_path,"cover_page.jpg")
if os.path.exists(image_path):
book.set_cover("cover_page.jpg", open(image_path, 'rb').read())
print("cover page set")
# retrieve chapter list and create chapters
cache_path = os.path.join(book_path,"chapter_list.txt")
list_chapters = []
if os.path.exists(cache_path):
f = open(cache_path, "r", encoding="utf8")
lines = f.readlines()
for line in lines:
title = line.split("_:_")[0]
link = line.split("_:_")[1].rstrip()
chapter_tuple = (title, link)
list_chapters.append(chapter_tuple)
f.close()
list_of_epub_chapters = []
for title, link in list_chapters:
chapter_filename = link.split("/")[-1]
chapter_path = os.path.join(book_path,title)
file = open(chapter_path, "r", encoding="utf8")
lines = file.readlines()
file.close()
chapter_content = ""
for line in lines:
chapter_content += "<br />" + line
epub_chapter = epub.EpubHtml(title=title, file_name=chapter_filename+".xhtml", lang="hr")
epub_chapter.content = '<head>\n<title>' + title + '</title>\n</head>\n<body>\n<strong>' + title + '</strong>\n<p>' + chapter_content + '</p>\n</body>\n</html>'
# add chapter
book.add_item(epub_chapter)
list_of_epub_chapters.append(epub_chapter)
print(title+" added to book.")
# define Table Of Contents
for chap in list_of_epub_chapters:
book.toc.append(chap)
# add default NCX and Nav file
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
# define CSS style
style = 'BODY {color: white;}'
nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)
# add CSS file
book.add_item(nav_css)
# basic spine
book.spine = ['nav']
for chap in list_of_epub_chapters:
book.spine.append(chap)
# write to the file
epub.write_epub(book_title + ' - EPUB generator by AkaHitsuji.epub', book)
print("epub for " + book_title + " created :) Have fun reading!!!")