from openpyxl import load_workbook from sqlalchemy import create_engine, Column, Integer, String, REAL from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker engine = create_engine('sqlite:///cffInvoice.db') engine.connect() Base = declarative_base() class Invoice(Base): __tablename__ = 'Invoice' rowid = Column(Integer, primary_key=True, autoincrement=True) OrderID = Column(String) InvoiceNumber = Column(String) DateCreated = Column(String) ProductionCharges = Column(REAL) ShippingCharges = Column(REAL) AdjustmentsBalance = Column(REAL) DuePayment = Column(REAL) ChargeCode = Column(String) UserLogon = Column(String) UserProfileFirstName = Column(String) UserProfileLastName = Column(String) InvoiceEmailAddress = Column(String) ShippingCompany = Column(String) ShippingFirstName = Column(String) ShippingLastName = Column(String) ShippingAddress1 = Column(String) ShippingAddress2 = Column(String) ShippingCity = Column(String) ShippingState = Column(String) ShippingPostalCode = Column(String) class ItemDetail(Base): __tablename__ = 'ItemDetail' rowid = Column(Integer, primary_key=True, autoincrement=True) OrderID = Column(String) DocumentID = Column(String) GLIJobNumber = Column(String) ProductName = Column(String) Quantity = Column(String) ItemPrice = Column(REAL) Session = sessionmaker(bind=engine) session = Session() def load_sheet(invoiceNumber): # starting invoice sub-number count = 2 #open the xlsx file from accounting and name the two sheets required wb = load_workbook('1.xlsx', data_only=True) shtInvoice = wb.get_sheet_by_name('Invoice') shtItemDetail = wb.get_sheet_by_name('Item Detail') #loop through each row in the Invoice sheet except for the first and last #write each row to the database for row in shtInvoice.iter_rows(min_row=2, max_row=shtInvoice.max_row-1): session.add(Invoice(OrderID=row[0].value, InvoiceNumber=invoiceNumber + '-' + str(count), DateCreated=row[2].value, ProductionCharges=row[4].value, ShippingCharges=row[5].value, AdjustmentsBalance=row[6].value, DuePayment=row[7].value, ChargeCode=row[8].value, UserLogon=row[9].value, UserProfileFirstName=row[10].value, UserProfileLastName=row[11].value, InvoiceEmailAddress=row[12].value, ShippingCompany=row[13].value, ShippingFirstName=row[14].value, ShippingLastName=row[15].value, ShippingAddress1=row[16].value, ShippingAddress2=row[17].value, ShippingCity=row[18].value, ShippingState=row[19].value, ShippingPostalCode=row[20].value)) count = count + 1 session.commit() #loops through each row in the Item Detail sheet except for the first and last #Writes each row to the datbase for row in shtItemDetail.iter_rows(min_row=2, max_row=shtItemDetail.max_row-1): session.add(ItemDetail(OrderID=row[0].value, DocumentID=row[1].value, GLIJobNumber=row[4].value, ProductName=row[5].value, Quantity=row[6].value, ItemPrice=row[7].value)) session.commit() def just_work(): #array of orderIDs orders = [] for order in session.query(Invoice).all(): orders.append(order.OrderID) return orders def record_lookup(record): #looks up the requested OrderID and returns an array of the results result = [] for lookup in session.query(Invoice).filter(Invoice.OrderID == record): result.append(lookup.OrderID) result.append(lookup.DateCreated) result.append(lookup.UserLogon) result.append(lookup.UserProfileFirstName) result.append(lookup.UserProfileLastName) result.append(lookup.ChargeCode) result.append(lookup.InvoiceNumber) result.append(lookup.InvoiceEmailAddress) return result