Skip to main content

FB2

PyPI

Python package for working with FictionBook2

Usage example

from urllib import request

from FB2 import Author, ChapterWithSubchapters, FictionBook2, Image, SimpleChapter

book = FictionBook2()
book.titleInfo.title = "Example book"
book.titleInfo.annotation = "Small test book. Shows basics of FB2 library"
book.titleInfo.authors = [
    Author(
        firstName="Alex",
        middleName="Unknown",
        nickname="Ae_Mc",
        emails=["ae_mc@mail.ru"],
        homePages=["ae-mc.ru"],
    )
]
book.titleInfo.genres = ["sf", "sf_fantasy", "shortstory"]
book.titleInfo.coverPageImages = [
    Image(
        media_type="image/jpeg",
        content=request.urlopen("https://picsum.photos/1080/1920").read(),
    ),
    # Multiple cover images not supported by most FB2 readers.
    # Image(
    #     media_type="image/jpeg",
    #     content=request.urlopen("https://picsum.photos/1080/1920").read(),
    # ),
]
book.titleInfo.sequences = [("Example books", 2)]
book.documentInfo.authors = ["Ae Mc"]
book.documentInfo.version = "1.1"

book.chapters = [
    SimpleChapter(
        title="Introduction",
        content=[
            "Introduction chapter first paragraph",
            "Introduction chapter second paragraph",
        ],
    ),
    SimpleChapter(
        title="1. Chapter one. FB2 format history",
        content=[
            "Introduction chapter first paragraph",
            "Introduction chapter second paragraph",
            Image(
                media_type="image/jpeg",
                content=request.urlopen("https://picsum.photos/1920/1080").read(),
            ),
            "Text after image 1",
            Image(
                media_type="image/jpeg",
                content=request.urlopen("https://picsum.photos/1920/1080").read(),
            ),
            "Text after image 2",
        ],
    ),
]

# Example of adding chapter with subchapters (subsections)
book.chapters.append(
    ChapterWithSubchapters(
        title="2. Chapter two. With subchapters (subsections)",
        epigraph=[
            "This is the epigraph for chapter two. It can be multiline.",
            "This is the second line of the epigraph.",
        ],
        annotation=[
            "This is the annotation for chapter two. It can also be multiline.",
            "This is the second line of the annotation.",
            "This chapter also contains cover image that goes before the text.",
        ],
        image=Image(
            media_type="image/jpeg",
            content=request.urlopen("https://picsum.photos/1920/1080").read(),
        ),
        subchapters=[
            SimpleChapter(
                title="Subchapter 2.1",
                content=[
                    "Subchapter 2.1 text",
                    "Subchapter 2.1 text 2",
                ],
            ),
            SimpleChapter(
                title="Subchapter 2.2",
                content=[
                    "Subchapter 2.2 text",
                    "Subchapter 2.2 text 2",
                ],
            ),
            ChapterWithSubchapters(
                title="Subchapter 2.3",
                subchapters=[
                    SimpleChapter(
                        title="Subsubchapter 2.3.1",
                        content=[
                            "Subsubchapter 2.3.1 text",
                            "Subsubchapter 2.3.1 text 2",
                        ],
                    ),
                    SimpleChapter(
                        title="Subsubchapter 2.3.2",
                        content=[
                            "Subsubchapter 2.3.2 text",
                            "Subsubchapter 2.3.2 text 2",
                        ],
                    ),
                ],
            ),
        ],
    ),
)

book.chapters.append(SimpleChapter(title="3. Chapter three. Empty"))
book.write("ExampleBook.fb2")

Requirements

  • iso639-lang package
  • Python 3.10+

Installation

  • From pip: pip install fb2

Metadata

Release files for FB2 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for FB2 0.3.3
File Size Uploaded
fb2-0.3.3.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for FB2 0.3.3
File Interpreter ABI Platform
fb2-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 23.5 kB

Release files / fb2-0.3.3.tar.gz

Download URL fb2-0.3.3.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3a9a01a625bcd06700d39e71acc58ed06fe4981437f0d3509339bb61e346bb59
BLAKE2b-256 checksum
How to use checksums
559ff622e4953dfd97bd2ede5568b1a9ac145c49c6688546eeb52a4263b9f473
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.3

Release files / fb2-0.3.3-py3-none-any.whl

Download URL fb2-0.3.3-py3-none-any.whl
Size 12.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0481e9f248ac3d69ce76d9e083ec97213960008a92ba037dc43bfeeac5d74363
BLAKE2b-256 checksum
How to use checksums
ff48bc6a618e02072ec228711891423914803793fa29a376d66af4add10e10db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.3

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.2.1

2 release files

0.1.8

1 release file

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page