Ver Mensaje Individual
  #1 (permalink)  
Antiguo 11/09/2014, 16:53
andy2507
 
Fecha de Ingreso: julio-2011
Ubicación: Peru
Mensajes: 92
Antigüedad: 12 años, 9 meses
Puntos: 1
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 original
  1. class DataExcel(models.Model):
  2.     tienda = (
  3.         (1,'Ripley'),
  4.         (2,'Saga')
  5.     )
  6.  
  7.  
  8.     name = models.CharField('Nombre', max_length=20)
  9.     fecha_inicio = models.DateField(verbose_name='Fecha inicio', max_length=10)
  10.     fecha_fin = models.DateField(verbose_name='Fecha Fin', max_length=10)
  11.     file = models.FileField(upload_to=settings.MEDIA_ROOT,verbose_name='Archivo')
  12.     tienda = models.IntegerField(choices=tienda)
  13.     active = models.BooleanField(default=True)
  14.  
  15. class Saga(models.Model):
  16.  
  17.     Id_DataExcel = models.ForeignKey(DataExcel)
  18.     Codigo = models.IntegerField(max_length=10, blank=True)
  19.     Consignacion = models.CharField(max_length=20, blank=True)
  20.     Tipo_Trx = models.CharField(max_length=20, blank=True)
  21.     Fecha_proceso = models.DateField()
  22.     Fecha_Trx = models.DateField()
  23.     Local = models.CharField(max_length=30)
  24.     Ccosto = models.CharField(max_length=30)
  25.     SubClase = models.CharField(max_length=30)
  26.     Ean =  models.IntegerField(max_length=60, blank=True)
  27.     Sku = models.IntegerField(max_length=60, blank=True)
  28.     Descripcion_Sku = models.CharField(max_length=200, blank=True)
  29.     Caja_Vta = models.IntegerField(max_length=60, blank=True)
  30.     Nro_Trx_Vta = models.IntegerField(max_length=60, blank=True)
  31.     Cantidad = models.FloatField()
  32.     Base_imponible_vta = models.FloatField()
  33.     Impto_vta = models.FloatField()
  34.     Monto_timbrado_vta = models.FloatField()
  35.     Tipo_comision = models.CharField(max_length=60, blank=True)
  36.     Monto_comision = models.IntegerField(max_length=20, blank=True)
  37.     Costo_vta = models.FloatField()
  38.     Igv_costo_vta = models.FloatField()
  39.     Total_costo_vta = models.FloatField()
  40.     Local_Trx_Ori = models.IntegerField(max_length=60, blank=True)
  41.     Fecha_Trx_Ori = models.DateField()
  42.     Caja_Trx_Ori = models.IntegerField(max_length=20, blank=True)
  43.     Nro_Trx_ori = models.IntegerField(max_length=20, blank=True)
  44.  
  45.  
  46.  
  47. class Ripley(models.Model):
  48.  
  49.     Id_DataExcel = models.ForeignKey(DataExcel)
  50.     CodSucursal = models.IntegerField(max_length=60, blank=True)
  51.     Sucursal = models.CharField(max_length=60, blank=True)
  52.     CodDivision = models.CharField(max_length=200, blank=True)
  53.     Division = models.CharField(max_length=300, blank=True)
  54.     CodArea = models.CharField(max_length=60, blank=True)
  55.     Area = models.CharField(max_length=30, blank=True)
  56.     CodDpto = models.CharField(max_length=60, blank=True)
  57.     Dpto = models.CharField(max_length=35, blank=True)
  58.     CodLinea = models.IntegerField(max_length=200, blank=True)
  59.     Linea = models.CharField(max_length=35, blank=True)
  60.     CodSubLinea = models.CharField(max_length=200, blank=True)
  61.     SubLinea = models.CharField(max_length=200, blank=True)
  62.     CodModelo = models.IntegerField(max_length=60, blank=True)
  63.     Modelo = models.CharField(max_length=50, blank=True)
  64.     CodVariacion = models.IntegerField(max_length=60, blank=True)
  65.     Variacion = models.CharField(max_length=50, blank=True)
  66.     CodMarca = models.IntegerField(max_length=200, blank=True)
  67.     Marca = models.CharField(max_length=60, blank=True)
  68.     CodProveedor = models.IntegerField(max_length=200, blank=True)
  69.     Proveedor = models.CharField(max_length=60, blank=True)
  70.     Temporada = models.CharField(max_length=60, blank=True)
  71.     Procedencia = models.CharField(max_length=60, blank=True)
  72.     TipoNeg = models.CharField(max_length=15, blank=True)
  73.     Color = models.CharField(max_length=200, blank=True)
  74.     Talla = models.CharField(max_length=200, blank=True)
  75.     SemAntig = models.CharField(max_length=200, blank=True)
  76.     ConVenta = models.CharField(max_length=200, blank=True)
  77.     ConStockOH = models.CharField(max_length=200, blank=True)
  78.     EsRebate = models.CharField(max_length=200, blank=True)
  79.     ConCobertura = models.CharField(max_length=200, blank=True)
  80.     VtaUnd = models.CharField(max_length=200, blank=True)
  81.     VtaSMF = models.FloatField()
  82.     Contr = models.FloatField()
  83.     Costo = models.FloatField()
  84.     Stock_OH = models.CharField(max_length=200, blank=True)
  85.     Costo_OH = models.FloatField()
  86.     Cbt = models.CharField(max_length=200, blank=True)

