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 panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

Python操作PDF文档的主流库使用指南

《Python操作PDF文档的主流库使用指南》PDF因其跨平台、格式固定的特性成为文档交换的标准,然而,由于其复杂的内部结构,程序化操作PDF一直是个挑战,本文主要为大家整理了Python操作PD... 目录一、 基础操作1.PyPDF2 (及其继任者 pypdf)2.PyMuPDF / fitz3.Fre

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统