Skip to main content

cnpj-dv for Python

PyPI Version PyPI Downloads Python Version Test Status Last Update Date Project License

🚀 Full support for the new alphanumeric CNPJ format.

🌎 Acessar documentação em português

A Python utility to calculate check digits on CNPJ (Brazilian Business Tax ID).

Python Support

Python 3.10 Python 3.11 Python 3.12 Python 3.13 Python 3.14
Passing ✔ Passing ✔ Passing ✔ Passing ✔ Passing ✔

Features

  • Alphanumeric CNPJ: Full support for the new alphanumeric CNPJ format (introduced in 2026)
  • Flexible input: Accepts str or list[str]
  • Format agnostic: Strips non-alphanumeric characters from string input and uppercases letters
  • Auto-expansion: Multi-character strings in lists are joined and parsed like a single string
  • Input validation: Rejects ineligible CNPJs (all-zero base ID 00000000, all-zero branch 0000, or 12 numeric-only repeated digits)
  • Lazy evaluation: Check digits are calculated only when accessed (via properties)
  • Caching: Calculated values are cached for subsequent access
  • Type hints: Built with Python 3.10+ type annotations
  • Minimal dependencies: Only lacus.utils
  • Error handling: Specific types for type, length, and invalid CNPJ scenarios (TypeError vs Exception semantics)

Installation

$ pip install cnpj-dv

Quick Start

from cnpj_dv import CnpjCheckDigits

Basic usage:

check_digits = CnpjCheckDigits("914157320007")

check_digits.first   # '9'
check_digits.second  # '3'
check_digits.both    # '93'
check_digits.cnpj    # '91415732000793'

With alphanumeric CNPJ (new format):

check_digits = CnpjCheckDigits("MGKGMJ9X0001")

check_digits.first   # '6'
check_digits.second  # '8'
check_digits.both    # '68'
check_digits.cnpj    # 'MGKGMJ9X000168'

Usage

The main resource of this package is the class CnpjCheckDigits. Through an instance, you access CNPJ check-digit information:

  • __init__: CnpjCheckDigits(str | list[str]) — 12–14 alphanumeric characters after sanitization (formatting stripped from strings; letters uppercased). Only the first 12 characters are used as the base; if you pass 13 or 14 characters (e.g. a full CNPJ including prior check digits), characters 13–14 are ignored and the digits are recalculated.
  • first: First check digit (13th character of the full CNPJ). Lazy, cached.
  • second: Second check digit (14th character of the full CNPJ). Lazy, cached.
  • both: Both check digits concatenated as a string.
  • cnpj: The complete CNPJ as a string of 14 characters (12 base characters + 2 check digits).

Input formats

The CnpjCheckDigits class accepts multiple input formats:

String input: raw digits and/or letters, or formatted CNPJ (e.g. 91.415.732/0007-93, MG.KGM.J9X/0001-68). Non-alphanumeric characters are removed; lowercase letters are uppercased.

List of strings: each element must be a string; values are concatenated and then parsed like a single string (e.g. ["9","1","4",…], ["9141","5732","0007"], ["MG","KGM","J9X","0001"]). Non-string elements are not allowed.

# String — plain, formatted, or with existing check digits (only first 12 chars used)
CnpjCheckDigits("914157320007")
CnpjCheckDigits("91.415.732/0007")
CnpjCheckDigits("91415732000793")

# List of strings — single- or multi-character elements
CnpjCheckDigits(["9", "1", "4", "1", "5", "7", "3", "2", "0", "0", "0", "7"])
CnpjCheckDigits(["9141", "5732", "0007"])
CnpjCheckDigits(["MG", "KGM", "J9X", "0001"])

Errors & exceptions handling

