-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapters2book.py
75 lines (58 loc) · 1.75 KB
/
chapters2book.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
from ebooklib import epub
import os
import re
def sort_custom(x):
res = re.findall(r'\d+', x)
return int(res[0])
def create_book(name):
print(name)
book = epub.EpubBook()
book.set_identifier('5646546')
book.set_title(name.replace("_"," "))
book.set_language('en')
files = os.listdir(os.getcwd())
if any(".epub" in x for x in files):
files.remove(f"{name}.epub")
files= sorted(files,key=sort_custom)
content = []
for file in files:
if file[-5:]!=".text":
continue
with open(file , "r",encoding="utf-8") as f :
c = epub.EpubHtml(title=file[:-5], file_name=f'{file[:-5]}.xhtml', lang='en')
c.content=""
c.content+=f'<h2>{file[:-5]}</h2>'
for line in f.readlines():
c.content+=f'<p>{line}</p>'
book.add_item(c)
content.append(c)
book.toc.append(epub.Link(f'{file[:-5]}.xhtml', file[:-5], "id"+file[:-5]))
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
style = '''
@namespace epub "http://www.idpf.org/2007/ops";
body {
font-family: Cambria, Liberation Serif, Bitstream Vera Serif, Georgia, Times, Times New Roman, serif;
}
h2 {
text-align: left;
text-transform: uppercase;
font-weight: 200;
}
ol {
list-style-type: none;
}
ol > li:first-child {
margin-top: 0.3em;
}
nav[epub|type~='toc'] > ol > li > ol {
list-style-type:square;
}
nav[epub|type~='toc'] > ol > li > ol > li {
margin-top: 0.3em;
}
'''
nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)
book.add_item(nav_css)
book.spine = ['nav', *content]
epub.write_epub(f'{name}.epub', book, {})