epubfileimportepubfilebook=epubfile.Epub.open('mybook.epub')fortext_idinbook.get_texts():soup=book.read_file(text_id,soup=True)...book.write_file(text_id,soup)forimage_idinbook.get_images():data=book.read_file(image_id)...book.write_file(image_id,data)# Note that this does not reverse the table of contents.book.set_spine_order(reversed(book.get_spine_order()))cover_id=book.get_cover_image()ifcover_id:book.rename_file(cover_id,'myfavoritecoverimage')book.save('modifiedbook.epub')
epubfile提供对epub书籍的简单编辑。epubfile尝试将文件修改保持在最低限度。它不会添加、删除或重新排列文件,除非您要求它这样做,并且不会插入其他元数据。因此,它对epub2和epub3都是有效的,前提是您坚持支持书籍版本的操作。
命令行实用程序
这个图书馆是出于我自己的需要而诞生的。所以有一些内置的实用程序。addfile:
Add files into the book.
covercomesfirst:
Rename the cover image file so that it is the alphabetically-first image.
merge:
Merge multiple books into one.
normalize:
Rename files and directories in the book to match a common structure.
规格符合性
epubfile没有严格执行epub规范,您可以使用它创建不符合规范的书籍。检查基本错误,我对有关如何提高规范遵守的问题和评论开放,而不给图书馆增加显著的大小或复杂性。我把简单性和易用性放在首位,而不是完美。
与……
对于高级的文件间操作和更好的验证,我建议将此库与一个好的编辑器(如Sigil)结合使用。我编写这个库是因为尽管sigil插件非常适合处理一本书,但是很难使用sigil来处理多本书、读取书籍数据以便在其他程序中使用,或者执行其他书籍间操作。
不可期待的
除了一些基本的getter和setter之外,我不打算实现图书元数据的对象模型。您可以完全控制Epub.opf美化组对象,以便可以根据需要编辑元数据。BSD 3-Clause License
Copyright (c) 2019, Ethan Dalool
https://github.com/voussoir/epubfile
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
欢迎加入QQ群-->: 979659372
推荐PyPI第三方库