Python - mobi、azw3转epub


本文使用下面仓库的方法进行转换,依赖 calibre
https://github.com/YangJie6020/covert


步骤

1、下载源代码

git clone https://github.com/YangJie6020/covert.git 

也可以复制文末的代码


2、安装 calibre
这里基于 macOS,如果你使用其他系统,可参考: https://manual.calibre-ebook.com/zh_CN/develop.html

brew install calibre

输入下面命令验证下

calibre --help

3、指定文件夹进行转换

python convert.py ~/Books

文件夹下将生成后缀为 .epub 的同名文件


代码

本质是调用 ebook-convert 命令

https://manual.calibre-ebook.com/conversion.html

#!/usr/bin/python
# -*- coding:utf-8 -*-
# convert.py
#

import sys
import os
import subprocess

def convert(dirname):

    if os.path.isdir(dirname):
        for pathname, dirs, files in os.walk(dirname):
            if files:
                for f in files:
                    filename = os.path.join(pathname, f)
                    convert_file(filename)
    else:
        convert_file(dirname)

def convert_file(filename):
    
    if filename.find("azw3") != -1 or filename.find("mobi") != -1:
        new_filename = filename
        converted_filename = ""
        if new_filename.find("azw3") != -1:
            converted_filename = new_filename.replace("azw3", "epub")
        if new_filename.find("mobi") != -1:
            converted_filename = new_filename.replace("mobi", "epub")

        commands = []
        commands.append("ebook-convert")
        commands.append(new_filename)
        commands.append(converted_filename)

        subprocess.call(commands)
    
if __name__ == "__main__":

    if len(sys.argv) != 2:
        print("Please enter directory or file!!")
    else:
        dirname = str(sys.argv[1])
        convert(dirname)
    


ebook-convert command

更多可见:https://manual.calibre-ebook.com/conversion.html

% ebook-convert --help
Usage: ebook-convert input_file output_file [options]

Convert an e-book from one format to another.

input_file is the input and output_file is the output. Both must be specified as the first two arguments to the command.

The output e-book format is guessed from the file extension of output_file. output_file can also be of the special format .EXT where EXT is the output file extension. In this case, the name of the output file is derived from the name of the input file. Note that the filenames must not start with a hyphen. Finally, if output_file has no extension, then it is treated as a folder and an "open e-book" (OEB) consisting of HTML files is written to that folder. These files are the files that would normally have been passed to the output plugin.

After specifying the input and output file you can customize the conversion by specifying various options. The available options depend on the input and output file types. To get help on them specify the input and output file and then use the -h option.

For full documentation of the conversion system see
https://manual.calibre-ebook.com/conversion.html

Whenever you pass arguments to ebook-convert that have spaces in them, enclose the arguments in quotation marks. For example: "/some path/with spaces"

Options:
  --version       show program's version number and exit

  -h, --help      show this help message and exit

  --list-recipes  List builtin recipe names. You can create an e-book from a
                  builtin recipe like this: ebook-convert "Recipe Name.recipe"
                  output.epub


Created by Kovid Goyal <kovid@kovidgoyal.net>


2024-06-14(五)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程乐园

请我喝杯伯爵奶茶~!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值