/ nat / blog

Tag Archives: epub

How to make an epub / mobi file

October 31, 2011 – 2:18 pm

You want to convert your Word Doc to an Epub or Mobi file with a table of contents and chapter breaks for easy eReader navigation & readability. No problem.

      Short Answer
  1. In your word document:
      set doc title & chapter titles to “Header 1” & save as *.html
  2. In Calibre:
      Calibre>Convert books>Look & Feel>Remove spacing between paragraphs
      Calibre>Convert books>Table of Contents>Level 1 TOC (XPath expression)://h:h1
      Long Answer

You must format your word document and save as html. Then use Calibre to convert the html document to epub and/or mobi and tell it that everything formatted as “Header 1” deserves to be in the auto-generated table of contents. I’ll explain.

      Formatting your document

You can use LibreOffice, OpenOffice, or Word, it’s all the same. I’m running Windows 7, OpenOffice 3.3, Calibre 0.8.24, but this information is generic. If you open a new “Word” doc and type out some text, then what you see is the default unformatted style. It might look like this:

style: default

Notice that the above document has a main title at the top of the first page and what we might call a chapter title (in all caps.) Highlight your main title, select “Format>Styles and Formatting” or the equivalent option for changing a “style” in your client, then select from the list of style options: “Heading 1” as shown here:

style: Header 1

Repeat this step for every chapter title and save your document as *.html with the “Save As” option. The trickiest part of formatting your document is finding the “Heading 1” style option for your particular word processor. After you’ve saved as html, then you upload it into Calibre.

      Converting with Calibre

Download and install Calibre. Open it and you’ll see something like this:

Calibre

Begin with the big red “Add books” button in the upper left and select the html version of the document you just saved. Once loaded into Calibre, hit the “Edit metadata” button and enter the title as it should be, this is the title you’ll see listed when you load it onto your ereader.

Calibre: Edit Metadata

Now hit “OK” to exit back to the main screen and hit “Convert books,” this is where the action is.

The “Convert books” window pops up. In the upper right corner of this screen find the “Output format” menu, select MOBI or EPUB. You can’t do both at once, but you can do one then come back and do the other.

Calibre: Convert

The left side menu has several items, but you’ll only need “Look & Feel” and “Table of Contents.” In “Look & Feel” select “Remove spacing between paragraphs.”

Calibre: Look & Feel

In “Table of Contents” select the wizard button to the right of “Level 1 TOC (XPath expression):”, in the new window that pops up, open the drop down menu labeled “Match HTML tags with tag name:” and from that list select “h1” then hit “OK.” You’ll now see “//h:h1” listed in the Level 1 TOC field.

Calibre: Table of Contents

Hit “OK” and Calibre will immediately begin converting.

Conversion should take a few seconds. If you converted to MOBI the first time, then hit the “Convert books” button again and select EPUB to convert to that format. Then you’ll want to hit the big “Save to disk” button. Actually, saving is a bit unorthodox and worth a mention.

Calibre: Save

What you’re saving is actually a directory (author’s name) with a set of files (all their works.) Because off this, you’ll want to save all your Calibre conversions into a super-directory called “authors” or some such. If you embiggen the picture above you’ll see our location is

    Documents>My Documents>My eBooks>authors

This is where you want to be, with the “Folder” field at the bottom of the window blank when you press the “Select Directory” button. It’s kind of weird, because it feels like you are not actually selecting a directory. But what happens is that Calibre will search this directory for the author’s name and either save to that author’s directory if it’s already been created, or create it if it’s not yet there. Sounds confusing, but I promise you this is a very cool feature.

Play with it and you’ll see what I mean.

You’ve formatted your document and saved as *.html. You’ve used Calibre to convert it to an epub and/or mobi file. Now you just need to email that file to your ereader account, or manually upload it, or use your preferred method to get that file onto your ereader, and you’re done.

For the record, I will not provide any technical help or answer any tech questions regarding the above information, so please do not ask. I’ve put it all out there, that’s all I know. If you have questions regarding information not covered above, then I recommend searching on Google and asking in forums. Good luck.


UPDATE 2012-03-19: Python script to read your ebook directories

