Skip to main content

Utility function to generate valid CNPJ (Brazilian employer ID)

Project description

cnpj-gen for Python

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

Utility function/class to generate valid 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-gen

Import

# Using class-based resource
from cnpj_gen import CnpjGenerator

# Or using function-based one
from cnpj_gen import cnpj_gen

Usage

Object-Oriented Usage

generator = CnpjGenerator()
cnpj = generator.generate()  # returns '65453043000178'

# With options
cnpj = generator.generate(
    format=True
)  # returns '65.453.043/0001-78'

cnpj = generator.generate(
    prefix='45623767'
)  # returns '45623767000296'

cnpj = generator.generate(
    prefix='456237670002',
    format=True
)  # returns '45.623.767/0002-96'

The options can be provided to the constructor or the generate() method. If passed to the constructor, the options will be attached to the CnpjGenerator instance. When passed to the generate() method, it only applies the options to that specific call.

generator = CnpjGenerator(format=True)

cnpj1 = generator.generate()  # '65.453.043/0001-78' (uses instance options)
cnpj2 = generator.generate(format=False)  # '65453043000178' (overrides instance options)
cnpj3 = generator.generate()  # '12.345.678/0001-95' (uses instance options again)

Functional programming

The helper function cnpj_gen() is just a functional abstraction. Internally it creates an instance of CnpjGenerator and calls the generate() method right away.

cnpj = cnpj_gen()  # returns '65453043000178'

cnpj = cnpj_gen(format=True)  # returns '65.453.043/0001-78'

cnpj = cnpj_gen(prefix='45623767')  # returns '45623767000296'

cnpj = cnpj_gen(prefix='456237670002', format=True)  # returns '45.623.767/0002-96'

Generator Options

Parameter Type Default Description
format bool | None False Whether to format the output with dots, slash, and dash
prefix str | None '' If you have CNPJ initials and want to complete it with valid digits. The string provided must contain between 0 and 12 digits. The branch ID (characters 8 to 11) cannot be "0000".

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

Project details


Download files

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

Source Distribution

cnpj_gen-1.0.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

cnpj_gen-1.0.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file cnpj_gen-1.0.0.tar.gz.

File metadata

  • Download URL: cnpj_gen-1.0.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for cnpj_gen-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c4af9ae4f40cce370a686ef86b9b325a0b9eb6d1401685da4b3942e6014618e3
MD5 12c060de827f770bbe27bc39ac8bc6ef
BLAKE2b-256 07ff6ea22dfc8fe8791ef89a414fda1114169d18222aaaffd9e6abbfafdafabf

See more details on using hashes here.

File details

Details for the file cnpj_gen-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: cnpj_gen-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for cnpj_gen-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 94e56f30b5142d8dc13743aecf7fd5d32a942542429d25c8a136142b1df2f057
MD5 959953cc54b282ec6e9faa3ef5739c10
BLAKE2b-256 647fb6aebe643a9d2a705a550009cf3a8ce2df01714650bcafdc8d03ca7df5c8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page