Skip to content

Instantly share code, notes, and snippets.

@telenieko
Created February 9, 2015 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telenieko/b203c58df00150182765 to your computer and use it in GitHub Desktop.
Save telenieko/b203c58df00150182765 to your computer and use it in GitHub Desktop.
Ejemplo de conversión A3 > ContaSOL usando importasol.
""" Importar un libro diario de A3 a ContaSOL.
A3 permite listar el libro Diario en Excel, lo que permite su conversion
e importacion en otros programas como ContaSOL.
El libro generado es un Excel formato "antiguo" con una hoja por cada mes
del ejercicio.
A parte de importasol, hay que tener los paquetes: click, xlrd, Bunch
"""
import click
from importasol.entorno import EntornoSOL
import os
import logging
import xlrd
from bunch import Bunch
from importasol.db.contasol import APU, MAE
log = logging.getLogger()
def procesar_fila(row):
d = Bunch({})
d.refint = row[0].value
d.fecha = row[1].value
d.asiento = row[2].value
d.orden = row[3].value
d.concepto = row[4].value
d.doc = row[5].value
d.cuenta = str(int(row[6].value))
if len(d.cuenta) < 8:
# Algunas cuentas tienen solo 4 digitos ...
d.cuenta = d.cuenta + '.0'
d.cuenta_desc = row[7].value
d.debe = row[8].value
d.haber = row[9].value
return d
def procesar_hoja(ws, entorno):
log.info("Procesando hoja A3 %s" % ws.name)
i = 1
started = False
fecha = None
asiento = None
orden = 0
while True:
row = ws.row(i)
i += 1
if row[0].value == 'Ref. Int.':
log.debug("La hoja empieza en fila %d" % i)
started = True
continue
elif not started:
continue
elif row[3].value == '':
break
rowdata = procesar_fila(row)
if rowdata.fecha is not None and rowdata.fecha != '':
fecha = rowdata.fecha
if rowdata.asiento is not None and rowdata.asiento != '':
asiento = rowdata.asiento
orden = 0
orden += 1
if rowdata.cuenta not in entorno.SEEN_ACCOUNTS:
m = MAE()
m.cuenta = rowdata.cuenta
m.descripcion = rowdata.cuenta_desc
entorno.bind(m)
entorno.SEEN_ACCOUNTS.append(rowdata.cuenta)
ap = APU()
ap.diario = 1
ap.fecha = fecha
ap.asiento = asiento
ap.orden = orden
ap.cuenta = rowdata.cuenta
ap.concepto = rowdata.concepto
ap.documento = rowdata.doc
if rowdata.debe != 0:
ap.debe = rowdata.debe
if rowdata.haber != 0:
ap.haber = rowdata.haber
entorno.bind(ap)
if i >= ws.nrows:
break
def procesar_archivo(fuente, entorno):
log.info("Procesando archivo A3 %s" % fuente)
wb = xlrd.open_workbook(fuente)
for ws in wb.sheets():
procesar_hoja(ws, entorno)
@click.command()
@click.argument('fuente', type=click.Path(exists=True))
@click.option('--level', default=logging.INFO, help='Log level')
def a3sol(fuente, level):
logging.basicConfig(level=level)
srcname = fuente[:fuente.find('.')]
outdir = "%s" % srcname
entorno = EntornoSOL()
entorno.SEEN_ACCOUNTS = []
if not os.path.exists(outdir):
os.mkdir(outdir)
procesar_archivo(fuente, entorno)
entorno.generar_xls(outdir)
logging.info("Salida en %s" % os.path.abspath(outdir))
if __name__ == '__main__':
a3sol()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment