From 622a6191ae14a33d4d8406167518f08edb9fab74 Mon Sep 17 00:00:00 2001 From: RODGGER SILVA Date: Tue, 7 Feb 2023 14:57:00 -0400 Subject: [PATCH] Script para renomear boletos com seus valores internos --- README | 0 financeiro_renomear_boletos.py | 155 +++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 README create mode 100644 financeiro_renomear_boletos.py diff --git a/README b/README new file mode 100644 index 0000000..e69de29 diff --git a/financeiro_renomear_boletos.py b/financeiro_renomear_boletos.py new file mode 100644 index 0000000..3c912e3 --- /dev/null +++ b/financeiro_renomear_boletos.py @@ -0,0 +1,155 @@ +import os +import sys +import shutil +import subprocess +try: + from pdfminer.high_level import extract_text +except: + sys.exit( color.RED+"Deve instalar pdfminer: pip3 install pdfminer"+ color.END ) + + +class color: + PURPLE = '\033[1;35;48m' + CYAN = '\033[1;36;48m' + BOLD = '\033[1;37;48m' + BLUE = '\033[1;34;48m' + GREEN = '\033[1;32;48m' + YELLOW = '\033[1;33;48m' + RED = '\033[1;31;48m' + BLACK = '\033[1;30;48m' + UNDERLINE = '\033[4;37;48m' + END = '\033[1;37;0m' + +print(color.PURPLE+"\ + /$$$$$$$ \n\ +| $$__ $$ \n\ +| $$ \ $$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$/$$$$ /$$$$$$ /$$$$$$ /$$$$$$ \n\ +| $$$$$$$//$$__ $| $$__ $$/$$__ $| $$_ $$_ $$/$$__ $$|____ $$/$$__ $$\n\ +| $$__ $| $$$$$$$| $$ \ $| $$ \ $| $$ \ $$ \ $| $$$$$$$$ /$$$$$$| $$ \__/\n\ +| $$ \ $| $$_____| $$ | $| $$ | $| $$ | $$ | $| $$_____//$$__ $| $$ \n\ +| $$ | $| $$$$$$| $$ | $| $$$$$$| $$ | $$ | $| $$$$$$| $$$$$$| $$ \n\ +|__/ |__/\_______|__/ |__/\______/|__/ |__/ |__/\_______/\_______|__/ \n\ + /$$$$$$$ /$$ /$$ \n\ +| $$__ $$ | $$ | $$ \n\ +| $$ \ $$ /$$$$$$| $$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ \n\ +| $$$$$$$ /$$__ $| $$/$$__ $|_ $$_/ /$$__ $$/$$_____/ \n\ +| $$__ $| $$ \ $| $| $$$$$$$$ | $$ | $$ \ $| $$$$$$ \n\ +| $$ \ $| $$ | $| $| $$_____/ | $$ /$| $$ | $$\____ $$ \n\ +| $$$$$$$| $$$$$$| $| $$$$$$$ | $$$$| $$$$$$//$$$$$$$/ \n\ +|_______/ \______/|__/\_______/ \___/ \______/|_______/ \n\ + \n\ +RENOMEAR BOLETOS "+color.END) + + +def read_pdf(path_pdf): + try: + content_pdf = str(extract_text(path_pdf).replace("\u00a0", "\u0020").encode('ascii', 'ignore')[19:]) + except: + print(color.RED+"Não pode ler o pdf:" + path_pdf,color.END) + return None + return content_pdf + + + +def parse_boleto(content_folder, path_new_boletos): + for x in content_folder["path"]: + content_boleto = read_pdf(x) + if type(content_boleto) == type(None): + continue + +######## NF ######## + str_nf = "" + index_nf = content_boleto.find("Valor do documentoNF") + if( index_nf == -1 ): + print(color.RED+"Não foi possível encontrar os campo (NF) do boleto: " + x,color.END) + continue + + content_boleto = content_boleto[(index_nf + 20):] + + try: + end_nf = content_boleto.find("/") + str_nf = content_boleto[:end_nf] + except: + print(color.RED+"Não foi possível encontrar NF do boleto: " + x, color.END) + continue + + content_boleto = content_boleto[(end_nf + 1):] + +######## DATA VENCIMENTO ######## + expiration = "" + try: + index_date = content_boleto.find("/") + if( index_date == -1 ): + print(color.RED+"Não foi possível encontrar os campo (vencimento) do boleto: " + x,color.END) + continue + + content_boleto = content_boleto[(index_date - 2):] + expiration = content_boleto[ :2 ] + "." + content_boleto[ 3:5 ] + "." + content_boleto[ 6:10 ] + except: + print(color.RED+"Não foi possível encontrar vencimento"+color.END) + continue + + +######## Nome Empresa ######## + payer = "" + try: + index_payer = content_boleto.find("Pagador") + if( index_payer == -1): + print(color.RED+"Não foi possível encontrar os campo (pagador) do boleto: " + x,color.END) + continue + + content_boleto = content_boleto[ (index_payer + 7 ): ] + + end_payer = index_payer = content_boleto.find("CNPJ") + if(end_payer == -1): + print(color.RED+"Não foi possível encontrar o fim (pagador) do boleto: " + x,color.END) + continue + + payer = content_boleto[:(end_payer - 1)] + + except: + print(color.RED+"Não foi possível manipular pagador: " + x,color.END) + continue + + if not path_new_boletos or not os.path.isdir( path_new_boletos ) : + path_new_boletos = content_folder["folder"][0] + + + rename_boleto = "{}\\BOLETO NF {} {} VENCIMENTO {}.pdf".format( path_new_boletos, str_nf, payer, expiration ) + try: + shutil.copy2( x, rename_boleto) + print(color.GREEN+ os.path.basename(x) + " -> " + os.path.basename(rename_boleto)+"\n"+color.END ) + except shutil.SameFileError: + print(color.RED + "Não é possível renomear o próprio boleto renomeado na mesma pasta: "+ color.END + x +"\n"+ color.END) + except FileNotFoundError: + print( color.RED + "O boleto para ser renomeado não foi encontrado: "+ + color.END + x + "\n" ) + except: + print( color.RED + "Erro renomear o boleto: "+ + color.END + x + "\n" ) + + + +def get_files(folder): + content_folder = { + "folder":[], + "path": [], + "name_ext": [], + "name": [] + } + + for top, dirs, files in os.walk(folder): + for file in files: + content_folder["path"].append(os.path.join(top, file)) + content_folder["folder"].append(os.path.dirname(content_folder["path"][0])) + content_folder["name_ext"].append(file) + content_folder["name"].append(os.path.splitext(file)[0]) + + if len(content_folder["name_ext"]) == 0: + sys.exit(color.RED+"Não existe arquivos na pasta "+folder+"\nTerminado"+color.END) + + return content_folder + +if __name__ == "__main__": + folder_boletos = input(color.BOLD+color.UNDERLINE+'\nPasta dos boletos: '+color.END) + folder_rename_boletos = input(color.BOLD+color.UNDERLINE+"\nPasta dos boletos renomeados (Vazio colocará da pasta acima): "+color.END) + print("\n") + parse_boleto(get_files(folder_boletos),folder_rename_boletos )