通过 Python 将 epub 文件的繁体字替换为中文简体
epub 文件是一个压缩文件,可以通过 7z 等解压工具解压成文件夹,文件夹里面的 OEBPS/toc.ncx 存储着目录信息,OEBPS\Text\Section*.xhtml 存储着章节的具体内容
因此只需要想办法把着 2 种文件的内容读取出来,再替换中文文本为简体中文就可以实现 epub 文件的繁体字替换为中文简体
方案一 调用 zipfile 解析 epub
import zipfile
import zhconv
import os
import re
def simpleChineseGet(s):
# 文本替换,中文替换为简体中文
chineseR = re.compile('[\u4e00-\u9fa5]+')
s = re.sub(chineseR, lambda text: zhconv.convert(text.group(0), "zh-hans"), s)
return s
def replace():
# 当前代码文件的目录
pwd = os.path.dirname(__file__)
# 替换后文件存放的目录
replace_dir = "中文替换"
replace_path = os.path.join(pwd, replace_dir)
if not os.path.exists(replace_path):
os.mkdir(replace_path)
# 遍历当前目录的一级子文件
file_list = os.listdir(pwd)
for file in file_list:
book_path = file
# 判断文件后缀名是 epub
if file.split(".")[-1] == 'epub':
print(f"replace file:\t{file}")
# 打开旧的 epub 压缩文件
old_epub = zipfile.ZipFile(book_path, "r")
# 新建 epub 文件
new_book_path = os.path.join(replace_path, os.path.basename(file))
new_epub = zipfile.ZipFile(new_book_path, "w", zipfile.ZIP_DEFLATED)
# 复制文件
for i in old_epub.filelist:
file_path = i.filename
# 读取旧 epub 子文件, byte 数据类型
file_data = old_epub.read(file_path)
# 获取子文件后缀
file_end = file_path.split(".")[-1]
# 是目录信息或章节文件
if file_end == "ncx" or file_end == "xhtml":
# 替换文件信息的中文为简体
file_data = simpleChineseGet(str(file_data, encoding="utf-8"))
# 转为 byte
file_data = bytes(file_data, encoding="utf-8")
# 将文件信息写到新的 epub 文件
new_epub.writestr(file_path, file_data)
# 关闭 epub 文件
old_epub.close()
new_epub.close()
replace()
print("all done")
方案二 调用 ebooklib 解析 epub
import ebooklib
from ebooklib import epub
import re
import zhconv
import os
def simpleChineseGet(s):
# 文本替换,中文替换为简体中文
chineseR = re.compile('[\u4e00-\u9fa5]+')
s = re.sub(chineseR, lambda text: zhconv.convert(text.group(0), "zh-hans"), s)
return s
def replace():
# 当前代码文件的目录
pwd = os.path.dirname(__file__)
# 替换后文件存放的目录
replace_dir = "中文替换"
replace_path = os.path.join(pwd, replace_dir)
if not os.path.exists(replace_path):
os.mkdir(replace_path)
# 遍历当前目录的一级子文件
file_list = os.listdir(pwd)
# 遍历文件
for file in file_list:
book_path = file
# 判断文件后缀名是 epub
if file.split(".")[-1] == 'epub':
print(f"replace file:\t{file}")
# 使用 epublib 打开文件
book = epub.read_epub(book_path)
# 修改 epub 的目录
def replaceToc(tocNcx):
for i in tocNcx:
if isinstance(i, tuple) or isinstance(i, list):
# 存在子章节
section, subsection = i[0], i[1]
# 父章节名字替换
section.title = simpleChineseGet(section.title)
# 子章节递归替换
replaceToc(subsection)
else:
# 不存在子章节,直接替换
i.title = simpleChineseGet(i.title)
tocNcx = book.toc
replaceToc(tocNcx)
# 修改章节内容
def replaceItems(items):
# 遍历文件
for item in items:
# 是章节文件
if item.get_type() == ebooklib.ITEM_DOCUMENT:
# 获取字符文本
s = str(item.get_content(), encoding='utf-8')
# 文本替换,中文替换为简体中文
s = simpleChineseGet(s)
# 将数据写回文件
item.set_content(bytes(s, encoding = "utf-8"))
items = book.get_items()
replaceItems(items)
# 保存 epub 文件
new_book_path = os.path.join(replace_path, os.path.basename(file))
epub.write_epub(new_book_path, book)
print("all done")
if __name__ == "__main__":
replace()