v20-1
This commit is contained in:
32
export_to_excel.py
Normal file
32
export_to_excel.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import pandas as pd
|
||||
|
||||
from models import Articl, Car, Category, Product
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
engine = create_engine('sqlite:///cars_database.db')
|
||||
Session = sessionmaker(bind=engine)
|
||||
session = Session()
|
||||
|
||||
models = {
|
||||
'cars.csv': Car,
|
||||
'categories.csv': Category,
|
||||
'products.csv': Product,
|
||||
'articles.csv': Articl
|
||||
}
|
||||
|
||||
for filename, model in models.items():
|
||||
records = session.query(model).all()
|
||||
if not records:
|
||||
continue
|
||||
|
||||
rows = []
|
||||
for r in records:
|
||||
row = {k: v for k, v in r.__dict__.items() if not k.startswith('_')}
|
||||
rows.append(row)
|
||||
|
||||
df = pd.DataFrame(rows)
|
||||
df.to_csv(filename, index=False)
|
||||
print(f"✅ {filename} сохранён")
|
||||
|
||||
session.close()
|
||||
Reference in New Issue
Block a user