xkcd.py

Installation

Python 3.8 or higher is required.

To install the stable version, do the following:

# Unix / macOS
python3 -m pip install "xkcd.py"

# Windows
py -m pip install "xkcd.py"

To install the development version, do the following:

$ git clone https://github.com/Ombucha/xkcd.py

Make sure you have the latest version of Python installed, or if you prefer, a Python version of 3.8 or greater.

If you have have any other issues feel free to search for duplicates and then create a new issue on GitHub with as much detail as possible. Include the output in your terminal, your OS details and Python version.

Comic

class xkcd.Comic(number: int | None = None, *, random: bool | None = False)

A class that represents a comic.

Parameters:
  • number (Optional[int]) – The comic’s number.

  • random (Optional[bool]) – Whether to choose a random comic, or not.

Note

If random is True, number must not be specified.

Variables:
  • date – The comic’s date.

  • image – The comic’s image.

  • number – The number of the comic.

  • title – The comic’s title.

  • safe_title – A safe form of the comic’s title.

  • transcript – The trascript of the comic.

  • wiki_url – The URL of the comic’s wiki.

  • url – The comic’s URL.

class Image(url: str, alt: str)

A class that represents an image.

Variables:
  • url – The image’s URL.

  • title – The image’s Alt text.

  • filename – The filename of the image.

download(*, filename: str | None = None, path: str | None = None) str

Downloads the comic image.

Parameters:
  • filename – The name of the file to save the image as. If not specified, uses the image’s filename.

  • path – The path to save the image to. If not specified, saves in the current directory.

Returns:

The full path of the saved image.

show(*, filename: str | None = None, path: str | None = None) None

Downloads the comic and opens the image in the default image viewer.

Parameters:
  • filename – The name of the file to save the image as. If not specified, uses the image’s filename.

  • path – The path to save the image to. If not specified, saves in the current directory.

What If Article

class xkcd.WhatIfArticle(number: int | None = None, *, random=False)

A class that represents a What If article.

Parameters:
  • number (Optional[int]) – The article’s number.

  • random (Optional[bool]) – Whether to choose a random article, or not.

Note

If random is True, number must not be specified.

Variables:
  • entry – The article entry. It is a list of str, Image, Hyperlink and Reference.

  • number – The number of the article.

  • title – The article’s title.

  • question – The question of the article.

  • author – The author of the article.

  • url – The article’s URL.

A class that represents hyperlinked text.

Variables:
  • url – The URL that hyperlink leads to.

  • text – The text shown on the hyperlink.

class Image(url: str, alt: str)

A class that represents an image.

Variables:
  • url – The image’s URL.

  • alt – The image’s Alt text.

  • filename – The filename of the image.

class Reference(number: int, text: List[str])

A class that represents a reference.

Variables:
  • number – The reference’s number.

  • text – The text shown upon hovering over the reference.

Other Functions

xkcd.get_comic_from_date(release_date: datetime | date, *, max_workers: int | None = 32) Generator[Comic, None, None] | None

Gets a comic by its date if it exists.

Note

The comics returned may not be in chronological order due to multithreading.

Parameters:
  • release_date (datetime.datetime or datetime.date) – The date of the comic to fetch.

  • max_workers (Optional[int]) – The maximum number of threads to use for fetching comics.