├── .gitignore ├── resource ├── mimetype ├── page_styles.css ├── META-INF │ └── container.xml ├── toc.ncx ├── content.opf ├── index.html └── stylesheet.css ├── main.py ├── zipFile.py ├── README.md ├── htmlcl.py └── html2epub.py /.gitignore: -------------------------------------------------------------------------------- 1 | .IDEA 2 | 3 | *.pyc -------------------------------------------------------------------------------- /resource/mimetype: -------------------------------------------------------------------------------- 1 | application/epub+zip -------------------------------------------------------------------------------- /resource/page_styles.css: -------------------------------------------------------------------------------- 1 | @page { 2 | margin-bottom: 5pt; 3 | margin-top: 5pt 4 | } 5 | -------------------------------------------------------------------------------- /resource/META-INF/container.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | import sys 3 | from html2epub import html2epub 4 | 5 | reload(sys) 6 | sys.setdefaultencoding('utf8') 7 | 8 | sys.getdefaultencoding() 9 | 10 | if __name__ == '__main__': 11 | path = raw_input('请输入需要转换的HTML存放目录:') 12 | ToPath = raw_input('请输入生成的epub存放目录:') 13 | 14 | zh = html2epub(path, ToPath) 15 | zh.start() 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /zipFile.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | import os 3 | import zipfile 4 | 5 | # 从oschina上抄的。 6 | # 写的很好:http://www.oschina.net/code/snippet_89296_9122 7 | def zip_dir(dirname,zipfilename): 8 | filelist = [] 9 | if os.path.isfile(dirname): 10 | filelist.append(dirname) 11 | else: 12 | for root, dirs, files in os.walk(dirname): 13 | for name in files: 14 | filelist.append(os.path.join(root, name)) 15 | 16 | zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED) 17 | for tar in filelist: 18 | arcname = tar[len(dirname):] 19 | #print arcname 20 | zf.write(tar,arcname) 21 | zf.close() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 一个简单的HTML转epub格式的Python程序 2 | 3 | ## 文件说明: 4 | 5 | ### 1. html2epub.py 6 | # 可直接实例化调用 7 | # 传入path要转换的文件所在路径以及生成文件的路径 8 | zh = html2epub(path, ToPath) 9 | zh.start() 10 | 11 | ### 2. main.py 12 | 一个示例,可直接从main运行此转换工具。 13 | 14 | ### 3. htmlcl.py 15 | html解析并下载图片等操作的类,html2epub类依赖此类。 16 | 17 | ### 4. zipFile.py 18 | 压缩操作,html2epub类依赖此类。 19 | 20 | ### 5. resource 21 | 内含生成epub所必须的资源文件。 22 | 23 | ### 注意:此程序运行需联网 24 | 25 | ## 希望能帮更多的人 26 | 27 | 之前写过一个爬廖雪峰的Python教程的程序,无奈爬下来的是html。想在kindle上看,找到了这个[在线转epub的网站](http://html.toepub.com/)。 28 | 29 | 但是这个网站转出来的epub没有图片,所以一直在找更靠谱的转换工具。发现epub本质上就是一堆xml配置文件+html+css打了一个zip包,所以就试着做了一下。 30 | 31 | 希望能帮助和我一样,在寻求可以把html带到kindle上看的人们。 -------------------------------------------------------------------------------- /resource/toc.ncx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Unknown 12 | 13 | 14 | 15 | 16 | Start 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /resource/content.opf: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Unknown 5 | 6 | ToEPUB.com 7 | 5e70792b-599b-4dc8-ba5c-f414915a2d38 8 | 9 | 10 | 11 | 12 | en 13 | 5e70792b-599b-4dc8-ba5c-f414915a2d38 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /htmlcl.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | import HTMLParser 3 | import hashlib 4 | import urllib 5 | 6 | # 继承于Python自带的HTMLParser解析类 7 | import urllib2 8 | 9 | 10 | class get_img(HTMLParser.HTMLParser): 11 | def __init__(self, html, path): 12 | self.html = html 13 | self.path = path 14 | HTMLParser.HTMLParser.__init__(self) 15 | 16 | # 当标签开始的时候 17 | def handle_starttag(self, tag, attrs): 18 | # 如果标签是img标签 19 | if tag == 'img': 20 | for key, value in attrs: 21 | # 取标签的src属性 22 | if key == "src": 23 | # 哈希url,得到一会儿要保存的唯一的图片名 24 | hash = hashlib.md5(value).hexdigest().upper()[0:8] 25 | new_url = "oebps/image/" + hash + '.png' 26 | 27 | # 下载图片并保存 28 | # print value 29 | # opener=urllib2.build_opener() 30 | # req=urllib2.Request(value) 31 | # req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36") 32 | # req.add_header("If-None-Match",'"1153673238\"') 33 | # res=opener.open(req) 34 | # f = open(self.path + new_url,'wb') 35 | # f.write(res.read()) 36 | try: 37 | i_headers = { 38 | "User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1) Gecko/20090624 Firefox/3.5", 39 | "Accept": "text/plain"} 40 | 41 | req = urllib2.Request(value, headers=i_headers) 42 | res = urllib2.urlopen(req) 43 | f = open(self.path + new_url, "wb") 44 | f.write(res.read()) 45 | 46 | # urllib.urlretrieve(value, self.path + new_url) 47 | except Exception: 48 | print Exception 49 | print key, value, hash 50 | 51 | # 替换HTML中的图片路径为本地图片路径 52 | self.html[0] = self.html[0].replace(value, new_url) 53 | -------------------------------------------------------------------------------- /resource/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |

