GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — #24 Make some function names more pythonic + update docs + update exa… · 06linux/ebooklib@64934cb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • ebooklib
    • epub.py
  • samples
    • 01_basic_create
      • create.py
    • 02_cover_create
      • create.py
    • 03_advanced_create
      • create.py
    • 04_markdown_parse
      • epub2markdown.py
  • tests
    • epub_booktype.py
    • epub_create.py
    • epub_parse.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎ebooklib/epub.py‎

Copy file name to clipboardExpand all lines: ebooklib/epub.py
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828

2929
from lxml import etree
3030

31-
32-
# this really should not be here
31+
# This really should not be here
3332
mimetypes.init()
3433

3534
# Version of EPUB library
@@ -931,7 +930,7 @@ def _load(self):
931930

932931
## WRITE
933932

934-
def writeEPUB(name, book, options = None):
933+
def write_epub(name, book, options = None):
935934
epub = EpubWriter(name, book, options)
936935

937936
epub.process()
@@ -943,7 +942,7 @@ def writeEPUB(name, book, options = None):
943942

944943
## READ
945944

946-
def readEPUB(name):
945+
def read_epub(name):
947946
reader = EpubReader(name)
948947

949948
book = reader.load()
Collapse file

‎samples/01_basic_create/create.py‎

Copy file name to clipboardExpand all lines: samples/01_basic_create/create.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,5 @@
8080
book.spine = ['nav', c1, c2]
8181

8282
# create epub file
83-
epub.writeEPUB('test.epub', book, {})
83+
epub.write_epub('test.epub', book, {})
8484

Collapse file

‎samples/02_cover_create/create.py‎

Copy file name to clipboardExpand all lines: samples/02_cover_create/create.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@
8484
book.spine = ['cover', 'nav', c1, c2]
8585

8686
# create epub file
87-
epub.writeEPUB('test.epub', book, {})
87+
epub.write_epub('test.epub', book, {})
8888

Collapse file

‎samples/03_advanced_create/create.py‎

Copy file name to clipboardExpand all lines: samples/03_advanced_create/create.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,5 @@
9393
book.spine = ['nav', c1, c2]
9494

9595
# create epub file
96-
epub.writeEPUB('test.epub', book, {})
96+
epub.write_epub('test.epub', book, {})
9797

Collapse file

‎samples/04_markdown_parse/epub2markdown.py‎

Copy file name to clipboardExpand all lines: samples/04_markdown_parse/epub2markdown.py
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
from ebooklib import epub
99

10+
# This is just a basic example which can easily break in real world.
11+
1012
if __name__ == '__main__':
1113
# read epub
12-
book = epub.readEPUB(sys.argv[1])
14+
book = epub.read_epub(sys.argv[1])
1315

1416
# get base filename from the epub
1517
base_name = os.path.basename(os.path.splitext(sys.argv[1])[0])
Collapse file

‎tests/epub_booktype.py‎

Copy file name to clipboardExpand all lines: tests/epub_booktype.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def export_booktype(bookid):
214214

215215

216216
# write book to the file
217-
epub.writeEPUB('test.epub', book, opts)
217+
epub.write_epub('test.epub', book, opts)
218218

219219

220220
if __name__ == '__main__':
Collapse file

‎tests/epub_create.py‎

Copy file name to clipboardExpand all lines: tests/epub_create.py
+22-13Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,33 @@
1111
book.set_title('This is my book')
1212
book.set_language('fi')
1313

14+
book.add_metadata('DC', 'title', 'Naslov', {'id': 't1'})
15+
book.add_metadata(None, 'meta', 'main', {'refines': '#t1', 'property': 'title-type'})
16+
book.add_metadata('DC', 'title', 'Naslov', {'id': 't2'})
17+
book.add_metadata(None, 'meta', 'edition', {'refines': '#t2', 'property': 'title-type'})
18+
1419
book.add_author('Mirko Bulaja')
1520
book.add_author('Danko Bananko', file_as='Gospodin Danko Bananko', role='ill', uid='coauthor')
1621

17-
img = open('cover.jpg', 'r').read()
22+
img = open('image.jpg', 'r').read()
1823
book.set_cover("image.jpg", img)
1924

2025
# create chapters
2126
c1 = epub.EpubHtml(title='Intro 1', file_name='chap_01.xhtml', lang='hr')
22-
c1.content=u'<html><head></head><body><h1>This is I!</h1><p>This is something i am writing.</p><p>Šime Đodan ima puno posla.</p></body></html>'
27+
c1.set_content(u'<html><head></head><body><h1>This is I!</h1><p>This is something i am writing.</p><p>Šime Đodan ima puno posla.</p></body></html>')
2328

24-
c2 = epub.EpubHtml(title='Intro 2', file_name='chap_02.xhtml')
25-
# c2.content='<html><head></head><body><h1>This is I!</h1><p>Helou, helou!</p><p><img src="static/image.jpg"/><a href="http://ajme/">Ovo je link</a></body></html>'
26-
c2.content='<h1>This is I!</h1><p>Helou, helou!</p><p><img src="image.jpg"/><a href="http://ajme/">Ovo je link</a></p>'
29+
c2 = epub.EpubHtml(title='Intro 2', file_name=u'chap_02_đšž.xhtml')
30+
c2.set_content('<h1>This is I!</h1><p>Helou, helou!</p><p><a href="http://ajme/">Ovo je link</a></p>')
2731
c2.properties.append('rendition:layout-pre-paginated rendition:orientation-landscape rendition:spread-none')
2832

2933
# add all the items
3034
book.add_item(c1)
3135
book.add_item(c2)
3236

33-
# book.toc = (epub.Link('cover.xhtml', 'Cover', 'cover'),
34-
book.toc = (epub.Link('chap_02.xhtml', 'Introduction', 'intro'),
35-
(epub.Section('Languages'),
36-
(c1, c2))
37+
book.toc = ( (epub.Section('Languages'),
38+
(c1, c2)),
3739
)
3840

39-
# this should be maybe add by default or something
40-
book.add_item(epub.EpubNcx())
41-
book.add_item(epub.EpubNav())
4241

4342
style = '''
4443
@namespace epub "http://www.idpf.org/2007/ops";
@@ -75,9 +74,19 @@
7574
nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)
7675
book.add_item(nav_css)
7776

77+
# this should be maybe add by default or something
78+
book.add_item(epub.EpubNcx())
79+
nav = epub.EpubNav()
80+
nav.add_item(nav_css)
81+
book.add_item(nav)
82+
7883
# this probably is not correct. should be able to define other properties also
7984
book.spine = ['cover', 'nav', c1, c2]
8085

8186
# write book to the file
82-
epub.writeEPUB('test.epub', book, {})
87+
epub.write_epub('test.epub', book)
88+
89+
print '------------'
90+
for x in nav.get_links_of_type('text/css'):
91+
print x
8392

Collapse file

‎tests/epub_parse.py‎

Copy file name to clipboardExpand all lines: tests/epub_parse.py
+8-7Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
from ebooklib import epub
44

5+
book = epub.read_epub(sys.argv[1])
56

6-
book = epub.readEPUB(sys.argv[1])
7-
8-
print book.metadata
9-
7+
epub.debug(book.metadata)
8+
epub.debug(book.spine)
9+
epub.debug(book.toc)
1010
for it in book.items:
11-
print it
11+
epub.debug( it.file_name)
12+
13+
for x in book.get_items_of_type(epub.ITEM_IMAGE):
14+
print x
1215

13-
book.add_item(epub.EpubNav())
14-
epub.writeEPUB('test.epub', book, {})
1516

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.