Skip to main content
https://gitlab.com/serial-lab/gs123/badges/master/pipeline.svg https://gitlab.com/serial-lab/gs123/badges/master/coverage.svg https://badge.fury.io/py/gs123.svg

Set of python tools to convert various GS1 formats to and from others. GS123 is an essential component of the QU4RTET Open Source EPCIS and is used in various tasks throughout the platform. GS123 can also be used as a standalone component to convert barcode data to URNs, etc.

Documentation

Find the latest docs here:

https://serial-lab.gitlab.io/gs123/

Features

  • Converts GS1 barcode values to EPC URN values.

  • Check digit calculation.

  • Converts URN values to barcode values.

  • Regular expressions to select barcode values in files.

  • File conversion utilities.

  • GS1 Data Matrix barcode generation (SVG and PNG output).

Installation

pip install gs123

For Data Matrix PNG output, also install Pillow:

pip install gs123 Pillow

Quick Start

Barcode to URN Conversion

from gs123.conversion import BarcodeConverter

# Parse a GS1 barcode (GTIN + Serial + Expiry + Lot)
bc = BarcodeConverter(
    "(01)20614141073461(21)0000010020(17)261231(10)LOTA1",
    company_prefix_length=7,
)

print(bc.epc_urn)          # urn:epc:id:sgtin:0614141.207346.10020
print(bc.gtin14)           # 20614141073461
print(bc.serial_number)    # 10020
print(bc.lot)              # LOTA1
print(bc.expiration_date)  # 261231

URN to Barcode Conversion

from gs123.conversion import URNConverter

converter = URNConverter("urn:epc:id:sgtin:0614141.207346.10020")

# Get a barcode string with lot and expiry
barcode = converter.get_barcode_value(
    lot="LOTA1",
    expiration="261231",
    parenthesis=True,
)
print(barcode)  # (01)20614141073461(21)10020(17)261231(10)LOTA1

Data Matrix Barcode Generation

Generate GS1 Data Matrix barcodes for pharmaceutical serialization, shipping labels, and traceability. Uses the ppf.datamatrix library (MIT license) for pure-Python encoding — no native libraries or Ghostscript required.

From a GS1 element string:

from gs123.datamatrix import GS1DataMatrix

dm = GS1DataMatrix("(01)20614141073461(21)0000010020(17)261231(10)LOTA1")

# Save as SVG (vector — crisp at any size, no extra deps)
dm.save_svg("barcode.svg", scale=10)

# Save as PNG (requires Pillow)
dm.save_png("barcode.png", scale=10)

# Get raw bytes for embedding in web pages, PDFs, etc.
svg_str = dm.svg()
png_bytes = dm.png()

From an EPC URN:

from gs123.datamatrix import GS1DataMatrix

dm = GS1DataMatrix.from_urn(
    "urn:epc:id:sgtin:0614141.207346.10020",
    company_prefix_length=7,
    lot="LOTA1",
    expiration="261231",
)
dm.save_png("from_urn.png", scale=12)

From a scanned barcode (round-trip):

from gs123.datamatrix import GS1DataMatrix

# Parse a scanned barcode and regenerate a clean Data Matrix
dm = GS1DataMatrix.from_barcode(
    "(01)20614141073461(21)0000010020(17)261231(10)LOTA1",
    company_prefix_length=7,
)
dm.save_svg("regenerated.svg")

# Override the lot for a repack scenario
dm = GS1DataMatrix.from_barcode(
    "(01)20614141073461(21)0000010020(17)261231(10)LOTA1",
    company_prefix_length=7,
    lot="NEWLOT99",
)

SSCC-18 (shipping containers):

from gs123.datamatrix import GS1DataMatrix

dm = GS1DataMatrix.from_urn(
    "urn:epc:id:sscc:0614141.1234567890",
    company_prefix_length=7,
)
dm.save_png("sscc_label.png", scale=10)

Batch generation:

from gs123.datamatrix import GS1DataMatrix

for serial in range(10001, 10021):
    urn = f"urn:epc:id:sgtin:0614141.207346.{serial}"
    dm = GS1DataMatrix.from_urn(
        urn,
        company_prefix_length=7,
        lot="BATCH2026A",
        expiration="271231",
    )
    dm.save_png(f"label_{serial}.png", scale=10)

Inspect the module matrix:

from gs123.datamatrix import GS1DataMatrix

dm = GS1DataMatrix("(01)20614141073461(21)0000010020")
rows, cols = dm.symbol_size  # (22, 22)
matrix = dm.matrix           # list of lists, 1=dark 0=light

# ASCII art
for row in matrix:
    print("".join("██" if v else "  " for v in row))

Jupyter Notebook

A complete interactive demo is available at notebooks/datamatrix_demo.ipynb. It covers all of the above examples with inline SVG and PNG rendering.

pip install gs123 Pillow jupyter
jupyter notebook notebooks/datamatrix_demo.ipynb

Dependencies

  • Click — CLI support

  • ppf.datamatrix — Pure-Python Data Matrix encoder (MIT license)

  • Pillow (optional) — Required only for PNG output

License

GNU General Public License v3 (GPLv3)

Metadata

Release files for gs123 4.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gs123 4.0.1
File Size Uploaded
gs123-4.0.1.tar.gz 42.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gs123 4.0.1
File Interpreter ABI Platform
gs123-4.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 72.5 kB

Release files / gs123-4.0.1.tar.gz

Download URL gs123-4.0.1.tar.gz
Size 42.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fe0ff3934fe228e799c2143fab2c522fb954e70a9a9f49a200aaf3e85ca72ef7
BLAKE2b-256 checksum
How to use checksums
15f55e7d0489768af559c3537988c96eb681676c5412dbbb72e3834291101643
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / gs123-4.0.1-py2.py3-none-any.whl

Download URL gs123-4.0.1-py2.py3-none-any.whl
Size 30.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
95c75d99adab9850ec6f145f4020136203b0477db3a03c02e3fdbbc7d05f04ee
BLAKE2b-256 checksum
How to use checksums
493d4e8b9a7a3c031e0627c287d56b6add832aabe09ddc60e6527b2c5947ec55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

4.0.1 This release

2 release files

3.0.0

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

2 release files

1.2.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.13

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page