🚀 Full support for the new alphanumeric CNPJ format.
A Python utility to generate valid CNPJ (Brazilian Business Tax ID) values.
Python Support
| Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ |
Features
- ✅ Alphanumeric CNPJ: Generates 14-character CNPJ with optional numeric, alphabetic, or alphanumeric (default) character sets
- ✅ Optional prefix: Provide 0–12 alphanumeric characters to fix the start of the CNPJ (e.g. base ID) and generate the rest with valid check digits
- ✅ Formatting: Option to return the standard formatted string (
00.000.000/0000-00) - ✅ Reusable generator:
CnpjGeneratorclass with default options and per-call overrides - ✅ Type hints: Built for Python 3.10+ with full type annotations
- ✅ Minimal dependencies: Only internal packages
lacus.utilsandcnpj-dvfor random sequence generation and check-digit calculation - ✅ Error handling: Specific type errors and exceptions for invalid options
Installation
$ pip install cnpj-gen
Quick Start
from cnpj_gen import cnpj_gen
Basic usage:
cnpj_gen() # e.g. 'AB123CDE000155' (14-char alphanumeric)
cnpj_gen(format=True) # e.g. 'AB.123.CDE/0001-55'
cnpj_gen(prefix='45623767') # e.g. '45623767ABCD96'
cnpj_gen( # e.g. '45.623.767/ABCD-96'
prefix='45623767',
format=True,
)
cnpj_gen(type='numeric') # e.g. '65453043000178' (digits only)
cnpj_gen(type='alphabetic') # e.g. 'ABCDEFGHIJKL80' (letters only, except check digits)
Options can also be passed as a mapping:
cnpj_gen({'format': True, 'type': 'numeric'})
Usage
Generator options
All options are optional:
| Option | Type | Default | Description |
|---|---|---|---|
format |
bool |
False |
When True, return the generated CNPJ in standard format (00.000.000/0000-00). Non-boolean values are coerced with bool(). |
prefix |
str |
'' |
Partial start string (0–12 alphanumeric chars). Only alphanumeric characters are kept and uppercased; missing characters are generated randomly and check digits are computed. |
type |
'numeric' | 'alphabetic' | 'alphanumeric' |
'alphanumeric' |
Character set for the randomly generated part (prefix is kept as-is after sanitization). Check digits are always numeric. |
Prefix rules: base ID (first 8 chars) and branch ID (chars 9–12) cannot be all zeros; 12 repeated digits (e.g. 777777777777) are also not allowed.
cnpj_gen (helper function)
Generates a valid CNPJ string. With no options, returns a 14-character alphanumeric CNPJ. This is a convenience wrapper around CnpjGenerator(options, ...).generate().
options(optional):CnpjGeneratorOptionsInput— aCnpjGeneratorOptionsinstance, a partial mapping, orNone. See Generator options.format,prefix,type(keyword-only): Per-option overrides whenoptionsis omitted or to layer on top of a mapping.
CnpjGenerator (class)
For reusable defaults or per-call overrides, use the class:
from cnpj_gen import CnpjGenerator
generator = CnpjGenerator(type='numeric', format=True)
generator.generate() # e.g. '73.008.535/0005-06'
generator.generate(prefix='12345678') # override for this call only
generator.options # current default options (CnpjGeneratorOptions)
__init__(options=None, *, format=None, prefix=None, type=None): Optional default options (plain mapping,CnpjGeneratorOptionsinstance, or keyword arguments).generate(options=None, *, format=None, prefix=None, type=None): Returns a valid CNPJ; per-call options override instance defaults for that call only.options: Property returning the default options used when per-call options are not provided (same instance as used internally; mutating it affects futuregeneratecalls).
Default options on the instance; per-call overrides:
generator = CnpjGenerator(format=True)
generator.generate() # formatted CNPJ
generator.generate(format=False) # this call only: unformatted
generator.generate() # formatted again (instance defaults preserved)
CnpjGeneratorOptions (class)
Holds options (format, prefix, type) with validation and merge support:
from cnpj_gen import CnpjGeneratorOptions
options = CnpjGeneratorOptions(
prefix='AB123XYZ',
type='numeric',
format=True,
)
options.prefix # 'AB123XYZ'
options.type # 'numeric'
options.format # True
options.set({'format': False}) # merge and return self
options.all # immutable shallow snapshot of current options
__init__(options=None, *extra_overrides, format=None, prefix=None, type=None): Options merged in order (later overrides win).format,prefix,type: Properties with setters;prefixis validated (base/branch ineligible, repeated digits).set(options): Update multiple options at once; omitted fields keep their current value; returnsself.all: Read-only snapshot of current options (MappingProxyType).DEFAULT_FORMAT,DEFAULT_PREFIX,DEFAULT_TYPE: Class-level default constants.
API
Exports
cnpj_gen:(options=None, *, format=None, prefix=None, type=None) -> strCnpjGenerator: Class to generate CNPJ with optional default options and per-call overrides.CnpjGeneratorOptions: Class holding options (format,prefix,type) with validation and merge.CNPJ_LENGTH:14(constant).CNPJ_PREFIX_MAX_LENGTH:12(constant).- Types:
CnpjType,CnpjGeneratorOptionsInput,CnpjGeneratorOptionsType. - Exceptions:
CnpjGeneratorTypeError,CnpjGeneratorOptionsTypeError,CnpjGeneratorException,CnpjGeneratorOptionPrefixInvalidException,CnpjGeneratorOptionTypeInvalidException.
Errors & Exceptions
This package uses TypeError subclasses for invalid option types and Exception subclasses for invalid option values (prefix or type). You can catch specific classes or the base types.
- CnpjGeneratorTypeError — base for option type errors
- CnpjGeneratorOptionsTypeError — an option has the wrong type (e.g.
prefixnot a string) - CnpjGeneratorException — base for option value exceptions
- CnpjGeneratorOptionPrefixInvalidException — prefix invalid (e.g. all-zero base/branch, repeated digits)
- CnpjGeneratorOptionTypeInvalidException —
typeis not one of'numeric','alphabetic','alphanumeric'
from cnpj_gen import (
cnpj_gen,
CnpjGeneratorOptionsTypeError,
CnpjGeneratorOptionPrefixInvalidException,
CnpjGeneratorOptionTypeInvalidException,
CnpjGeneratorException,
)
# Option type (e.g. `prefix` must be string)
try:
cnpj_gen(prefix=123)
except CnpjGeneratorOptionsTypeError as e:
print(e.option_name, e.expected_type, e.actual_type)
# Invalid prefix (e.g. all-zero base)
try:
cnpj_gen(prefix='000000000001')
except CnpjGeneratorOptionPrefixInvalidException as e:
print(e.reason, e.actual_input)
# Invalid type value
try:
cnpj_gen(type='invalid')
except CnpjGeneratorOptionTypeInvalidException as e:
print(e.expected_values, e.actual_input)
# Any exception from the package
try:
cnpj_gen(prefix='000000000000')
except CnpjGeneratorException as e:
print(e)
Contribution & Support
We welcome contributions! Please see our Contributing Guidelines for details. If you find this project helpful, please consider:
- ⭐ Starring the repository
- 🤝 Contributing to the codebase
- 💡 Suggesting new features
- 🐛 Reporting bugs
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-gen 2.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cnpj_gen-2.0.3.tar.gz | 15.3 kB | Without provenance | Details for cnpj_gen-2.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cnpj_gen-2.0.3-py3-none-any.whl | Python 3 | none | any | Details for cnpj_gen-2.0.3-py3-none-any.whl |
Total release size: 29.6 kB
Release files / cnpj_gen-2.0.3.tar.gz
| Download URL | cnpj_gen-2.0.3.tar.gz |
|---|---|
| Size | 15.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ae083d7700187ab10a98896b130099e7d1fbc70c0839cd1203e0f6044303a360
|
|
BLAKE2b-256 checksum How to use checksums |
6d80a9fa9c5cb7f2b9f2f669e8ddfee7149a34ba1fa3ca9dc03f5cbf346b11da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.6
|
Release files / cnpj_gen-2.0.3-py3-none-any.whl
| Download URL | cnpj_gen-2.0.3-py3-none-any.whl |
|---|---|
| Size | 14.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1e05a7c777554addc94d0eb22c629166017a4f3b182be075b3a3f74fcb486966
|
|
BLAKE2b-256 checksum How to use checksums |
2e0fc079f0c6f5b5e57f167a992ca78655c3f7e870e85728d77b3b4c3f1e8d6b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.6
|
Security for cnpj-gen 2.0.3
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.