Python - mobi、azw3转epub

2024-06-17 08:52
文章标签 python epub mobi azw3

本文主要是介绍Python - mobi、azw3转epub,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 步骤
    • 代码
    • ebook-convert command


本文使用下面仓库的方法进行转换,依赖 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 subprocessdef 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 = filenameconverted_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.htmlWhenever 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 abuiltin recipe like this: ebook-convert "Recipe Name.recipe"output.epubCreated by Kovid Goyal <kovid@kovidgoyal.net>

2024-06-14(五)

这篇关于Python - mobi、azw3转epub的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1068993

相关文章

python安装whl包并解决依赖关系的实现

《python安装whl包并解决依赖关系的实现》本文主要介绍了python安装whl包并解决依赖关系的实现,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、什么是whl文件?二、我们为什么需要使用whl文件来安装python库?三、我们应该去哪儿下

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi

Python中多线程和多进程的基本用法详解

《Python中多线程和多进程的基本用法详解》这篇文章介绍了Python中多线程和多进程的相关知识,包括并发编程的优势,多线程和多进程的概念、适用场景、示例代码,线程池和进程池的使用,以及如何选择合适... 目录引言一、并发编程的主要优势二、python的多线程(Threading)1. 什么是多线程?2.

Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)

《Python爬虫selenium验证之中文识别点选+图片验证码案例(最新推荐)》本文介绍了如何使用Python和Selenium结合ddddocr库实现图片验证码的识别和点击功能,感兴趣的朋友一起看... 目录1.获取图片2.目标识别3.背景坐标识别3.1 ddddocr3.2 打码平台4.坐标点击5.图

Python自动化Office文档处理全攻略

《Python自动化Office文档处理全攻略》在日常办公中,处理Word、Excel和PDF等Office文档是再常见不过的任务,手动操作这些文档不仅耗时耗力,还容易出错,幸运的是,Python提供... 目录一、自动化处理Word文档1. 安装python-docx库2. 读取Word文档内容3. 修改

Python重命名文件并移动到对应文件夹

《Python重命名文件并移动到对应文件夹》在日常的文件管理和处理过程中,我们可能会遇到需要将文件整理到不同文件夹中的需求,下面我们就来看看如何使用Python实现重命名文件并移动到对应文件夹吧... 目录检查并删除空文件夹1. 基本需求2. 实现代码解析3. 代码解释4. 代码执行结果5. 总结方法补充在

Python自动化办公之合并多个Excel

《Python自动化办公之合并多个Excel》在日常的办公自动化工作中,尤其是处理大量数据时,合并多个Excel表格是一个常见且繁琐的任务,下面小编就来为大家介绍一下如何使用Python轻松实现合... 目录为什么选择 python 自动化目标使用 Python 合并多个 Excel 文件安装所需库示例代码

Python使用Pandas对比两列数据取最大值的五种方法

《Python使用Pandas对比两列数据取最大值的五种方法》本文主要介绍使用Pandas对比两列数据取最大值的五种方法,包括使用max方法、apply方法结合lambda函数、函数、clip方法、w... 目录引言一、使用max方法二、使用apply方法结合lambda函数三、使用np.maximum函数

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何