4
Hello, everybody.
Imagine the following situation:
I have a directory with several Xmls Nfe files and need to move them by issuer (CNPJ) and/or issue date to another directory.
I’ve been able to iterate through the elements of a file, just, and copy it from one directory to another, if it’s in the stipulated condition, but I don’t know how to do it with all the files. If anyone can help, I’d appreciate it!
One of the analyzed Xmls has this structure:
<?xml version="1.0" encoding="UTF-8" ?>
- <nfeProc versao="4.00" xmlns="http://www.portalfiscal.inf.br/nfe">
- <NFe xmlns="http://www.portalfiscal.inf.br/nfe">
- <infNFe Id="NFe16190119250367000191550030000618321111024017" versao="4.00">
- <emit>
<CNPJ>19250367000191</CNPJ>
<xNome>M B PRODUTOS ALIMENTICIOS LTDA</xNome>
Follow the code I made to read a file, only.
import shutil
import xml.etree.ElementTree as ET
tree = ET.parse(r'/Users/Jp/Desktop/teste.xml')
root = tree.getroot()
NFe = root.find('{http://www.portalfiscal.inf.br/nfe}NFe')
infNFe = NFe.find('{http://www.portalfiscal.inf.br/nfe}infNFe')
emit = infNFe.find('{http://www.portalfiscal.inf.br/nfe}emit')
cnpj = emit.find('{http://www.portalfiscal.inf.br/nfe}CNPJ')
if cnpj.text == '19250367000191':
shutil.copy(r'/Users/Jp/Desktop/teste.xml', r'/Users/Jp/Desktop/Hiper')
print(cnpj.text)
else:
print('Não tem o CNPJ solicitado')
I use version 3.7 of Python!
In this new schema, I need to copy files that have the attribute "AR_CENTRAL_PG1_V1"!
Follow the schema:
- <XMLDataFile>
- <Groups fileName="092449274_000059131.12097869.NFe16141204842563000420551000000591311003460046.SERVIDOR250.NDDigitaleFormsConnectorService7.xml" pjlHeaderData="" pjlFooterData="">
- <Formulary>
- <XMLHead>
- <Form name="AR_HIPER_PG1_V1">
<PrinterName>HP LaserJet P1505</PrinterName>
<RawData />
<InsertInCold>1</InsertInCold>
<IsDanfe>1</IsDanfe>
<DocumentUser>FormsUser</DocumentUser>
<DocumentTitle>No Name</DocumentTitle>
<pjlHeaderData />
<pjlFooterData />
<AutomaticFields />
</Form>
</XMLHead>
Lacobus, good afternoon!
– user139559
I’ll test your code, thank you for the force!
– user139559
Hello, Lacobus. After testing the code, you are returning the following message: > line 9, in obten_cnpj_emissor Return Node[0]. text Indexerror: list index out of range What I did wrong?
– user139559
This error indicates that CNPJ was not found in the input XML file. You could attach it in your question ?
– Lacobus
I attached a part of XML!
– user139559
@user139559: I added an error control if CNPJ cannot be extracted from the XML file properly. That should solve the problem you mentioned.
– Lacobus
Good afternoon, Lacobus! Your code has solved much of the problem, thank you. But I have another xml schema that I can’t read with this code, could you help me? I’ll attach the new schema to the question!
– user139559
@user139559 Elaborate a new question with the schema you quoted. Separating the two questions would be better.
– Lacobus