leer 2 archivos de excel con diferentes estructuras Hola tengo un programa que me lee archivos de excel esta echo en python 2.7 con Django 1.6.5
para leer el archivo de excel estoy usando la libreria "xlrd"
Mi programa lee y guarda la data del excel en una BD, el primer(Ripley) archivo de excel tiene una estructura, el segundo archivo(Saga) de excel su estructura es diferente al primero(Ripley) y no guarda la data del segundo archivo de excel , le dejo el codigo espero su pronta ayuda
MODELS.PY
Código Python:
Ver originalclass DataExcel(models.Model): tienda = ( (1,'Ripley'), (2,'Saga') ) name = models.CharField('Nombre', max_length=20) fecha_inicio = models.DateField(verbose_name='Fecha inicio', max_length=10) fecha_fin = models.DateField(verbose_name='Fecha Fin', max_length=10) file = models.FileField(upload_to=settings.MEDIA_ROOT,verbose_name='Archivo') tienda = models.IntegerField(choices=tienda) active = models.BooleanField(default=True) class Saga(models.Model): Id_DataExcel = models.ForeignKey(DataExcel) Codigo = models.IntegerField(max_length=10, blank=True) Consignacion = models.CharField(max_length=20, blank=True) Tipo_Trx = models.CharField(max_length=20, blank=True) Fecha_proceso = models.DateField() Fecha_Trx = models.DateField() Local = models.CharField(max_length=30) Ccosto = models.CharField(max_length=30) SubClase = models.CharField(max_length=30) Ean = models.IntegerField(max_length=60, blank=True) Sku = models.IntegerField(max_length=60, blank=True) Descripcion_Sku = models.CharField(max_length=200, blank=True) Caja_Vta = models.IntegerField(max_length=60, blank=True) Nro_Trx_Vta = models.IntegerField(max_length=60, blank=True) Cantidad = models.FloatField() Base_imponible_vta = models.FloatField() Impto_vta = models.FloatField() Monto_timbrado_vta = models.FloatField() Tipo_comision = models.CharField(max_length=60, blank=True) Monto_comision = models.IntegerField(max_length=20, blank=True) Costo_vta = models.FloatField() Igv_costo_vta = models.FloatField() Total_costo_vta = models.FloatField() Local_Trx_Ori = models.IntegerField(max_length=60, blank=True) Fecha_Trx_Ori = models.DateField() Caja_Trx_Ori = models.IntegerField(max_length=20, blank=True) Nro_Trx_ori = models.IntegerField(max_length=20, blank=True) class Ripley(models.Model): Id_DataExcel = models.ForeignKey(DataExcel) CodSucursal = models.IntegerField(max_length=60, blank=True) Sucursal = models.CharField(max_length=60, blank=True) CodDivision = models.CharField(max_length=200, blank=True) Division = models.CharField(max_length=300, blank=True) CodArea = models.CharField(max_length=60, blank=True) Area = models.CharField(max_length=30, blank=True) CodDpto = models.CharField(max_length=60, blank=True) Dpto = models.CharField(max_length=35, blank=True) CodLinea = models.IntegerField(max_length=200, blank=True) Linea = models.CharField(max_length=35, blank=True) CodSubLinea = models.CharField(max_length=200, blank=True) SubLinea = models.CharField(max_length=200, blank=True) CodModelo = models.IntegerField(max_length=60, blank=True) Modelo = models.CharField(max_length=50, blank=True) CodVariacion = models.IntegerField(max_length=60, blank=True) Variacion = models.CharField(max_length=50, blank=True) CodMarca = models.IntegerField(max_length=200, blank=True) Marca = models.CharField(max_length=60, blank=True) CodProveedor = models.IntegerField(max_length=200, blank=True) Proveedor = models.CharField(max_length=60, blank=True) Temporada = models.CharField(max_length=60, blank=True) Procedencia = models.CharField(max_length=60, blank=True) TipoNeg = models.CharField(max_length=15, blank=True) Color = models.CharField(max_length=200, blank=True) Talla = models.CharField(max_length=200, blank=True) SemAntig = models.CharField(max_length=200, blank=True) ConVenta = models.CharField(max_length=200, blank=True) ConStockOH = models.CharField(max_length=200, blank=True) EsRebate = models.CharField(max_length=200, blank=True) ConCobertura = models.CharField(max_length=200, blank=True) VtaUnd = models.CharField(max_length=200, blank=True) VtaSMF = models.FloatField() Contr = models.FloatField() Costo = models.FloatField() Stock_OH = models.CharField(max_length=200, blank=True) Costo_OH = models.FloatField() Cbt = models.CharField(max_length=200, blank=True)
VIEWS.PY
Código Python:
Ver originalclass DataExcelProcessView(TemplateView): template_name = 'app/volver.html' def get(self, request, *args, **kwargs): return super(DataExcelProcessView, self).get(request, args, kwargs) def get_context_data(self, **kwargs): ctx = super(DataExcelProcessView, self).get_context_data(**kwargs) ctx['lista_data'] = self.process_file() return ctx def process_file(self): object = DataExcel.objects.get(pk=self.kwargs['pk']) book = xlrd.open_workbook(object.file.path) hoja1 = book.sheet_by_index(0) print object.tienda if object.tienda == 1: #=============================================RIPLEY============================================================ fields = ('CodSucursal', 'Sucursal', 'CodDivision', 'Division', 'CodArea', 'Area', 'CodDpto', 'Dpto', 'CodLinea', 'Linea', 'CodSubLinea', 'SubLinea', 'CodModelo', 'Modelo', 'CodVariacion', 'Variacion', 'CodMarca', 'Marca', 'CodProveedor', 'Proveedor', 'Temporada', 'Procedencia', 'TipoNeg', 'Color', 'Talla', 'SemAntig', 'ConVenta', 'ConStockOH', 'EsRebate', 'ConCobertura', 'VtaUnd', 'VtaSMF', 'Contr', 'Costo', 'Stock_OH', 'Costo_OH', 'Cbt') products = [Ripley(Id_DataExcel=DataExcel.objects.get(pk=object.id),**{fields[i]: cell for i, cell in enumerate(hoja1.row_values(rowid))}) # recorrido para guardar la data for rowid in range(1, hoja1.nrows)] for product in products: product.Color = str(product.Color).replace('.0', '') product.Talla = str(product.Talla).replace('.0', '') product.SemAntig = str(product.SemAntig).replace('.0', '') product.VtaUnd = str(product.VtaUnd).replace('.0', '') product.Stock_OH = str(product.Stock_OH).replace('.0', '') product.Cbt = str(product.Cbt).replace('.0', '') try: Ripley.objects.bulk_create(products) data_excel = DataExcel.objects.get(pk=object.id) data_excel.active = False data_excel.save() except IntegrityError as e: return False elif object.tienda == 2: #=============================================SAGA============================================================ fields = ( 'Codigo','Consignacion','Tipo Trx.','Fecha Proceso','Fecha Trx.','Local', 'Ccosto','SubClase','EAN','Sku','Descripcion Sku','Caja Vta','Nro.Trx. Vta','Cantidad', 'Base Imponible Vta','Impto Vta','Monto Timbrado Vta','Tipo Comision','Monto Comision', 'Costo Vta','IGV Costo Vta','Total Costo Vta','Local Trx Ori','Fecha Trx Ori','Caja Trx Ori','Nro Trx Ori') products_saga = [Saga(Id_DataExcel=DataExcel.objects.get(pk=object.id),**{fields[i]: cell for i, cell in enumerate(hoja1.row_values(rowid))}) # recorrido para guardar la data for rowid in range(6, hoja1.nrows)] try: Saga.objects.bulk_create(products_saga) data_excel = DataExcel.objects.get(pk=object.id) data_excel.active = False data_excel.save() except IntegrityError as e: return False
esperando su pronta ayuda |