Skip to main content

A small package for working with FB2 files

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fb2-0.3.2.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fb2-0.3.2-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file fb2-0.3.2.tar.gz.

File metadata

  • Download URL: fb2-0.3.2.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.5

File hashes

Hashes for fb2-0.3.2.tar.gz
Algorithm Hash digest
SHA256 b07d4a730204f5c04a4764015a92e2c8cdf6cb33e5a48ac04b5f94a4274dbdcd
MD5 febfe6829364ca5ba35aed9485796314
BLAKE2b-256 a7312e740c52b8696045abc45bff683245a7f232e8378469a820bf981f80a605

See more details on using hashes here.

File details

Details for the file fb2-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: fb2-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.5

File hashes

Hashes for fb2-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 63274c6fba246c9b2ba5627f074a02ba2b0e30c7a6f5d9e83142cd2634b3b993
MD5 4c909beaaa787741c89562f01a510a13
BLAKE2b-256 53c25cd4efeeca3c9c9eda1478eccaf855cb85ca6017fa9f55a736b6c647ce63

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page