Over time I’ve cobbled together a Python script to keep my library straight. In its current version the script will (create, display, print, save) a list of all my ebook authors and titles. While taking a break from the word mines this afternoon, I figured I’d share this gem with you. Savvy hackers can port this Python code to their preferred language and OS; as is it’s a Windows-Python script.

After following directions at how to make an epub file, your directory structure should resemble this…
…\My eBooks\
…\My eBooks\_authors\
…\My eBooks\_authors\authorName\
…\My eBooks\_authors\authorName\bookTitle\
…\My eBooks\_authors\authorName\bookTitle\bookTitle.epub
…\My eBooks\_authors\authorName\bookTitle\bookTitle.mobi

With Python installed at “C:\Python\” (note: for users with 64-bit systems, the 32-bit version of all software is recommended,) copy-n-paste the following code into a text file, name it “_updateBookList.py” (or whatever.py) and save it to your _tools directory…
…\My eBooks\
…\My eBooks\_tools\
…\My eBooks\_tools\_updateBookList.py
Double click your *.py file to get started.

toggle code: _updateBookList.py

# _updateBookList.py by Nat Weinham; free to share or use for any purpose.
#
# /My eBooks/_tools/_updateBookList.py
# This script creates an "ebook authors & titles" list
# -a- create list
# -b- view list in newly created text file
# -c- send list directly to printer

import os, time, win32api, win32print               #import packages
nowTime = time.asctime(time.localtime(time.time())) #set time
ebooks = [str()]                                    #create string array
ebooks = [[s for s in x] for x in ebooks]           #define 2d array range
#"ebooks" array looks like: [[author, title, title],[next author, title]]
autFile = "_authors.txt"                            #set file name
a = 0                                               #count authors
t = 0                                               #count titles

def makeList():                     #loop through ebook dir & count dirs
    global a                            #count authors
    global t                            #count titles
    path = '../authors/'                #set path to ebooks (adjust as needed)
    i = 0                                  
    for dirName in os.listdir(path):    #for every author...
        ebooks.append([dirName.upper()])    #append new author
        i=i+1                               #increment array
        a=a+1                               #count authors
        for root, dirs, files in os.walk(path+dirName):
            for d in dirs:              #for every title...
                ebooks[i].append(d.title()) #append new title
                t=t+1                       #count titles

def writeList():                    #write author/title list to file
    file = open(autFile,"w")
    file.write('                                         '+nowTime+'\n')
    file.write('total authors '+str(a)+'\ntotal titles  '+str(t)+'\n\n')
    g = 0               #each 'g' contains one author & all his titles
    while g < len(ebooks):          #while there are authors...
        f = 0           #'f' iterates through items in the 'g' data set
        while f < len(ebooks[g]):   #loop through their titles.
            if f < 1:   #item f=0 is author name
                file.write(ebooks[g][f].upper()+'\n')         #write author
            else:       #items f>0 are titles
                file.write('     '+ebooks[g][f].title()+'\n') #write title
            f = f+1     #increment to next title
        g = g+1         #increment to next author/title data set
    file.close()

def viewList():
    os.startfile(autFile)

def printList(f):                  
    open (f,"r")
    win32api.ShellExecute (0,"print",f,'/d:"%s"' % win32print.GetDefaultPrinter(),".",0)

###########################################################################
makeList()                  #loop through directory & count items
writeList()                 #write author / title list to txt file
###########################################################################

p = raw_input(
    '\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n'+
    '\n '+nowTime+'\n\n'+
    ' ebook list created in file \''+autFile+'\'\n\n'+
    ' view list   (v+enter)\n'+
    ' print list  (p+enter)\n'+
    ' to exit hit (enter)\n\n'+
    ' authors: '+str(a)+'\n'+
    ' titles:  '+str(t)+'\n\n'+
    '\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n'+
    '')
if p.upper()=='P':
    printList(autFile)
elif p.upper()=='V':
    viewList();
elif p.upper()=='':
    exit()

In a nutshell, this code surfs your specified directory, builds a list of the contents, saves the list to a text file which you can view or print.


_updateBookList.py

March 19, 2012 – 10:10 pm

