-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_files_aula.py
More file actions
95 lines (77 loc) · 3.32 KB
/
Copy pathxml_files_aula.py
File metadata and controls
95 lines (77 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import xml.etree.ElementTree as Et
from datetime import date
class Read_xml():
def __init__(self, directory) -> None:
self.directory = directory
def all_files(self):
return [ os.path.join(self.directory, arq) for arq in os.listdir(self.directory)
if arq.lower().endswith(".xml")]
def nfe_data(self, xml):
root = Et.parse(xml).getroot()
nsNFe = {"ns": "http://www.portalfiscal.inf.br/nfe"}
#DADOS DA NFE
NFe = self.check_none(root.find("./ns:NFe/ns:infNFe/ns:ide/ns:nNF", nsNFe)) #1
serie = self.check_none(root.find("./ns:NFe/ns:infNFe/ns:ide/ns:serie", nsNFe)) #2
data_emissao = self.check_none(root.find("./ns:NFe/ns:infNFe/ns:ide/ns:dhEmi", nsNFe))
data_emissao = F"{data_emissao[8:10]}/{data_emissao[5:7]}/{data_emissao[:4]}"
# DADOS EMITENTES
chave = self.check_none(root.find("./ns:protNFe/ns:infProt/ns:chNFe", nsNFe))
cnpj_emitente = self.check_none(root.find("./ns:NFe/ns:infNFe/ns:emit/ns:CNPJ", nsNFe))
nome_emitente = self.check_none(root.find("./ns:NFe/ns:infNFe/ns:emit/ns:xNome", nsNFe)) #1
cnpj_emitente = self.format_cnpj(cnpj_emitente)
valorNfe = self.check_none(root.find("./ns:NFe/ns:infNFe/ns:total/ns:ICMSTot/ns:vNF", nsNFe)) #13
data_importacao = date.today()
data_importacao = data_importacao.strftime('%d/%m/%Y')
data_saida = ""
usuario = ''
itemNota = 1
notas = []
for item in root.findall("./ns:NFe/ns:infNFe/ns:det", nsNFe):
# DADOS DO ITEM =======================================================================================
cod = self.check_none(item.find(".ns:prod/ns:cProd", nsNFe))
qntd = self.check_none(item.find(".ns:prod/ns:qCom", nsNFe))
descricao = self.check_none(item.find(".ns:prod/ns:xProd", nsNFe))
unidade_medida = self.check_none(item.find(".ns:prod/ns:uCom", nsNFe))
valorProd = self.check_none(item.find (".ns:prod/ns:vProd", nsNFe))
dados = [
NFe, #1
serie, #2
data_emissao, #3
chave, #4
cnpj_emitente, #5
nome_emitente, #6
valorNfe, #7
str(itemNota), #8 (itemNota como string)
cod, #9
qntd, #10
descricao, #11
unidade_medida, #12
valorProd, #13
data_importacao, #14
usuario, #15
data_saida #16
]
notas.append(dados)
itemNota +=1
return notas
def check_none(self, var):
if var == None:
return ""
else:
try:
return var.text.replace('.',',')
except:
return var.text
def format_cnpj(self, cnpj):
try:
cnpj = f'{cnpj[:2]}.{cnpj[2:5]}.{cnpj[5:8]}/{cnpj[8:12]}-{cnpj[12:14]}'
return cnpj
except:
return ""
if __name__ == "__main__":
xml = Read_xml('D:\\Empowerdata\\Python\\projetos\\projeto-gerenciamento\\notas_fiscais')
all = xml.all_files()
for i in all:
result = xml.nfe_data(i)
print(result)