|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 shuoye 于 2014-10-1 13:50 编辑
帮我看看这是怎么了 我打算写个制作upub的程序结果测试分章时除了莫名错误代码运行正常结果很奇葩
- #!/usr/bin/env python3
- #coding=utf-8
- import sys
- if sys.version_info < (3, 0):
- print('ERROR:Python not > 3.0')
- sys.exit()
- import os
- import re
- import zipfile
- class Pyepub:
- def __init__(self, filename, author=None, cover=None):
- self.filename = filename
- self.author = author
- self.cover = cover
- def segmeat(self):
- os.makedirs('tmp/OPS')
- old_file = open(self.filename, 'r')
- new_file = open('tmp/OPS/' + 'chapter0'+ '.html', 'w')
- chapterlist = open('tmp/OPS/' + 'chapterlist', 'w')
- chapternum = 0
- regx = re.compile('^.*第.*章.*
- )
- new_file.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN">\n<head>\n<meta http-equiv="Content-Tepe" content="text/html: charset=utf-8" />\n<mets name="provider" content="" />\n<meta name="right" content=""/>\n<link rel="stylesheet" type="text/css" href="css/main.css">\n<script src="js/main.js" type="text/javascript"></script>\n<title>%s</title>\n</head>\n<body>\n<div>\n' % self.filename.split('.')[0])
- for each_line in old_file:
- search = regx.search(each_line)
- if search:
- chapternum += 1
- new_file.writelines('\n\n</div>\n</body>\n</html>')
- new_file.close()
- new_file = open('tmp/OPS/' + 'chapter' + str(chapternum) + '.html', 'w')
- new_file.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN">\n<head>\n<meta http-equiv="Content-Tepe" content="text/html: charset=utf-8" />\n<mets name="provider" content="" />\n<meta name="right" content=""/>\n<link rel="stylesheet" type="text/css" href="css/main.css">\n<script src="js/main.js" type="text/javascript"></script>\n<title>%s</title>\n</head>\n<body>\n<div>\n' % each_line)
- new_file.write('<h3>%s</h3>\n' % each_line)
- new_file.write('<p>%s</p>\n' % each_line)
- chapterlist.write(each_line)
- else:
- new_file.write('<p>%s</p>\n' % each_line)
- new_file.writelines('\n\n</div>\n</body>\n</html>\n')
- old_file.close()
- new_file.close()
- chapterlist.close()
- def other_file(self):
- os.mkdir('tmp/META-INF')
- tf = opem('tmp/META-INF/container.rxml')
- tf.write('<?xml version="1.0" encoding="UTF-8" ?>\n<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">\n\t<rootfiles>\n\t\t<rootfile full-path="OPS/fb.opf" media-type="application/oebps-package+xml"/>\n\t</rootfiles>\n</container>\n')
- tf.close()
- tf = opem('tmp/mimetype', 'w')
- tf.write('application/epub+zip')
- tf.close()
- def zipepub(self):
- pass
- if __name__ == '__main__':
- Pyepub(sys.argv[1]).segmeat()
复制代码
|
|