自用网文漫画脚本

就,我读网文习惯自制epub,从 zxcs.me 下载完之前一直手动做正则,今天兴致上来了写了个python小脚本,没做路径输入,每次都要改读取那里(今晚的兴致没支持做完这部分),做完需要微调下metadata,然后就可以愉快扔给 pandoc 了

关联阅读:

import re

old=open(r'pathToNovel.txt',encoding="utf-8")
content=old.read()
old.close()

h1=re.compile(r'\n([第序终][〇零一两二三四五六七八九十百千\d]*?[卷部篇].*|卷[〇零一两二三四五六七八九十百千\d]*?.*)')
h2=re.compile(r'\n([第序终][〇零一二两三四五六七八九十百千\d]*?[章节].*)')
p1=re.compile('  (.+)')
p2=re.compile(r'\n{3,}')
p3=re.compile(r'==========================================================\n更多精校小说尽在知轩藏书下载:http://www.zxcs.me/\n==========================================================')

a=h1.sub(r'\n* \1\n',content)
b=h2.sub(r'\n** \1\n',a)
c=p1.sub(r'\1\n',b)
d=p3.sub('',e)
e=p2.sub(r'\n\n',d)

new=open(r'Novel.org','w',encoding="utf-8")
new.write(e)
new.close()
/* This defines styles and classes used in the book */
body {
    text-align:  justify;
    }
code {
    font-family: monospace;
    }
h1 {
    text-align: center;
    font-size: 1.5em;
}
h2 {
    text-align: left;
    font-size: 1.2em;
}
h3 { text-align: left; }
h4 { text-align: left; }
h5 { text-align: left; }
h6 { text-align: left; }
p  {
     margin:0em;
     /*font-family: ; 标准的正文字体*/
     font-size: 14px; /*正文字体大小,默认是16px*/
     text-indent: 2em; /*首行缩进2个汉字空间*/
     text-align: justify; /*两端对齐*/
     text-justify: inter-ideograph; /*用表意文本来排齐内容。*/
     }
/* For title, author, and date on the cover page */
h1.title { }
p.author { }
p.date { }
nav#toc ol,
nav#landmarks ol {
    padding: 0;
    margin-left: 1em;
}
nav#toc ol li,
nav#landmarks ol li { list-style-type: none; margin: 0; padding: 0; }
a.footnote-ref { vertical-align: super; }
em, em em em, em em em em em { font-style: italic;}
em em, em em em em { font-style: normal; }
code{ white-space: pre-wrap; }
span.smallcaps{ font-variant: small-caps; }
span.underline{ text-decoration: underline; }
q { quotes: "“" "”" "‘" "’"; }
div.column{ display: inline-block; vertical-align: top; width: 50%; }
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
@media screen { /* Workaround for iBooks issue; see #6242 */
  .sourceCode {
    overflow: visible !important;
    white-space: pre-wrap !important;
  }
}
2 个赞

好难找到写得好的网文啊

从网上找的 ePub 转 CBZ 的脚本:

# -*- coding: utf-8 -*-
#使用方法,将本文件放置到和待转换文件的同级目录
import sys,time
import zipfile
import os
def find_str(content):
    content = content.decode()
    begin = content.find("<img src=")
    end = content.find("\"",begin+10)
    return content[begin+10:end].replace("../","")
def get_map(filename):
    zread =zipfile.ZipFile(filename, 'r')
    zwrite = zipfile.ZipFile(filename.replace(".epub",".cbz"), 'w')
    for name in zread.namelist(): 
        if name.endswith(".html"):
            name_item = name.split(".")
            name_begin = name_item[0].split('/')[1]
            content = zread.read(name)
            img_path = find_str(content)
            img_end = img_path.split(".")[-1]
            if len(img_end)>4:
                continue
            image_name = "%s.%s"%(name_begin.rjust(3,'0') , img_end)
            if img_path in zread.namelist():
                #print (image_name,img_path)
                zwrite.writestr(image_name,zread.read(img_path))
    print("Successfully Converted",filename)
    zread.close()
    zwrite.close()
import os  
def fn(file_dir):
    L=[]   
    for root, dirs, files in os.walk(file_dir):  
        for f in files:  
            if os.path.splitext(f)[1] == '.epub':  # 想要保存的文件格式
                L.append(os.path.join(f))  #root 代表路径
    return L
now = os.getcwd()#当前目录
for filename in fn(now):#读取当前目录下所有的epub文件
    get_map(filename)