# 自用网文漫画脚本

**URL:** <https://meta.appinn.net/t/topic/36364>\
**Category:** 讨论分享\
**Tags:** python\
**Created:** [2022 年9 月 19 日 15:38 UTC](https://meta.appinn.net/t/topic/36364 "2022-09-19T15:38:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Randir](https://meta-edge.appinn.com/letter_avatar_proxy/v4/letter/r/ac8455/32.png) [@Randir](https://meta.appinn.net/u/Randir)\
**Post date:** [2022 年9 月 19 日 15:38 UTC](https://meta.appinn.net/t/topic/36364/1 "2022-09-19T15:38:54Z")

</div>

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

关联阅读：

- [从下载到下过了就是看过了：电子书一键摆烂ABC](https://meta.appinn.net/t/topic/26903)

```python
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()

```

```css
/* 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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![jy02201949](https://meta-edge.appinn.com/user_avatar/meta.appinn.net/jy02201949/32/41_2.png) [@jy02201949](https://meta.appinn.net/u/jy02201949)\
**Post date:** [2022 年9 月 20 日 00:58 UTC](https://meta.appinn.net/t/topic/36364/2 "2022-09-20T00:58:00Z")

</div>

好难找到写得好的网文啊

---

<div class="post-metadata">

**Author:** ![Randir](https://meta-edge.appinn.com/letter_avatar_proxy/v4/letter/r/ac8455/32.png) [@Randir](https://meta.appinn.net/u/Randir)\
**Post date:** [2023 年4 月 25 日 00:07 UTC](https://meta.appinn.net/t/topic/36364/3 "2023-04-25T00:07:46Z")

</div>

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

```python
# -*- 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)

```
