Skip to content

Instantly share code, notes, and snippets.

@MoisesTedeschi
Created August 17, 2021 22:24
Show Gist options
  • Save MoisesTedeschi/b83a8ff5750a01692b1d2624b238e90e to your computer and use it in GitHub Desktop.
Save MoisesTedeschi/b83a8ff5750a01692b1d2624b238e90e to your computer and use it in GitHub Desktop.
Validador de CPF e/ou CNPJ com Python
# Com biblioteca: pip install pycpfcnpj
# https://github.com/matheuscas/pycpfcnpj
from pycpfcnpj import cpfcnpj
cpf_number = '11144477735'
masked_cpf_number = '111.444.777-35'
cnpj_number = '11444777000161'
masked_cnpj_number = '11.444.777/0001-61'
print cpfcnpj.validate(cpf_number)
print cpfcnpj.validate(masked_cpf_number)
print cpfcnpj.validate(cnpj_number)
print cpfcnpj.validate(masked_cnpj_number)
# Gerando CPF ou CNPJ
from pycpfcnpj import gen
gen.cpf()
gen.cnpj()
# Regex
'''
Para CPF
/^\d{3}\.\d{3}\.\d{3}\-\d{2}$/
Para CNPJ
/^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/
Para ambos ao mesmo tempo
/(^\d{3}\.\d{3}\.\d{3}\-\d{2}$)|(^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$)/
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment