epub解压的多个html制作单个html


'''
运行方法:"python " 加上 "build_one_html_from_multi_html_in_epub.py"所在路径
'''

import os
import sys

srcpth = os.path.split(sys.argv[0])[0].replace("\\","/")

print(srcpth)


'''
递归遍历
'''
def dirlist(path, ext="", allfile=None):
    if allfile == None:
        allfile = []
    filelist = os.listdir(path)
    for filename in filelist: #广义
        filepath = os.path.join(path, filename)
        if os.path.isdir(filepath):
            dirlist(filepath, ext, allfile)
        if ext == "":
            filepath = filepath.replace("\\", "/")
            allfile.append(filepath)
        else:
            if filepath.endswith(ext):
                filepath = filepath.replace("\\", "/")
                allfile.append(filepath)
    return allfile

if os.path.split(sys.argv[0])[0].replace("\\","/")+"/text" not in dirlist(srcpth):
    print("请放在epub改后缀zip解压后text文件夹的同级目录下!")
    exit()

fp = open(srcpth+"/all.html","w",encoding="utf-8")

mark1 = "<?xml version='1.0' encoding='utf-8'?>\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n  <head>\n    <title>Burnham’s Celestial Handbook</title>\n    <link rel=\"stylesheet\" type=\"application/vnd.adobe-page-template+xml\" href=\"page-template.xpgt\"/>\n    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n  <link href=\"stylesheet.css\" rel=\"stylesheet\" type=\"text/css\"/>\n<link href=\"page_styles.css\" rel=\"stylesheet\" type=\"text/css\"/>\n</head>\n  <body class=\"calibre\">\n"
fp.write(mark1)
pass
marks = mark1.split("\n")

marks = marks + ["</body></html>"]
#print(marks)


for src in dirlist(srcpth+"/text"):
    print(src)
    bodyflag = False
    for line in open(src,encoding="utf-8").readlines():
        if line.startswith("  <body") is True:
            bodyflag = None
        elif line.startswith("</body>") is True:
            bodyflag = False

        if bodyflag == True:
            fp.write(line.replace("../",""))
        elif bodyflag == None:
            bodyflag = True


fp.write("</body></html>\n")
pass

fp.close()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值