Skip to content

Easy Base64

py_simple.easy_base64

easy_base64 turns text into Base64 and back without the encoding boilerplate.

EasyBase64Error

Bases: Exception

Raised when text cannot be encoded or decoded as Base64.

Parameters:

Name Type Description Default
message str

Description of what went wrong.

required

decode_text(encoded)

Decode a Base64 string back into text.

Parameters:

Name Type Description Default
encoded str

Base64 text, such as the result of encode_text.

required

Returns:

Name Type Description
str str

The original text.

Raises:

Type Description
EasyBase64Error

If encoded is not valid Base64 text.

Example
from py_simple import decode_text

message = decode_text("aGVsbG8=")
import base64

message = base64.b64decode("aGVsbG8=").decode("utf-8")

decode_to_bytes(encoded)

Decode a Base64 string back into raw bytes.

The counterpart of encode_bytes. Use this when you need the original bytes rather than text (for example when the encoded data is an image or a binary file).

Parameters:

Name Type Description Default
encoded str

Base64 text, such as the result of encode_bytes.

required

Returns:

Name Type Description
bytes bytes

The original bytes.

Raises:

Type Description
EasyBase64Error

If encoded is not valid Base64 text.

Example
from py_simple import decode_to_bytes

raw = decode_to_bytes("AAEC")
import base64

raw = base64.b64decode("AAEC")

encode_bytes(data)

Encode raw bytes as a Base64 string.

Useful when working with binary data such as images, audio, or file contents that are already in bytes form and not text.

Parameters:

Name Type Description Default
data bytes

The bytes to encode.

required

Returns:

Name Type Description
str str

The Base64 text, safe to paste into a file or a message.

Raises:

Type Description
EasyBase64Error

If data is not bytes.

Example
from py_simple import encode_bytes

encoded = encode_bytes(b"\x00\x01\x02")
import base64

encoded = base64.b64encode(b"\x00\x01\x02").decode("ascii")

encode_file(file_path)

Encode the contents of a file as a Base64 string.

Reads a file in binary mode and returns its contents encoded as Base64. Useful for embedding icons, images, or documents into JSON, HTML, or API payloads.

Parameters:

Name Type Description Default
file_path str

Path to the file to encode.

required

Returns:

Name Type Description
str str

The Base64 text representing the file's contents.

Raises:

Type Description
EasyBase64Error

If file_path is not a string, the file is not found, or an error occurs reading the file.

Example
from py_simple import encode_file

data = encode_file("logo.png")
import base64

with open("logo.png", "rb") as file:
    data = base64.b64encode(file.read()).decode("ascii")

encode_text(text)

Encode a piece of text as a Base64 string.

Parameters:

Name Type Description Default
text str

The text to encode.

required

Returns:

Name Type Description
str str

The Base64 text, safe to paste into a file or a message.

Raises:

Type Description
EasyBase64Error

If text is not a string.

Example
from py_simple import encode_text

secret = encode_text("hello")
import base64

secret = base64.b64encode("hello".encode("utf-8")).decode("ascii")

is_base64(value)

Check whether a string is valid Base64.

Parameters:

Name Type Description Default
value str

The text to check.

required

Returns:

Name Type Description
bool bool

True when value can be decoded as Base64, otherwise False.

Example
from py_simple import is_base64

is_base64("aGVsbG8=")  # True
import base64

try:
    base64.b64decode("aGVsbG8=", validate=True)
    ok = True
except ValueError:
    ok = False