Python教程

8 |
33602次阅读
9 |
10 |

这是小白的Python新手教程。

11 |

Python是一种计算机程序设计语言。你可能已经听说过很多种流行的编程语言,比如非常难学的C语言,非常流行的Java语言,适合初学者的Basic语言,适合网页编程的JavaScript语言,等等。

12 |

那Python是一种什么语言?

13 |

首选,我们普及一下编程语言的基础知识。用任何编程语言来开发程序,都是为了让计算机干活,比如下载一个MP3,编写一个文档等等,而计算机干活的CPU只认识机器指令,所以,尽管不同的编程语言差异极大,最后都得“翻译”成CPU可以执行的机器指令。而不同的编程语言,干同一个活,编写的代码量,差距也很大。

14 |

比如,完成同一个任务,C语言要写1000行代码,Java只需要写100行,而Python可能只要20行。

15 |

所以Python是一种相当高级的语言。

16 |

你也许会问,代码少还不好?代码少的代价是运行速度慢,C程序运行1秒钟,Java程序可能需要2秒,而Python程序可能就需要10秒。

17 |

那是不是越低级的程序越难学,越高级的程序越简单?表面上来说,是的,但是,在非常高的抽象计算中,高级的Python程序设计也是非常难学的,所以,高级程序语言不等于简单。

18 |

但是,对于初学者和完成普通任务,Python语言是非常简单易用的。连Google都在大规模使用Python,你就不用担心学了会没用。

19 |

用Python可以做什么?可以做日常任务,比如自动备份你的MP3;可以做网站,很多著名的网站包括YouTube就是Python写的;可以做网络游戏的后台,很多在线游戏的后台都是Python开发的。总之就是能干很多很多事啦。

20 |

Python当然也有不能干的事情,比如写操作系统,这个只能用C语言写;写手机应用,只能用Objective-C(针对iPhone)和Java(针对Android);写3D游戏,最好用C或C++。

21 |

如果你是小白用户,满足以下条件:

22 | 28 |

不要再犹豫了,这个教程就是为你准备的!

29 |

准备好了吗?

30 |

challenge-accepted

31 |

关于作者

32 |

廖雪峰,十年软件开发经验,业余产品经理,精通Java/Python/Ruby/Visual Basic/Objective C等,对开源框架有深入研究,著有《Spring 2.0核心技术与最佳实践》一书,多个业余开源项目托管在GitHub,欢迎微博交流:

33 |