VIEWS.PY

Código Python:
Ver original
  1. class DataExcelProcessView(TemplateView):
  2.     template_name = 'app/volver.html'
  3.  
  4.     def get(self, request, *args, **kwargs):
  5.         return super(DataExcelProcessView, self).get(request, args, kwargs)
  6.  
  7.     def get_context_data(self, **kwargs):
  8.         ctx = super(DataExcelProcessView, self).get_context_data(**kwargs)
  9.         ctx['lista_data'] = self.process_file()
  10.         return ctx
  11.  
  12.     def process_file(self):
  13.         object = DataExcel.objects.get(pk=self.kwargs['pk'])
  14.         book = xlrd.open_workbook(object.file.path)
  15.         hoja1 = book.sheet_by_index(0)
  16.  
  17.         print object.tienda
  18.  
  19.         if object.tienda == 1:
  20.  
  21. #=============================================RIPLEY============================================================
  22.  
  23.             fields = ('CodSucursal', 'Sucursal', 'CodDivision', 'Division', 'CodArea', 'Area', 'CodDpto', 'Dpto', 'CodLinea',
  24.                         'Linea', 'CodSubLinea', 'SubLinea', 'CodModelo', 'Modelo', 'CodVariacion', 'Variacion', 'CodMarca',
  25.                         'Marca', 'CodProveedor', 'Proveedor', 'Temporada', 'Procedencia', 'TipoNeg', 'Color', 'Talla',
  26.                         'SemAntig', 'ConVenta', 'ConStockOH', 'EsRebate', 'ConCobertura', 'VtaUnd', 'VtaSMF', 'Contr',
  27.                         'Costo', 'Stock_OH', 'Costo_OH', 'Cbt')
  28.  
  29.             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
  30.                         for rowid in range(1, hoja1.nrows)]
  31.  
  32.  
  33.             for product in products:
  34.                 product.Color = str(product.Color).replace('.0', '')
  35.                 product.Talla = str(product.Talla).replace('.0', '')
  36.                 product.SemAntig = str(product.SemAntig).replace('.0', '')
  37.                 product.VtaUnd = str(product.VtaUnd).replace('.0', '')
  38.                 product.Stock_OH = str(product.Stock_OH).replace('.0', '')
  39.                 product.Cbt = str(product.Cbt).replace('.0', '')
  40.  
  41.             try:
  42.                 Ripley.objects.bulk_create(products)
  43.                 data_excel = DataExcel.objects.get(pk=object.id)
  44.                 data_excel.active = False
  45.                 data_excel.save()
  46.             except IntegrityError as e:
  47.                     return False
  48.  
  49.         elif object.tienda == 2:
  50. #=============================================SAGA============================================================
  51.             fields = ( 'Codigo','Consignacion','Tipo Trx.','Fecha Proceso','Fecha Trx.','Local',
  52.                         'Ccosto','SubClase','EAN','Sku','Descripcion Sku','Caja Vta','Nro.Trx. Vta','Cantidad',
  53.                         'Base Imponible Vta','Impto Vta','Monto Timbrado Vta','Tipo Comision','Monto Comision',
  54.                         'Costo Vta','IGV Costo Vta','Total Costo Vta','Local Trx Ori','Fecha Trx Ori','Caja Trx Ori','Nro Trx Ori')
  55.  
  56.             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
  57.                              for rowid in range(6, hoja1.nrows)]
  58.  
  59.             try:
  60.                 Saga.objects.bulk_create(products_saga)
  61.                 data_excel = DataExcel.objects.get(pk=object.id)
  62.                 data_excel.active = False
  63.                 data_excel.save()
  64.             except IntegrityError as e:
  65.                     return False


esperando su pronta ayuda