import json
import pandas as pd

# 1. Cargar el archivo JSON desde el archivo de texto
with open('base de datos_4.txt', 'r', encoding='utf-8') as f:
    data = json.load(f)

# 2. Crear un archivo Excel con múltiples pestañas (hojas)
with pd.ExcelWriter('inventario_convertido.xlsx', engine='openpyxl') as writer:
    
    # Pestaña: Preformas
    if 'preformas' in data:
        df_preformas = pd.DataFrame(list(data['preformas'].items()), columns=['Peso (gr)', 'Cantidad'])
        df_preformas.to_excel(writer, sheet_name='Preformas', index=False)
        
    # Pestaña: Tapas
    if 'tapas' in data:
        df_tapas = pd.DataFrame(list(data['tapas'].items()), columns=['Tipo de Tapa', 'Cantidad'])
        df_tapas.to_excel(writer, sheet_name='Tapas', index=False)
        
    # Pestaña: Latas Blancas
    if 'latas_blancas' in data:
        df_blancas = pd.DataFrame(list(data['latas_blancas'].items()), columns=['Medida', 'Cantidad'])
        df_blancas.to_excel(writer, sheet_name='Latas Blancas', index=False)
        
    # Pestaña: Latas Mal Pintadas
    if 'latas_mal_pintadas_por_medida' in data:
        df_mal_pintadas = pd.DataFrame(list(data['latas_mal_pintadas_por_medida'].items()), columns=['Medida', 'Cantidad'])
        df_mal_pintadas.to_excel(writer, sheet_name='Mermas por Medida', index=False)
        
    # Pestaña: Lotes Pintados
    if 'lotes_pintados' in data:
        df_lotes = pd.DataFrame(data['lotes_pintados'])
        df_lotes.to_excel(writer, sheet_name='Lotes Pintados', index=False)
        
    # Pestaña: Historial
    if 'historial' in data:
        # Aplanamos los datos principales del historial para que queden bien en filas y columnas
        historial_lista = []
        for h in data['historial']:
            historial_lista.append({
                'ID': h.get('id'),
                'Fecha': h.get('fecha'),
                'Acción': h.get('accion'),
                'Detalle': h.get('detalle'),
                'Tipo': h.get('tipo')
            })
        df_historial = pd.DataFrame(historial_lista)
        df_historial.to_excel(writer, sheet_name='Historial', index=False)

print("¡Conversión exitosa! El archivo 'inventario_convertido.xlsx' ha sido creado.")

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: