打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python生成epub文档
开始之前

本教程讲述如何创建 EPUB 格式的电子图书。EPUB 是一种基于 XML 的、对开发者友好的格式,正逐渐成为数字图书的事实标准。但 EPUB 不仅可用于图书,还包括:

  • 对文档打包以便离线阅读或者分发
  • 打包博客文章或者其他 Web 内容
  • 使用常见的开放源代码工具创建、搜索和整理

解剖 EPUB 包

一个 EPUB 就是一个简单 ZIP 格式文件(使用 .epub 扩展名),其中包括按照预先定义的方式排列的文件。

简单 EPUB 档案的目录和文件结构
  1. mimetype
  2. META-INF/
  3. container.xml
  4. OEBPS/
  5. content.opf
  6. title.html (可忽略)
  7. content.html (可忽略)
  8. stylesheet.css
  9. toc.ncx
  10. images/ (可忽略)
  11. cover.png
mimetype 文件

这个文件非常简单,必须命名为 mimetype,文件内容如下:

application/epub+zip

要注意,mimetype 文件不能包含新行或者回车。
此外,mimetype 文件必须作为 ZIP 档案中的第一个文件,而且自身不能压缩。首先创建该文件并保存,并确保它在 EPUB 项目的根目录中。

META-INF/container.xml

EPUB 根目录下必须包含 META-INF 目录,而且其中要有一个文件 container.xml。EPUB 阅读系统首先查看该文件,它指向数字图书元数据的位置。

  1. <?xml version='1.0' encoding='utf-8' standalone='no'?>
  2. <container version='1.0' xmlns='urn:oasis:names:tc:opendocument:xmlns:container'>
  3. <rootfiles>
  4. <rootfile full-path='OEBPS/content.opf' media-type='application/oebps-package+xml'/>
  5. </rootfiles>
  6. </container>
OEBPS/content.opf

指定了图书中所有 内容的位置,如文本和图像等其他媒体。它还给出了另一个元数据文件,内容的 Navigation Center eXtended (NCX) 表。该 OPF 文件是 EPUB 规范中最复杂的元数据。

  1. <?xml version='1.0' encoding='utf-8' standalone='no'?>
  2. <package xmlns='http://www.idpf.org/2007/opf'
  3. xmlns:dc='http://purl.org/dc/elements/1.1/'
  4. unique-identifier='bookid' version='2.0'>
  5. <metadata>
  6. <dc:title>Hello World: My First EPUB</dc:title>
  7. <dc:creator>svoid</dc:creator>
  8. <dc:identifier id='bookid'>urn:uuid:12345</dc:identifier>
  9. <meta name='cover' content='cover-image' />
  10. </metadata>
  11. <manifest>
  12. <item id='ncx' href='toc.ncx' media-type='text/xml'/>
  13. <item id='cover' href='title.html' media-type='application/xhtml+xml'/>
  14. <item id='content' href='content.html' media-type='application/xhtml+xml'/>
  15. <item id='cover-image' href='images/cover.png' media-type='image/png'/>
  16. <item id='css' href='stylesheet.css' media-type='text/css'/>
  17. </manifest>
  18. <spine toc='ncx'>
  19. <itemref idref='cover' linear='no'/>
  20. <itemref idref='content'/>
  21. </spine>
  22. <guide>
  23. <reference href='cover.html' type='cover' title='Cover'/>
  24. </guide>
  25. </package>
内容的 NCX 表

NCX 定义了数字图书的目录表。复杂的图书中,目录表通常采用层次结构,包括嵌套的内容、章和节。

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE ncx PUBLIC '-//NISO//DTD ncx 2005-1//EN'
  3. 'http://www.daisy.org/z3986/2005/ncx-2005-1.dtd'>
  4. <ncx xmlns='http://www.daisy.org/z3986/2005/ncx/' version='2005-1'>
  5. <head>
  6. <meta name='dtb:uid' content='urn:uuid:12345'/>
  7. <meta name='dtb:depth' content='1'/>
  8. <meta name='dtb:totalPageCount' content='0'/>
  9. <meta name='dtb:maxPageNumber' content='0'/>
  10. </head>
  11. <docTitle>
  12. <text>Hello World: My First EPUB</text>
  13. </docTitle>
  14. <navMap>
  15. <navPoint id='navpoint-1' playOrder='1'>
  16. <navLabel>
  17. <text>Book cover</text>
  18. </navLabel>
  19. <content src='title.html'/>
  20. </navPoint>
  21. <navPoint id='navpoint-2' playOrder='2'>
  22. <navLabel>
  23. <text>Contents</text>
  24. </navLabel>
  25. <content src='content.html'/>
  26. </navPoint>
  27. </navMap>
  28. </ncx>
添加最后的内容

现在知道了 EPUB 需要的所有元数据,可以加入真正的图书内容了。可以使用下载的内容,也可以自己写,只要文件名和元数据匹配即可。然后创建下列文件和文件夹:
title.html:图书的标题页。创建该文件并在其中包含引用封面图片的 img 元素,src 的属性值为 images/cover.png。
images:在 OEBPS 下创建该文件夹,然后复制给定的示例图片(或者创建自己的图片)并命名为 cover.png。
content.html:图书的实际文字内容。
stylesheet.css:将该文件放在和 XHTML 文件相同的 OEBPS 目录中。该文件可以包含任意 CSS 声明,比如设置字体或者文字颜色。

Python生成epub的简单实例

  1. # encoding:utf-8
  2. #!/usr/bin/python3
  3. import zipfile
  4. import os.path
  5. '''
  6. Script Name : createEpubBook.py
  7. Author : svoid
  8. Created : 2015-03-28
  9. Last Modified :
  10. Version : 1.0
  11. Modifications :
  12. Description : 根据HTML生成epub文档
  13. '''
  14. def create_mimetype(epub):
  15. epub.writestr('mimetype','application/epub+zip',compress_type=zipfile.ZIP_STORED)
  16. def create_container(epub):
  17. container_info = '''<?xml version='1.0' encoding='utf-8' standalone='no'?>
  18. <container version='1.0' xmlns='urn:oasis:names:tc:opendocument:xmlns:container'>
  19. <rootfiles>
  20. <rootfile full-path='OEBPS/content.opf' media-type='application/oebps-package+xml'/>
  21. </rootfiles>
  22. </container>
  23. '''
  24. epub.writestr('META-INF/container.xml',container_info, compress_type=zipfile.ZIP_STORED)
  25. def create_content(epub,path):
  26. content_info = '''
  27. <?xml version='1.0' encoding='utf-8' standalone='no'?>
  28. <package xmlns='http://www.idpf.org/2007/opf'
  29. xmlns:dc='http://purl.org/dc/elements/1.1/'
  30. unique-identifier='bookid' version='2.0'>
  31. <metadata>
  32. <dc:title>Hello World: My First EPUB</dc:title>
  33. <dc:creator>Svoid</dc:creator>
  34. <dc:identifier id='bookid'>urn:uuid:12345</dc:identifier>
  35. <meta name='cover' content='cover-image' />
  36. </metadata>
  37. <manifest>
  38. %(manifest)s
  39. <item id='ncx' href='toc.ncx' media-type='text/xml'/>
  40. <item id='content' href='content.html' media-type='application/xhtml+xml'/>
  41. <item id='css' href='stylesheet.css' media-type='text/css'/>
  42. </manifest>
  43. <spine toc='ncx'>
  44. %(spine)s
  45. <itemref idref='cover' linear='no'/>
  46. <itemref idref='content'/>
  47. </spine>
  48. <guide>
  49. <reference href='cover.html' type='cover' title='Cover'/>
  50. </guide>
  51. </package>
  52. '''
  53. manifest = ''
  54. spine = ''
  55. for html in os.listdir(path):
  56. basename = os.path.basename(html)
  57. if basename.endswith('html'):
  58. manifest += '<item id='%s' href='%s' media-type='application/xhtml+xml'/>' % (basename, basename)
  59. spine += '<itemref idref='%s'/>' % (basename)
  60. epub.writestr('OEBPS/content.opf',content_info % {
  61. 'manifest': manifest,
  62. 'spine': spine,},
  63. compress_type=zipfile.ZIP_STORED)
  64. def create_stylesheet(epub):
  65. css_info = '''
  66. body {
  67. font-family: sans-serif;
  68. }
  69. h1,h2,h3,h4 {
  70. font-family: serif;
  71. color: red;
  72. }
  73. '''
  74. epub.writestr('OEBPS/stylesheet.css',css_info,compress_type=zipfile.ZIP_STORED)
  75. def create_archive(path):
  76. epub_name = '%s.epub' % os.path.basename(path)
  77. os.chdir(path)
  78. epub = zipfile.ZipFile(epub_name, 'w')
  79. create_mimetype(epub)
  80. create_container(epub)
  81. create_content(epub,path)
  82. create_stylesheet(epub)
  83. for html in os.listdir('.'):
  84. basename = os.path.basename(html)
  85. if basename.endswith('html'):
  86. epub.write(html, 'OEBPS/'+basename, compress_type=zipfile.ZIP_DEFLATED)
  87. epub.close()
  88. if __name__ == '__main__':
  89. path = 'D:\\persion\\epub\\test1'
  90. create_archive(path)

参考文档:
http://www.ibm.com/developerworks/cn/xml/tutorials/x-epubtut/index.html
http://www.manuel-strehl.de/dev/simple_epub_ebooks_with_python.en.html

整理自网络

Svoid
2015-03-28

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
手把手教你怎么制作epub电子书
使用sigil制作epub格式电子书
web前端开发之版本控制(缓存处理)
epub文件格式介绍
Jmeter查看结果树之查看响应的13种方法[详解]
Docbook写作指南
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服