API Reference
Main module
get_book_template()
ACBFBook
- class libacbf.ACBFBook(file: Union[str, pathlib.Path, IO], mode: Literal['r', 'w', 'a', 'x'] = 'r', archive_type: Optional[str] = 'Zip')[source]
Bases:
object
Base class for reading ACBF ebooks.
- Parameters
file (str | Path | IO) – Path or file object to write ACBF book to. May be absolute or relative.
mode ('r' | 'w' | 'a' | 'x', default='r') –
The mode to open the file in. Defaults to read-only mode.
- r
Read only mode. No editing is possible. Can read ACBF, Zip, 7Zip, Tar and Rar formatted books.
- w
Overwrite file with new file. Raises exception for Rar archive types.
- a
Edit the book without truncating. Raises exception for Rar archive types.
- x
Exclusive write to file. Raises
FileExists
exception if file already exists. Only works for file paths. Raises exception for Rar archive types.
archive_type (str | None, default="Zip") –
The type of ACBF book that the file is. If
None
Then creates a plain XML book. Otherwise creates archive of format. Accepted string values are listed atArchiveTypes
.Warning
You do not have to specify the type of archive unless you are creating a new one. The correct type will be determined regardless of this parameter’s value. Use this when you want to create a new book.
- Raises
EditRARArchiveError – Raised if
mode
parameter is not'r'
but file is a Rar archive.InvalidBook – Raised if the XML does not match ACBF schema or if archive does not contain ACBF file.
See also
Notes
Archive formats use the defaults of each type like compression level etc. Manage the archives yourself if you want to change this. Image refs that are relative paths check within the archive if the book is an archive. Otherwise it checks relative to the ‘.acbf’ file. So you can simply use a directory to manage the book and archive it with your own settings when you are done.
Examples
A book object can be opened, read and then closed.
from libacbf import ACBFBook book = ACBFBook("path/to/file.cbz") # Read data from book book.close()
ACBFBook
is also a context manager and can be used in with statements.from libacbf import ACBFBook with ACBFBook("path/to/file.cbz") as book: # Read data from book
You can pass a
BytesIO
object. Keep in mind that you cannot usemode='x'
in this case.import io from libacbf import ACBFBook file = io.BytesIO() with ACBFBook(file, 'w') as book: # Write data to book
- publisher_info
See
PublishInfo
for more information.- Type
- document_info
See
DocumentInfo
for more information.- Type
- references
A dictionary that contains a list of particular references that occur inside the main document body. Keys are unique reference ids and values are dictionaries that contain a
'_'
key with text.{ "ref_id_001": { "_": "This is a reference." } "ref_id_002": { "_": "This is another reference." } }
'_'
can contain special tags for formatting. For more information and a full list, seeTextArea.text
.- Type
dict
- archive
Can be used to read archive directly if file is not plain ACBF. Use this if you want to read exactly what files the book contains but try to avoid directly writing files through
ArchiveReader
.- Type
ArchiveReader | None
- get_acbf_xml() str [source]
Get the XML tree of the ACBF book.
- Returns
The XML content of the ACBF book.
- Return type
str
- make_archive(archive_type: str = 'Zip')[source]
Convert a plain ACBF XML book to an archive format.
- Parameters
archive_type (str, default="Zip") – The type of archive to create. Allowed values are listed at
ArchiveTypes
.- Raises
AttributeError (Book is already an archive of type {archive.type}.) – Raised when book is already an archive.
Book Info
- class libacbf.libacbf.BookInfo(book: libacbf.libacbf.ACBFBook)[source]
Bases:
object
Metadata about the book itself.
See also
- book_title
A dictionary with standard language codes as keys and titles as values. Key is
'_'
if no language is defined.{ "_": "book title without language", "en": "English title", "en_GB": "English (UK) title", "en_US": "English (US) title" }
- Type
Dict[str, str]
- genres
A dictionary with keys being a value from
constants.Genres
Enum and values being integers with the match value orNone
. Seeget_match()
.- Type
Dict[Genres, int | None]
- annotations
A short summary describing the book.
It is a dictionary with keys being standard language codes or
'_'
if no language is defined and values being multiline strings.- Type
Dict[str, str]
- coverpage
It is the same as
body.Page
except it does not havebody.Page.title
,body.Page.bgcolor
andbody.Page.transition
.- Type
- languages
It represents all
body.TextLayer
objects of the book.A list of
LanguageLayer
objects.- Type
List[LanguageLayer], optional
- characters
List of (main) characters that appear in the book.
- Type
List[str], optional
- keywords
For use by search engines.
A dictionary with keys as standard language codes or
'_'
if no language is defined. Values are a set of lowercase keywords.- Type
Dict[str, Set[str]], optional
- series
Contains the sequence and number if particular comic book is part of a series.
A dictionary with keys as the title of the series and values as
Series
objects.- Type
Dict[str, Series], optional
- content_rating
Content rating of the book based on age appropriateness and trigger warnings.
It is a dictionary with the keys being the rating system and values being the rating.
{ "Age Rating": "16+", "DC Comics rating system": "T+", "Marvel Comics rating system": "PARENTAL ADVISORY" }
- Type
Dict[str, str], optional
- database_ref
References to a record in a comic book database (eg: GCD, MAL).
A list of
DBRef
objects.- Type
List[DBRef], optional
- add_author(*names: str, first_name=None, last_name=None, nickname=None) libacbf.metadata.Author [source]
Add an Author to the book info. Usage is the same as
Author
.- Returns
The created Author object.
- Return type
- edit_genre(genre: str, match: Optional[int] = '_')[source]
Edit a genre by string. Add it if it doesn’t exist.
- Parameters
genre (str) – See
constants.Genres
enum for a list of possible values.match (int | None, optional) – Set the match percentage of the genre. If
None
, removes the match value.
- pop_genre(genre: str) Optional[int] [source]
Pop a genre by string.
- Returns
The match value of the genre.
- Return type
int | None
- add_language(lang: str, show: bool)[source]
Add a language layer to the book. Usage is the same as
LanguageLayer
.
Publisher Info
- class libacbf.libacbf.PublishInfo(book: libacbf.libacbf.ACBFBook)[source]
Bases:
object
Metadata about the book’s publisher.
See also
- publisher
Name of the publisher.
- Type
str
- publish_date
Date when the book was published as a human readable string.
- Type
str
- publish_date_value
Date when the book was published.
- Type
datetime.date, optional
- publish_city
City where the book was published.
- Type
str, optional
- isbn
International Standard Book Number.
- Type
str, optional
- license
The license that the book is under.
- Type
str, optional
- set_date(date: Union[str, datetime.date], include_date: bool = True)[source]
Edit the date the book was published.
- Parameters
date (str | datetime.date) – Date to set to.
include_date (bool, default=True) – Whether to also set
publish_date_value
. PassingFalse
will set it toNone
.
Document Info
- class libacbf.libacbf.DocumentInfo(book: libacbf.libacbf.ACBFBook)[source]
Bases:
object
Metadata about the ACBF file itself.
See also
- creation_date
Date when the ACBF file was created as a human readable string.
- Type
str
- creation_date_value
Date when the ACBF file was created.
- Type
datetime.date, optional
- source
A multiline string with information if this book is a derivative of another work. May contain URL and other source descriptions.
- Type
str, optional
- document_id
Unique Document ID. Used to distinctly define ACBF files for cataloguing.
- Type
str, optional
- document_version
Version of ACBF file.
- Type
str, optional
- document_history
Change history of the ACBF file with change information in a list of strings.
- Type
List[str], optional
- add_author(*names: str, first_name=None, last_name=None, nickname=None) libacbf.metadata.Author [source]
Add an Author to the document info. Usage is the same as
Author
.- Returns
The created Author object.
- Return type
- set_date(date: Union[str, datetime.date], include_date: bool = True)[source]
Edit the date the ACBF file was created.
- Parameters
date (str | datetime.date) – Date to set to.
include_date (bool, default=True) – Whether to also set
creation_date_value
. PassingFalse
will set it toNone
.
Body
- class libacbf.libacbf.ACBFBody(book: libacbf.libacbf.ACBFBook)[source]
Bases:
object
Body section contains the definition of individual book pages and text layers, frames and jumps inside those pages.
See also
- bgcolor
Defines a background colour for the whole book. Can be overridden by
bgcolor
in pages, text layers, text areas and frames.- Type
str, optional
- insert_page(index: int, image_ref: str) libacbf.body.Page [source]
Insert a new Page object at the index.
- Parameters
index (int) – Index of new page.
image_ref (str) – Value to set for the image reference. See
Page.image_ref
for information on how to format it.
- Returns
The created Page object.
- Return type
- append_page(image_ref: str) libacbf.body.Page [source]
Append a new Page object to the body.
- Parameters
image_ref (str) – Value to set for the image reference. See
Page.image_ref
for information on how to format it.- Returns
The created Page object.
- Return type
Data
- class libacbf.libacbf.ACBFData(book: libacbf.libacbf.ACBFBook)[source]
Bases:
object
Get any binary data embedded in the ACBF file or write data to archive or embed data in ACBF.
See also
- Returns
A file as a
BookData
object.- Return type
- Raises
FileNotFoundError – Raised if file is not found embedded in the ACBF file.
Examples
To get a file embedded in the ACBF file:
from libacbf import ACBFBook with ACBFBook("path/to/book.cbz") as book: image = book.data["image.png"] font = book.data["font.ttf"]
- list_files() Set[str] [source]
Returns a list of all the names of the files embedded in the ACBF file. May be images, fonts etc.
- Returns
A set of file names.
- Return type
Set[str]
- add_data(target: Union[str, pathlib.Path, bytes], name: str = None, embed: bool = False)[source]
Add or embed data into the book.
- Parameters
target (str | Path | bytes) – Path to file to be added or data as bytes.
name (str, optional) – Name to assign to file after writing. Defaults to name part of target. Required if
target
is bytes.embed (bool, default=False) – Whether to embed the file in the ACBF XML. Cannot be
False
if book is not an archive type. UseACBFBook.make_archive(...)
to convert the book to an archive.
- remove_data(target: Union[str, pathlib.Path], embed: bool = False)[source]
Remove file at target in the archive. If
embed
is true, removes from embedded files.- Parameters
target (str | Path) – Path to file in archive or id of embedded file.
embed (bool, default=False) – Whether to check for file in archive or embedded in ACBF XML. Must be true if book is plain ACBF XML.
Styles
- class libacbf.libacbf.Styles(book: libacbf.libacbf.ACBFBook)[source]
Bases:
object
Stylesheets to be used in the book.
See also
- Returns
Stylesheet data.
- Return type
bytes
Examples
To get stylesheets
from libacbf import ACBFBook with ACBFBook("path/to/book.cbz") as book: style1 = book.styles["style1.css"] # Style referenced at the top of the ACBF XML. embedded_style = book.styles['_'] # Returns the stylesheet embedded in ACBF XML.
- types
A dictionary with keys being the style name (or
'_'
) and values being the type orNone
if not specified.- Type
Dict[str, str | None]
- list_styles() Set[str] [source]
All the stylesheets referenced by the ACBF XML.
- Returns
Referenced stylesheets.
- Return type
Set[str]
- edit_style(stylesheet: Union[str, pathlib.Path, bytes], style_name: str = None, type: str = 'text/css', embed: bool = False)[source]
Writes or overwrites file in book with referenced stylesheet.
- Parameters
stylesheet (str | Path | bytes) – Path to stylesheet or stylesheet as bytes.
style_name (str, optional) – Name of stylesheet after being written. Defaults to name part of
stylesheet_ref
. If it is'_'
, writes stylesheet to style tag of ACBF XML. Required ifstylesheet
is bytes.type (str, default="text/css") – Mime Type of stylesheet. Defaults to CSS but can be others (like SASS).
embed (bool, default=False) – Whether to embed stylesheet in the data section of the book. This is ignored if
style_name
is'_'
. Must be True if book is plain ACBF XML. UseACBFBook.make_archive(...)
to convert the book to an archive.
- remove_style(style_name: str, embedded: bool = False)[source]
Remove stylesheet from book.
- Parameters
style_name (str) – Stylesheet to remove. If it is
'_'
, remove embedded stylesheet.embedded (bool, default=False) – Remove style from embedded data of book. Ignored if style_name is
'_'
. Must be False if book is plain ACBF XML.
Metadata
LanguageLayer
- class libacbf.metadata.LanguageLayer(lang: str, show: bool)[source]
Bases:
object
Used by
ACBFBook.book_info.languages
.See also
- lang
Language of the layer as a standard language code.
- Type
str
- show
Whether the layer is drawn.
- Type
bool
Series
- class libacbf.metadata.Series(sequence: str, volume: Optional[str] = None)[source]
Bases:
object
Used by
ACBFBook.book_info.series
.See also
- sequence
The book’s position/entry in the series.
- Type
str
- volume
The volume that the book belongs to.
- Type
str, optional
DBRef
- class libacbf.metadata.DBRef(dbname: str, ref: str, type: Optional[str] = None)[source]
Bases:
object
Used by
ACBFBook.book_info.database_ref
.See also
- dbname
Name of the database.
- Type
str
- reference
Reference of the book in the database.
- Type
str
- type
Type of the given reference such as URL, ID etc.
- Type
str, optional
Body
Page
- class libacbf.body.Page(image_ref: str, book: ACBFBook, coverpage: bool = False)[source]
Bases:
object
A page in the book.
See also
- image_ref
Reference to the image file. May be embedded in the ACBF file, in the ACBF archive, in an external archive, a local path or a URL.
There are several ways to format it to read data:
- Reference to a file embedded in
ACBFBook.data
: “#page1.jpg“
- Reference to a file on disk:
“/path/to/file/page1.jpg“
"C:\path\to\file\page1.jpg"
“file:///path/to/file/page1.jpg“
“file://C:\path\to\file\page1.jpg“
- Path to a file in the book’s archive or relative path to file on disk if book is a plain ACBF XML:
“page1.jpg“
“images/page1.jpg“
- Reference to file in an archive:
“zip:path/to/archive.zip!/path/to/file/page1.jpg“
- URL address containing the image:
“https://example.com/book1/images/page1.jpg“
- Type
str
- Reference to a file embedded in
- ref_type
A value from
ImageRefType
indicating the type of reference inPage.image_ref
.- Type
ImageRefType(Enum)
- text_layers
A dictionary with keys being the language of the text layer and values being
TextLayer
objects.- Type
Dict[str, TextLayer]
Warning
The attributes
title
,bgcolor
andtransition
are not available onACBFBook.book_info.coverpage
.- title
It is used to define beginning of chapters, sections of the book and can be used to create a table of contents. Keys are standard language codes or
'_'
if not defined. Values are titles as string.- Type
Dict[str, str], optional
- bgcolor
Defines the background colour for the page. Inherits from
ACBFBody.bgcolor
ifNone
.- Type
str, optional
- transition
Defines the type of transition from the previous page to this one. Allowed values are in
PageTransitions
.- Type
PageTransitions(Enum), optional
- property image: libacbf.bookdata.BookData
Gets the image data from the source.
- set_transition(tr: Optional[str])[source]
Set transition by string.
- Parameters
tr (str | None) – Transition value to be set. Pass
None
to remove.
- add_textlayer(lang: str, *areas: libacbf.body.TextArea) libacbf.body.TextLayer [source]
Add a text layer to the page.
- insert_frame(index: int, points: List[Tuple[int, int]]) libacbf.body.Frame [source]
Insert a frame at the index.
- Parameters
index (int) – Index to insert at.
points (List[Tuple[int, int]]) – The points defining the frame.
- Returns
The newly created frame.
- Return type
- append_frame(points: List[Tuple[int, int]]) libacbf.body.Frame [source]
Append a frame to the page.
- Returns
The newly created frame.
- Return type
- add_jump(target: int, points: List[Tuple[int, int]]) libacbf.body.Jump [source]
Add a jump to the page.
- Parameters
target (int) – The target page.
0
is the cover page,1
is the first page,2
is the second page etc.points (List[Tuple[int, int]]) – The points defining the jump.
- Returns
The newly created jump.
- Return type
TextLayer
- class libacbf.body.TextLayer(*areas: libacbf.body.TextArea)[source]
Bases:
object
Defines a text layer drawn on a page.
See also
- text_areas
A list of
TextArea
objects in order (order matters for text-to-speech).- Type
List[TextArea]
- bgcolor
Defines the background colour of the text areas or inherits from
Page.bgcolor
ifNone
.- Type
str, optional
- insert_textarea(index: int, text: str, points: List[Tuple[int, int]]) libacbf.body.TextArea [source]
Insert a text area at the index.
- Parameters
index (int) – Index to insert at.
text (str) – Multiline text of the text area.
points (List[Tuple[int, int]]) – The points that define the text area.
- Returns
- Return type
The newly created text area.
- append_textarea(text: str, points: List[Tuple[int, int]]) libacbf.body.TextArea [source]
Append a text area to the layer.
- Parameters
text (str) – Multiline text of the text area.
points (List[Tuple[int, int]]) – The points that define the text area.
- Returns
- Return type
The newly created text area.
TextArea
- class libacbf.body.TextArea(text: str, points: List[Tuple[int, int]])[source]
Bases:
object
Defines an area where text is drawn.
See also
- points
A list of tuples as coordinates.
- Type
List[Tuple[int, int]]
- text
A multiline string of what text to show in the are. Can have special tags for formatting.
- <strong>…</strong>
Bold letters.
- <emphasis>…</emphasis>
Italicised or cursive text.
- <strikethrough>…</strikethrough>
Striked-through text.
- <sub>…</sub>
Subscript text.
- <sup>…</sup>
Superscript text.
- <a href=“…“>…</a>
A link. Internal or external.
- Type
str
- bgcolor
Defines the background colour of the text area or inherits from
TextLayer.bgcolor
ifNone
.- Type
str, optional
- rotation
Defines the rotation of the text layer.
Can be an integer from 0 to 360.
- Type
int, optional
- type
The type of text area. Rendering can be changed based on type. Allowed values are defined in
TextAreas
.- Type
TextAreas(Enum), optional
- inverted
Whether text is rendered with inverted colour.
- Type
bool, optional
- transparent
Whether text is drawn.
- Type
bool, optional
Frame
- class libacbf.body.Frame(points: List[Tuple[int, int]])[source]
Bases:
object
A subsection of a page.
See also
- points
A list of tuples as coordinates.
- Type
List[Tuple[int, int]]
- bgcolor
Defines the background colour for the page. Inherits from
Page.bgcolor
ifNone
.- Type
str, optional
Jump
- class libacbf.body.Jump(target: int, points: List[Tuple[int, int]], book: ACBFBook)[source]
Bases:
object
Clickable area on a page which navigates to another page.
See also
- target
The target page index. Cover page is
0
, first page is1
, second page is2
and so on.- Type
int
- points
A list of tuples as coordinates.
- Type
List[Tuple[int, int]]
- property page: libacbf.body.Page
Target page to go to when clicked.
Book Data
Constants
Warning
The values of the enum members don’t matter and there is no guarantee that they will never change. If you have to use it, use strings instead (case sensitive).
activity = AuthorActivities.Artist.name
# Check if value exists
_ = AuthorActivities["Writer"] # No `KeyError` exception
_ = AuthorActivities["asdfgh"] # `KeyError` exception is raised
Genres(Enum)
- class libacbf.constants.Genres(value)[source]
Bases:
enum.Enum
List of accepted values for keys of
book_info.genres
.- adult = 0
- adventure = 1
- alternative = 2
- biography = 3
- caricature = 4
- children = 5
- computer = 6
- crime = 7
- education = 8
- fantasy = 9
- history = 10
- horror = 11
- humor = 12
- manga = 13
- military = 14
- mystery = 15
- non_fiction = 16
- politics = 17
- real_life = 18
- religion = 19
- romance = 20
- science_fiction = 21
- sports = 22
- superhero = 23
- western = 24
- other = 25
TextAreas(Enum)
PageTransitions(Enum)
ImageRefType(Enum)
ArchiveTypes(Enum)
Exceptions
- exception libacbf.exceptions.UnsupportedArchive(message: str = 'File is not a supported archive type.', *args)[source]
Bases:
Exception
libacbf.archivereader module
- libacbf.archivereader.get_archive_type(file: Union[str, pathlib.Path, BinaryIO]) libacbf.constants.ArchiveTypes [source]
Get the type of archive.
- Parameters
file (str | pathlib.Path | BinaryIO) – File to check.
- Returns
Returns
ArchiveTypes
enum.- Return type
ArchiveTypes(Enum)
- Raises
UnsupportedArchive – Raised if file is not of a supported archive type.
- class libacbf.archivereader.ArchiveReader(file: Union[str, pathlib.Path, BinaryIO], mode: Literal['r', 'w'] = 'r')[source]
Bases:
object
This can read and write Zip, 7Zip and Tar archives. Rar archives are read-only.
Notes
Writing and creating archives uses the default options for each type. You cannot use this module to change compression levels or other options.
- Parameters
file (str | pathlib.Path | BinaryIO) – Archive file to be used.
mode ('r' | 'w') – Mode to open file in. Can be
'r'
for read-only or'w'
for read-write. Nothing is overwritten.
- archive
The archive being used.
- Type
zipfile.ZipFile | tarfile.TarFile | py7zr.SevenZipFile | rarfile.RarFile
- type
The type of archive. See enum for possible types.
- Type
- mode
Mode to open file in. Can be
'r'
for read-only or'w'
for read-write. Nothing is overwritten.- Type
‘r’ | ‘w’
- _extract
The contents of the archive are extracted to a temporary directory in write mode only and this is used for listing, reading and writing. It is created in the same directory as the archive or, if the path is not found, it is created in the system temp directory.
- Type
tempfile.TemporaryDirectory | None
- _arc_path
The path to the temporary directory the archive is extracted to in write mode.
- Type
pathlib.Path | None
- _source
The file passed in.
- Type
str | Path | BinaryIO
- property filepath: Optional[pathlib.Path]
Path to the archive file. Returns
None
if it does not have a path.
- property filename: Optional[str]
Name of the archive file. Returns
None
if it does not have a path.
- _get_acbf_file() Optional[str] [source]
Returns the name of the first file with the
.acbf
extension at the root level of the archive orNone
if no file is found.
- read(target: str) Optional[bytes] [source]
Get file as bytes from archive.
- Parameters
target (str) – Path relative to root of archive.
- Returns
Contents of file.
- Return type
bytes
- write(target: Union[str, pathlib.Path, bytes], arcname: Optional[str] = None)[source]
Write file to archive.
- Parameters
target (str | Path | bytes) – File to be written. Reads a file on disk if string or path is passed. Writes data directly if bytes is passed.
arcname (str, default=Name of target file) – Name of file in archive.
- delete(target: Union[str, pathlib.Path], recursive: bool = False)[source]
File to delete from archive.
- Parameters
target (str | Path) – Path of file to delete relative to root of archive.
recursive (bool, default=False) – Whether to remove directories recursively.
- __module__ = 'libacbf.archivereader'
- __weakref__
list of weak references to the object (if defined)