With the arrival of ebooks I’ve been saving some of my stories as epub files using the directions I outlined in a previous post. Over time I’ve cobbled together a Python script to keep my library straight. In its current version the script will (create, display, print, save) a list of all my ebook authors and titles. While taking a break from the word mines this afternoon, I figured I’d share this gem with you. Savvy hackers can port this Python code to their preferred language and OS; as is it’s a Windows-Python script.

After following directions at how to make an epub file, your directory structure should resemble this…
…\My eBooks\
…\My eBooks\_authors\
…\My eBooks\_authors\authorName\
…\My eBooks\_authors\authorName\bookTitle\
…\My eBooks\_authors\authorName\bookTitle\bookTitle.epub
…\My eBooks\_authors\authorName\bookTitle\bookTitle.mobi

With Python installed at “C:\Python\” (note: for users with 64-bit systems, the 32-bit version of all software is recommended,) copy-n-paste the following code into a text file, name it “_updateBookList.py” (or whatever.py) and save it to your _tools directory…
…\My eBooks\
…\My eBooks\_tools\
…\My eBooks\_tools\_updateBookList.py
Double click your *.py file to get started.

toggle code: _updateBookList.py

# _updateBookList.py by Nat Weinham; free to share or use for any purpose.
#
# /My eBooks/_tools/_updateBookList.py
# This script creates an "ebook authors & titles" list
# -a- create list
# -b- view list in newly created text file
# -c- send list directly to printer

import os, time, win32api, win32print               #import packages
nowTime = time.asctime(time.localtime(time.time())) #set time
ebooks = [str()]                                    #create string array
ebooks = [[s for s in x] for x in ebooks]           #define 2d array range
#"ebooks" array looks like: [[author, title, title],[next author, title]]
autFile = "_authors.txt"                            #set file name
a = 0                                               #count authors
t = 0                                               #count titles

def makeList():                     #loop through ebook dir & count dirs
    global a                            #count authors
    global t                            #count titles
    path = '../authors/'                #set path to ebooks (adjust as needed)
    i = 0                                  
    for dirName in os.listdir(path):    #for every author...
        ebooks.append([dirName.upper()])    #append new author
        i=i+1                               #increment array
        a=a+1                               #count authors
        for root, dirs, files in os.walk(path+dirName):
            for d in dirs:              #for every title...
                ebooks[i].append(d.title()) #append new title
                t=t+1                       #count titles

def writeList():                    #write author/title list to file
    file = open(autFile,"w")
    file.write('                                         '+nowTime+'\n')
    file.write('total authors '+str(a)+'\ntotal titles  '+str(t)+'\n\n')
    g = 0               #each 'g' contains one author & all his titles
    while g < len(ebooks):          #while there are authors...
        f = 0           #'f' iterates through items in the 'g' data set
        while f < len(ebooks[g]):   #loop through their titles.
            if f < 1:   #item f=0 is author name
                file.write(ebooks[g][f].upper()+'\n')         #write author
            else:       #items f>0 are titles
                file.write('     '+ebooks[g][f].title()+'\n') #write title
            f = f+1     #increment to next title
        g = g+1         #increment to next author/title data set
    file.close()

def viewList():
    os.startfile(autFile)

def printList(f):                  
    open (f,"r")
    win32api.ShellExecute (0,"print",f,'/d:"%s"' % win32print.GetDefaultPrinter(),".",0)

###########################################################################
makeList()                  #loop through directory & count items
writeList()                 #write author / title list to txt file
###########################################################################

p = raw_input(
    '\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n'+
    '\n '+nowTime+'\n\n'+
    ' ebook list created in file \''+autFile+'\'\n\n'+
    ' view list   (v+enter)\n'+
    ' print list  (p+enter)\n'+
    ' to exit hit (enter)\n\n'+
    ' authors: '+str(a)+'\n'+
    ' titles:  '+str(t)+'\n\n'+
    '\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n'+
    '')
if p.upper()=='P':
    printList(autFile)
elif p.upper()=='V':
    viewList();
elif p.upper()=='':
    exit()

In a nutshell, this code surfs your specified directory, builds a list of the contents, saves the list to a text file which you can view or print. And that’s just how I roll.