Created
July 21, 2018 01:02
-
-
Save categulario/d51f934b0ec065fabd1d0e7e0f97858b to your computer and use it in GitHub Desktop.
Lee información de facturas en formato cfdi 3.3 y suma los impuestos
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
from xml.dom.minidom import parse | |
import os | |
import sys | |
def main(): | |
if len(sys.argv) < 2: | |
exit('Usage: sum.py folder-with-xmls') | |
dirname = sys.argv[1] | |
if not os.path.isdir(dirname): | |
exit('argument must be a directory') | |
total = 0.0 | |
for filename in os.listdir(dirname): | |
if not filename.endswith('.xml'): | |
continue | |
dom = parse(os.path.join(dirname, filename)) | |
print(dom.getElementsByTagName('cfdi:Comprobante')[0].getAttribute('Fecha')) | |
for item in dom.getElementsByTagName('cfdi:Concepto'): | |
print('\t{}…: {}x{}'.format( | |
item.getAttribute('Descripcion')[:15], | |
item.getAttribute('ValorUnitario'), | |
item.getAttribute('Cantidad'), | |
)) | |
for tax in item.getElementsByTagName('cfdi:Traslado'): | |
amount = float(tax.getAttribute('Importe')) | |
print('\t\t{}'.format(amount)) | |
total += amount | |
print(total) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment