Skip to main content

⛔ ⛔ ⛔ DEPRECATED PACKAGE ⛔ ⛔ ⛔

This package (cnpj-cd) is no longer maintained and will not receive further updates.

For consistency reasons across BR Utils initiative, this project was renamed to cnpj-dv.

pip uninstall cnpj-cd
pip install cnpj-dv

And since v1 of cnpj-dv follows the same API of cnpj-cd, updating the imports should be enough:

- from cnpj_cd import CnpjCheckDigits
+ from cnpj_dv import CnpjCheckDigits

Visit the cnpj-dv repository for additional information and support.


cnpj-cd for Python

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

Utility class to calculate check digits on CNPJ (Brazilian employer ID).

Python Support

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

Installation

$ pip install cnpj-cd

Import

from cnpj_cd import CnpjCheckDigits

Usage

Basic Usage

# Calculate check digits from a 12-digit CNPJ base
check_digits = CnpjCheckDigits("914157320007")

print(check_digits.first_digit)    # returns 9
print(check_digits.second_digit)   # returns 3
print(check_digits.to_string())    # returns '91415732000793'
print(check_digits.to_list())      # returns [9, 1, 4, 1, 5, 7, 3, 2, 0, 0, 0, 7, 9, 3]

Input Formats

The CnpjCheckDigits class accepts multiple input formats:

String Input

# Plain string (non-numeric characters are automatically stripped)
check_digits = CnpjCheckDigits("914157320007")
check_digits = CnpjCheckDigits("91.415.732/0007")  # formatting is ignored
check_digits = CnpjCheckDigits("914157320007")      # 12 digits
check_digits = CnpjCheckDigits("91415732000793")    # 14 digits (only first 12 are used)

List of Strings

# List of single-character strings
check_digits = CnpjCheckDigits(["9", "1", "4", "1", "5", "7", "3", "2", "0", "0", "0", "7"])

# List with multi-digit strings (automatically flattened)
check_digits = CnpjCheckDigits(["914157320007"])     # flattens to individual digits
check_digits = CnpjCheckDigits(["91", "415", "732", "0007"])  # also flattens

List of Integers

# List of single-digit integers
check_digits = CnpjCheckDigits([9, 1, 4, 1, 5, 7, 3, 2, 0, 0, 0, 7])

# List with multi-digit integers (automatically flattened)
check_digits = CnpjCheckDigits([914157320007])       # flattens to individual digits
check_digits = CnpjCheckDigits([914, 157, 320, 7])  # also flattens

Properties

first_digit: int

Returns the first check digit (13th digit of the CNPJ).

check_digits = CnpjCheckDigits("914157320007")
print(check_digits.first_digit)  # returns 9

second_digit: int

Returns the second check digit (14th digit of the CNPJ).

check_digits = CnpjCheckDigits("914157320007")
print(check_digits.second_digit)  # returns 3

Methods

to_list() -> list[int]

Returns the complete CNPJ as a list of integers (12 base digits + 2 check digits).

check_digits = CnpjCheckDigits("914157320007")
print(check_digits.to_list())  # returns [9, 1, 4, 1, 5, 7, 3, 2, 0, 0, 0, 7, 9, 3]

to_string() -> str

Returns the complete CNPJ as a string (12 base digits + 2 check digits).

check_digits = CnpjCheckDigits("914157320007")
print(check_digits.to_string())  # returns '91415732000793'

Examples

from cnpj_cd import CnpjCheckDigits

# Calculate check digits for a CNPJ base
base = "914157320007"
check_digits = CnpjCheckDigits(base)

# Get individual check digits
first = check_digits.first_digit    # 9
second = check_digits.second_digit   # 3

# Get complete CNPJ
complete = check_digits.to_string()  # '91415732000793'

# Work with formatted input
formatted = CnpjCheckDigits("91.415.732/0007")
print(formatted.to_string())  # '91415732000793'

# Work with list input
list_input = CnpjCheckDigits([9, 1, 4, 1, 5, 7, 3, 2, 0, 0, 0, 7])
print(list_input.to_string())  # '91415732000793'

Error Handling

The package raises specific exceptions for different error scenarios:

CnpjTypeError

Raised when the input type is not supported (must be str, list[str], or list[int]).

from cnpj_cd import CnpjCheckDigits, CnpjTypeError

try:
    CnpjCheckDigits(12345678901234)  # int not allowed
except CnpjTypeError as e:
    print(e)  # CNPJ input must be of type str, list[str] or list[int]. Got "int".

CnpjInvalidLengthError

