Last active
April 13, 2022 15:08
-
-
Save PedroArSp/3d80e205db2d266ee93ea383cfaf8601 to your computer and use it in GitHub Desktop.
Quick function for formatting Brazilian company's tax ID (CNPJ) / Formatar CNPJ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Modulo format-cnpj | |
| """ | |
| def fmtCnpj(inStr:str)->str: | |
| """ | |
| Formata Cnpj para tradicional formatação ##.####.###/#####-## | |
| """ | |
| inStr = inStr.replace('.','').replace('-','').replace('/','') | |
| ret = '' | |
| inStr = '00000000000' + inStr | |
| inStr = inStr[-14:] | |
| if len(inStr)==14: | |
| ret=f'{inStr[:2]}.{inStr[2:5]}.{inStr[5:8]}/{inStr[8:12]}-{inStr[-2:]}' | |
| return ret | |
| def ValidaCPF(cpf:str)->bool: | |
| cpf = cpf.replace('.','').replace('-','') | |
| if len(cpf)!=11: | |
| print('Tamanho invalido. Deve ser 11. Tamanho:',len(cpf)) | |
| return False | |
| dig1 = sum([int(x)*(idx+1) for idx,x in enumerate(cpf[:9])])%11%10 | |
| dig2 = sum([int(x)*(idx+1) for idx,x in enumerate(cpf[1:9]+str(dig1))])%11%10 | |
| print(f'{dig1}{dig2} / {cpf[-2:]}') | |
| return f'{dig1}{dig2}'==cpf[-2:] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment