EPubIMG

From XPUB & Lens-Based wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Generating an ePub filled with images

For this script to work you need to run it next to the attached ePub skeleton (unzipped...).

#!/usr/bin/python
import os, shutil

# Remove previous epub files
if os.path.isdir("/tmp/epub"):
    shutil.rmtree("/tmp/epub")

# Copy ePub skeleton
shutil.copytree("epub-raw-files", "/tmp/epub")

# Copy images
for image in os.listdir("7240846"):
    shutil.copyfile("7240846/" + image, "/tmp/epub/OEBPS/images/" + image)

### Update the container's file listing
# Create item list
imageitems = ""
for image in os.listdir("/tmp/epub/OEBPS/images/"):
    if image != "cover.png":
        imageitems += '<item id="' + image + '" href="images/' + image  +'" media-type="image/' + image.split('.')[-1]  + '"/>'

# Add images to listing
opf = open("/tmp/epub/OEBPS/content.opf", "w")

content = """<?xml version='1.0' encoding='utf-8'?>
<package xmlns="http://www.idpf.org/2007/opf" 
            xmlns:dc="http://purl.org/dc/elements/1.1/" 
            unique-identifier="bookid" version="2.0">
  <metadata>
    <dc:title>Pwetty Pictures</dc:title>
    <dc:creator>Anonymous</dc:creator>
    <dc:identifier id="bookid">urn:uuid:12345</dc:identifier>
    <dc:language>en-US</dc:language>
    <meta name="cover" content="cover-image" /> 
  </metadata>
  <manifest>
    <item id="ncx" href="toc.ncx" media-type="text/xml"/>
    <item id="cover" href="title.html" media-type="application/xhtml+xml"/>
    <item id="content" href="content.html" media-type="application/xhtml+xml"/>
    <item id="cover-image" href="images/cover.png" media-type="image/png"/>
    <item id="css" href="stylesheet.css" media-type="text/css"/>
""" + imageitems + """
  </manifest>
  <spine toc="ncx">
    <itemref idref="cover" linear="no"/>
    <itemref idref="content"/>
  </spine>
  <guide>
    <reference href="title.html" type="cover" title="Cover"/>
  </guide>
</package>"""

opf.write(content)

### Place the images in the book
# Create item list
imageitems = ""
for image in os.listdir("/tmp/epub/OEBPS/images/"):
    if image != "cover.png":
        imageitems += '<img src="images/' + image  +'"/>'

# Add images in main html file
html = open("/tmp/epub/OEBPS/content.html", "w")

content = """<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Pwetty Pictures!</title>
    <link type="text/css" rel="stylesheet" media="all" href="stylesheet.css" />
  </head>
  <body>
    <h1>Today's selection</h1>
    <div style="text-align:center">""" + imageitems + """</div>
  </body>
</html>"""

html.write(content)

print "done - container ready"


cd /tmp/epub
zip -0Xq img.epub mimetype
zip -Xr9Dq img.epub *


Attachments