Raised when the input does not contain 12 to 14 digits.

from cnpj_cd import CnpjCheckDigits, CnpjInvalidLengthError

try:
    CnpjCheckDigits("12345678901")  # only 11 digits
except CnpjInvalidLengthError as e:
    print(e)  # Parameter "12345678901" does not contain 12 to 14 digits. Got 11.

CnpjCheckDigitsCalculationError

Raised when the check digit calculation fails due to invalid sequence length.

from cnpj_cd import CnpjCheckDigits, CnpjCheckDigitsCalculationError

# This is an internal error that should not occur in normal usage
# It happens when the sequence passed to _calculate() has invalid length

Features

  • ✅ Multiple Input Formats: Accepts strings, lists of strings, or lists of integers
  • ✅ Format Agnostic: Automatically strips non-numeric characters from string input
  • ✅ Auto-Expansion: Automatically expands multi-digit numbers in lists to individual digits
  • ✅ Lazy Evaluation: Check digits are calculated only when accessed (via properties)
  • ✅ Type Safety: Built with Python 3.10+ type hints
  • ✅ Zero Dependencies: No external dependencies required
  • ✅ Comprehensive Error Handling: Specific exceptions for different error scenarios

API Reference

CnpjCheckDigits Class

Constructor

CnpjCheckDigits(cnpj_digits: str | list[str] | list[int]) -> CnpjCheckDigits

Creates a new CnpjCheckDigits instance from the provided CNPJ base digits.

Parameters:

  • cnpj_digits (str | list[str] | list[int]): The CNPJ base digits (12-14 digits). Can be:
    • A string with 12-14 digits (formatting characters are ignored)
    • A list of strings (each string can be a single digit or multi-digit number)
    • A list of integers (each integer can be a single digit or multi-digit number)

Raises:

  • CnpjTypeError: If the input type is not supported
  • CnpjInvalidLengthError: If the input does not contain 12-14 digits

Returns:

  • CnpjCheckDigits: A new instance ready to calculate check digits

Properties

first_digit: int

The first check digit (13th digit of the CNPJ). Calculated lazily on first access.

second_digit: int

The second check digit (14th digit of the CNPJ). Calculated lazily on first access.

Methods

to_list() -> list[int]

Returns the complete CNPJ as a list of 14 integers (12 base digits + 2 check digits).

to_string() -> str

Returns the complete CNPJ as a string of 14 digits (12 base digits + 2 check digits).

Calculation Algorithm

The package calculates CNPJ check digits using the official Brazilian algorithm:

  1. First Check Digit (13th position):

    • Uses digits 1-12 of the CNPJ base
    • Applies weights: 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 (from right to left)
    • Calculates: sum(digit × weight) % 11
    • Result: 0 if remainder < 2, otherwise 11 - remainder
  2. Second Check Digit (14th position):

    • Uses digits 1-12 + first check digit
    • Applies weights: 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 (from right to left)
    • Calculates: sum(digit × weight) % 11
    • Result: 0 if remainder < 2, otherwise 11 - remainder

Dependencies

  • Python: >= 3.10

No external dependencies required.

Contribution & Support

We welcome contributions! Please see our Contributing Guidelines for details. But 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

Metadata

Release files for cnpj-cd 1.0.2

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

Source distribution (sdist)

Source distribution for cnpj-cd 1.0.2
File Size Uploaded Provenance Actions
cnpj_cd-1.0.2.tar.gz 10.1 kB Without provenance Details for cnpj_cd-1.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cnpj-cd 1.0.2
File Interpreter ABI Platform Actions
cnpj_cd-1.0.2-py3-none-any.whl Python 3 none any Details for cnpj_cd-1.0.2-py3-none-any.whl

Total release size: 18.0 kB

Release files / cnpj_cd-1.0.2.tar.gz

Download URL cnpj_cd-1.0.2.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f1fb2e0e5ab622d697e6373c06c25f9d29e298e1177a3bf550f11af893a9f7dd
BLAKE2b-256 checksum
How to use checksums
616ce15ea09300ed57f2b3418f7095f169013fcdfe74188ca226ee6a7ade5816
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / cnpj_cd-1.0.2-py3-none-any.whl

Download URL cnpj_cd-1.0.2-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2078a3a3a54208b98fa96a98ba9ffa39f02fd2f7aefd50a57828a20988707346
BLAKE2b-256 checksum
How to use checksums
79f2efe5e397ae1fb93e8cf8b49d6063239327f7b55bc9d904dfb929a29eac61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Security for cnpj-cd 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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