34 |
35 | 36 |
37 | 38 | -------------------------------------------------------------------------------- /resource/stylesheet.css: -------------------------------------------------------------------------------- 1 | .calibre { 2 | display: block; 3 | font-size: 1em; 4 | padding: 0; 5 | margin: 0 5pt 6 | } 7 | .calibre1 { 8 | padding: 0; 9 | margin: 0 10 | } 11 | .calibre2 { 12 | display: list-item; 13 | padding: 0; 14 | margin: 0 15 | } 16 | .calibre3 { 17 | height: auto; 18 | max-height: 100%; 19 | max-width: 100%; 20 | width: auto; 21 | padding: 0; 22 | margin: 0 23 | } 24 | .calibre4 { 25 | display: block; 26 | font-family: "Liberation Serif", serif; 27 | font-size: 1.125em; 28 | font-weight: bold; 29 | line-height: 1.2; 30 | padding: 0; 31 | margin: 0.1665in 0 0.1965in 32 | } 33 | .calibre5 { 34 | line-height: 1.2; 35 | padding: 0; 36 | margin: 0 37 | } 38 | .G-fr { 39 | display: inline; 40 | font-size: 1em; 41 | height: 2in; 42 | position: static; 43 | width: 3.3335in; 44 | padding: 0; 45 | margin: 0 auto 46 | } 47 | .G-fr1 { 48 | display: inline; 49 | font-size: 1em; 50 | height: 0.3929in; 51 | position: static; 52 | width: 0.7862in; 53 | padding: 0; 54 | margin: 0 auto 55 | } 56 | .L { 57 | display: block; 58 | list-style-type: disc; 59 | margin-bottom: 1em; 60 | margin-right: 0; 61 | margin-top: 1em; 62 | padding-bottom: 0; 63 | padding-right: 0; 64 | padding-top: 0 65 | } 66 | .P-Horizontal20Line { 67 | display: block; 68 | font-family: "Liberation Serif", serif; 69 | font-size: 0.41667em; 70 | padding: 0; 71 | border-top: currentColor none medium; 72 | border-right: currentColor none medium; 73 | border-bottom: #808080 double 0.06pt; 74 | border-left: currentColor none medium; 75 | margin: 0 0 0.1965in 76 | } 77 | .P-P { 78 | display: block; 79 | font-family: "Liberation Serif", serif; 80 | font-size: 1em; 81 | font-weight: bold; 82 | padding: 0; 83 | margin: 0.1665in 0 0.1965in 84 | } 85 | .P-P1 { 86 | display: block; 87 | font-family: "Liberation Serif", serif; 88 | font-size: 1em; 89 | padding: 0; 90 | margin: 0 91 | } 92 | .P-Text20body { 93 | display: block; 94 | font-family: "Liberation Serif", serif; 95 | font-size: 1em; 96 | padding: 0; 97 | margin: 0 0 0.1965in 98 | } 99 | .P-Text20body1 { 100 | display: block; 101 | font-family: "Liberation Serif", serif; 102 | font-size: 1em; 103 | text-align: center; 104 | padding: 0; 105 | margin: 0 0 0.1965in 106 | } 107 | -------------------------------------------------------------------------------- /html2epub.py: -------------------------------------------------------------------------------- 1 | # coding:utf8 2 | import os 3 | import shutil 4 | import htmlcl 5 | import zipFile 6 | 7 | 8 | class html2epub: 9 | def __init__(self, Path, Topath): 10 | # 得到两个路径 11 | self.path = self.Path2Std(Path) 12 | self.toPath = self.Path2Std(Topath) 13 | 14 | def start(self): 15 | # 举出html存放路径下所有文件 16 | all_file = os.listdir(self.path) 17 | 18 | # print all_file 19 | for each in all_file: 20 | print each 21 | if not each.endswith('.html'): 22 | continue 23 | # 新建一个temp文件夹用来组织zip包里的内容 24 | # 如果已经存在则删除temp文件夹 25 | if os.path.exists('temp'): 26 | shutil.rmtree('temp') 27 | # 复制粘贴 28 | shutil.copytree('resource', 'temp') 29 | # 创建存放图片的目录 30 | os.makedirs('temp/oebps/image') 31 | 32 | # 组合文件名 33 | name = each.decode('utf-8') 34 | FilePath = self.path + name 35 | 36 | #第一步:修改content.opf文件的title 37 | file0 = open(r'resource/content.opf', 'r') 38 | change = file0.read() 39 | file0.close() 40 | 41 | OnlyName = name.replace('.html', '') 42 | change = change.replace('', '' + OnlyName) # 很粗暴的替换 43 | change = change.replace('content="', 'content="' + OnlyName) # 很粗暴的替换 44 | file0 = open(r'temp/content.opf', 'w') 45 | file0.write(change) 46 | file0.close() 47 | # 第一个要处理的文件处理完了 48 | 49 | # 第二步:处理HTML文件 50 | file1 = open(FilePath, 'r') 51 | index = file1.read() 52 | file1.close() 53 | 54 | # 写一个解析类,负责下载html中的图片,并放入特定的路径下,并修改HTML文件中的图片路径 55 | rep = [index] 56 | Parser = htmlcl.get_img(html=rep, path=r'temp/') 57 | # print index 58 | Parser.feed(index) 59 | # print rep[0] 60 | 61 | #把改好的html写入文件 62 | file1 = open(r'temp/index.html', 'w') 63 | file1.write(rep[0]) 64 | file1.close() 65 | 66 | # 第三步:zip压缩修改好的文件,命名为.epub后缀 67 | zipFile.zip_dir(r'temp', self.toPath + OnlyName + '.epub') 68 | 69 | # 删除临时的文件夹 70 | shutil.rmtree('temp') 71 | print '所有已完成' 72 | 73 | 74 | 75 | 76 | def Path2Std(self, Path): 77 | 78 | Path = Path.decode('utf-8') 79 | Path = Path.replace('\\', '/') 80 | 81 | if Path.endswith('/'): 82 | pass 83 | else: 84 | Path += '/' 85 | 86 | # print Path 87 | return Path --------------------------------------------------------------------------------