Introduction

Aspose.BarCode FOSS for Python provides per-symbology option classes that control encoding behavior across all seven supported barcode formats. Each option class inherits from EncodeOptions and adds properties specific to its symbology — Code 128 encode modes, Code 39 ASCII selection, QR error correction levels, and EAN/UPC check digit handling.

This post covers each option class, its properties, and working code examples. All classes are available from the top-level aspose_barcode_foss package.


Key Features

Code 128 Encode Modes

Code128Options controls which Code 128 character sets the encoder uses. The encode_mode property accepts a Code128EncodeMode enum value: AUTO, CODE_A, CODE_B, CODE_C, CODE_AB, CODE_AC, or CODE_BC. AUTO selects the most compact encoding automatically. CODE_C is optimized for digit-only data (pairs of digits encoded as single code points).

import aspose_barcode_foss as barcode
from aspose_barcode_foss import Code128Options, Code128EncodeMode

bc = barcode.code128(
    "1234567890",
    encode=Code128Options(encode_mode=Code128EncodeMode.CODE_C),
)
svg = bc.to_svg()

Code 128 also supports gs1_enabled and eci_assignment_number properties inherited from EncodeOptions for GS1 and ECI metadata.

Code 39 Encoding

Code39Options controls whether the encoder uses the base 43-character set or the Full ASCII extension. Set full_ascii=True to enable Full ASCII mode, which encodes the full 128-character ASCII range using shift pairs. The optional add_check_digit property appends a modulo-43 check digit.

import aspose_barcode_foss as barcode
from aspose_barcode_foss import Code39Options

bc = barcode.code39(
    "Hello",
    encode=Code39Options(full_ascii=True, add_check_digit=True),
)
svg = bc.to_svg()

QR Code Configuration

QrOptions offers the most configuration among all symbologies:

  • error_correction_level: QrErrorCorrectionLevel.L (7%), .M (15%), .Q (25%), .H (30%)
  • encoding_mode: QrEncodeMode.AUTO, .NUMERIC, .ALPHANUMERIC, .BYTE, .KANJI
  • version: QR version (1–40), controlling symbol size
  • mask: mask pattern (0–7) for manual selection
import aspose_barcode_foss as barcode
from aspose_barcode_foss import QrOptions, QrErrorCorrectionLevel, QrEncodeMode

bc = barcode.qr(
    "https://example.com/product/12345",
    encode=QrOptions(
        error_correction_level=QrErrorCorrectionLevel.Q,
        encoding_mode=QrEncodeMode.BYTE,
    ),
)
png_bytes = bc.to_png()

EAN and UPC Check Digits

Ean13Options, Ean8Options, UpcaOptions, and UpceOptions each have an allow_check_digit_input property. By default, the library computes the check digit automatically from the data. Set allow_check_digit_input=True if your data already includes the check digit.

import aspose_barcode_foss as barcode
from aspose_barcode_foss import Ean13Options

# Automatic check digit (12 digits in, library appends the 13th)
bc = barcode.ean13("590123412345")
svg = bc.to_svg()

# Pre-computed check digit (13 digits, library validates)
bc2 = barcode.ean13(
    "5901234123457",
    encode=Ean13Options(allow_check_digit_input=True),
)

Rendering Options

RenderOptions applies to all symbologies and controls the visual output. Pass it to to_svg() or to_png():

PropertyTypeDescription
scalefloatUniform scale multiplier
dpiintResolution for PNG output
module_widthfloatWidth of one barcode module
module_heightfloatHeight of one barcode module
quiet_zonefloatWhitespace padding around the barcode
foreground_colorstrHex color for bars (e.g., "#000000")
background_colorstrHex color for background
transparent_backgroundboolTransparent background for PNG
show_textboolShow human-readable text below barcode
font_familystrFont for human-readable text
font_sizefloatFont size for human-readable text
import aspose_barcode_foss as barcode
from aspose_barcode_foss import RenderOptions

bc = barcode.upca("01234567890")
png = bc.to_png(options=RenderOptions(
    scale=3.0,
    dpi=300,
    foreground_color="#1a1a1a",
    background_color="#ffffff",
    show_text=True,
    font_family="Courier New",
))

Error Handling

All encode option validation happens at parse time. If invalid data or incompatible options are provided, the library raises a typed exception before any encoding begins:

  • InvalidInputError — data contains characters not supported by the symbology
  • EncodingError — encoding fails (e.g., data exceeds capacity)
  • SymbologyNotFoundError — unrecognized symbology name passed to generate()
  • UnsupportedCapabilityError — requested feature not available for this symbology

All exception classes inherit from BarcodeError.


Quick Start

pip install aspose-barcode-foss

Generate barcodes with different encode options and save them:

import aspose_barcode_foss as barcode
from aspose_barcode_foss import (
    Code128Options, Code128EncodeMode,
    QrOptions, QrErrorCorrectionLevel,
    RenderOptions,
)

# Code 128 in CODE_C mode (compact digit encoding)
c128 = barcode.code128(
    "9876543210",
    encode=Code128Options(encode_mode=Code128EncodeMode.CODE_C),
)
with open("code128.svg", "w") as f:
    f.write(c128.to_svg())

# QR with high error correction
qr = barcode.qr(
    "https://example.com",
    encode=QrOptions(error_correction_level=QrErrorCorrectionLevel.H),
)
with open("qr.png", "wb") as f:
    f.write(qr.to_png(options=RenderOptions(scale=4.0)))

# EAN-13 with custom colors
ean = barcode.ean13("590123412345")
with open("ean13.svg", "w") as f:
    f.write(ean.to_svg(options=RenderOptions(
        foreground_color="#003366",
        show_text=True,
    )))

Supported Formats

SymbologyTypeOptions ClassKey Properties
Code 1281DCode128Optionsencode_mode
Code 391DCode39Optionsfull_ascii, add_check_digit
EAN-131DEan13Optionsallow_check_digit_input
EAN-81DEan8Optionsallow_check_digit_input
QR Code2DQrOptionserror_correction_level, encoding_mode, version, mask
UPC-A1DUpcaOptionsallow_check_digit_input
UPC-E1DUpceOptionsallow_check_digit_input

Output formats:

FormatExtensionExport
SVG.svg
PNG.png

Open Source & Licensing

Aspose.BarCode FOSS for Python is MIT licensed. The source is available on GitHub and the package installs from PyPI with pip install aspose-barcode-foss. No native dependencies, no telemetry, no usage limits.


Getting Started