This package uses TypeError vs Exception semantics: type errors indicate incorrect API use (e.g. wrong type); exceptions indicate invalid or ineligible data (e.g. invalid length or business rules). You can catch specific classes or use the base classes.

  • CnpjCheckDigitsTypeError — base class for type errors; extends Python's TypeError
  • CnpjCheckDigitsInputTypeError — input is not str or list[str] (or list contains a non-string element)
  • CnpjCheckDigitsException — base class for data/flow exceptions; extends Exception
  • CnpjCheckDigitsInputLengthException — sanitized length is not 12–14
  • CnpjCheckDigitsInputInvalidException — base ID 00000000, branch ID 0000, or 12 identical numeric digits (repeated-digit pattern)
from cnpj_dv import (
    CnpjCheckDigits,
    CnpjCheckDigitsException,
    CnpjCheckDigitsInputInvalidException,
    CnpjCheckDigitsInputLengthException,
    CnpjCheckDigitsInputTypeError,
)

# Input type (e.g. integer not allowed)
try:
    CnpjCheckDigits(12345678000100)
except CnpjCheckDigitsInputTypeError as e:
    print(e)  # CNPJ input must be of type string or string[]. Got integer number.

# Length (must be 12–14 alphanumeric characters after sanitization)
try:
    CnpjCheckDigits("12345678901")
except CnpjCheckDigitsInputLengthException as e:
    print(e)  # CNPJ input "12345678901" does not contain 12 to 14 characters. Got 11.

# Invalid (e.g. all-zero base or branch, or repeated numeric digits)
try:
    CnpjCheckDigits("000000000001")
except CnpjCheckDigitsInputInvalidException as e:
    print(e)  # CNPJ input "000000000001" is invalid. Base ID "00000000" is not eligible.

# Any data exception from the package
try:
    CnpjCheckDigits("000000000001")
except CnpjCheckDigitsException as e:
    print(e)

Other available resources

Import from cnpj_dv:

  • CNPJ_MIN_LENGTH: 12
  • CNPJ_MAX_LENGTH: 14
  • Exceptions: see above

Calculation algorithm

The package computes check digits with the official Brazilian modulo-11 rules extended to alphanumeric characters:

  1. Character value: each character contributes ord(character) − 48 (so 09 stay 0–9; letters use their ASCII offset from 0).
  2. Weights: from right to left, multiply by weights that cycle 2, 3, 4, 5, 6, 7, 8, 9, then repeat from 2.
  3. First check digit (13th position): apply steps 1–2 to the first 12 base characters; let r = sum % 11. The digit is 0 if r < 2, otherwise 11 − r.
  4. Second check digit (14th position): apply steps 1–2 to the first 12 characters plus the first check digit; same formula for r.

Contribution & Support

We welcome contributions! Please see our Contributing Guidelines for details. If you find this project helpful, please consider:

License

This project is licensed under the MIT License — see the LICENSE file for details.

Changelog

See CHANGELOG for a list of changes and version history.


Made with ❤️ by Lacus Solutions

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cnpj_dv-2.0.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cnpj_dv-2.0.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file cnpj_dv-2.0.0.tar.gz.

File metadata

  • Download URL: cnpj_dv-2.0.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for cnpj_dv-2.0.0.tar.gz
Algorithm Hash digest
SHA256 31d12b3e696ffeca5d6a5545abe54c1489ea59f41d3e485a44f38593b1c3119d
MD5 ffe636c0e06ff7915a22544ef6ad1e15
BLAKE2b-256 a51dea6769bf3688e983f4d5a26525adbee53a8b3ce4783ca4a42176626e6f15

See more details on using hashes here.

File details

Details for the file cnpj_dv-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: cnpj_dv-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for cnpj_dv-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f335201d26088b6f7373b06634fd0267f1644bfeb9cad1104a49aea2e87c6f3a
MD5 5266f2589057bdd246c404d6ee87b775
BLAKE2b-256 f8894e5e6f76aefa586d326575e82b4893e99d35f2870b84888432e5c3cbde0b

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 files

1.0.0

2 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