Added xls output.

This commit is contained in:
Dan Dembinski
2021-02-19 11:34:47 -05:00
parent f6457a7223
commit c0710262a1

58
main.py
View File

@@ -1,9 +1,11 @@
from sqlalchemy import create_engine, Column, Integer, String, BOOLEAN from sqlalchemy import create_engine, Column, Integer, String, BOOLEAN
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
import xlwt
UserAddressID = [] UserAddressID = []
ActiveUser = [] ActiveUser = []
LineCount = 1
engine = create_engine('mssql+pymssql://IDV2Ridge:cWZSGWXS9muyYkHN*@10.10.10.50/IDV2Ridge', echo=False) engine = create_engine('mssql+pymssql://IDV2Ridge:cWZSGWXS9muyYkHN*@10.10.10.50/IDV2Ridge', echo=False)
engine.connect() engine.connect()
@@ -17,7 +19,7 @@ class UserAddress(Base):
AddressId = Column(Integer, primary_key=True) AddressId = Column(Integer, primary_key=True)
class Address(Base) : class Address(Base):
__tablename__ = 'Address' __tablename__ = 'Address'
Id = Column(Integer, primary_key=True) Id = Column(Integer, primary_key=True)
@@ -47,23 +49,48 @@ class State(Base):
Id = Column(Integer, primary_key=True) Id = Column(Integer, primary_key=True)
StateName = Column(String) StateName = Column(String)
class Country(Base): class Country(Base):
__tablename__ = 'Country' __tablename__ = 'Country'
Id = Column(Integer, primary_key=True) Id = Column(Integer, primary_key=True)
CountryName = Column(String) CountryName = Column(String)
Session = sessionmaker(bind=engine) Session = sessionmaker(bind=engine)
session = Session() session = Session()
# Get list of active users # Get list of active users
for user in session.query(User).filter(User.ID == 1): for user in session.query(User).filter(User.ID == 14):
ActiveUser.append([user.ID, user.FirstName, user.LastName]) ActiveUser.append([user.ID, user.FirstName, user.LastName])
# loop through each user in the ActiveUser array # loop through each user in the ActiveUser array
for each in ActiveUser: for each in ActiveUser:
# Combine Users First/Last name into single variable # Combine Users First/Last name into single variable
UserName = each[1] + ' ' + each[2] FileName = each[1] + ' ' + each[2]
print(UserName) UserName = each[1][0]+each[2]
# Setup XLS workbook and sheet
wb = xlwt.Workbook()
ws = wb.add_sheet('Addresses')
# Write header row
ws.write(0,0,'ACTION')
ws.write(0,1,'BU_ID')
ws.write(0,2,'LOGIN_ID')
ws.write(0,3,'PA_NAME')
ws.write(0,4, 'PA_NAME2')
ws.write(0,5,'LINE1')
ws.write(0,6,'LINE2')
ws.write(0,7,'LINE3')
ws.write(0,8,'CITY')
ws.write(0,9,'STATE')
ws.write(0,10,'ZIP')
ws.write(0,11,'COUNTRY')
ws.write(0,12,'PHONE_NR')
ws.write(0,13,'SHIP_TO_ATTN_TX')
ws.write(0,14, 'PROFILE_TYPE')
# For each username we're filtering the UserAddress table by their userID and adding it to the UserAddressID array. # For each username we're filtering the UserAddress table by their userID and adding it to the UserAddressID array.
# The UserAddressID array is then looped through and for each AddressID we pull the Address information, # The UserAddressID array is then looped through and for each AddressID we pull the Address information,
# State Information, and Country Information and combine it into one result # State Information, and Country Information and combine it into one result
@@ -71,7 +98,24 @@ for each in ActiveUser:
for ua in session.query(UserAddress).filter(UserAddress.UserId == each[0]): for ua in session.query(UserAddress).filter(UserAddress.UserId == each[0]):
UserAddressID.append(ua.AddressId) UserAddressID.append(ua.AddressId)
for aid in UserAddressID: for aid in UserAddressID:
for cn in session.query(Address, State, Country).join(State, Address.StateId == State.Id).join(Country, Address.CountryId == Country.Id).filter(Address.Id == aid).all(): for cn in session.query(Address, State, Country).join(State, Address.StateId == State.Id).join(Country, Address.CountryId == Country.Id).filter(Address.Id == aid):
print(cn[0].ContactName, cn[0].CompanyName, cn[0].Line1, cn[0].Line2, cn[0].City, cn[1].StateName, cn[0].PostalCode, cn[2].CountryName, cn[0].PhoneNumber, cn[0].EmailAddress) # Combine all the required fields into one variable for simplicity
UserAddressID = [] AddresssBookLine = UserName, cn[0].CompanyName, cn[0].Line1, cn[0].Line2, cn[0].City, cn[1].StateName, cn[0].PostalCode, cn[2].CountryName, cn[0].PhoneNumber,cn[0].ContactName
# Loop through each row and populate the variable columns with the corresponding AddressBookLine element
print(LineCount)
ws.write(LineCount, 2, AddresssBookLine[0])
ws.write(LineCount, 3, AddresssBookLine[1])
ws.write(LineCount, 5, AddresssBookLine[2])
ws.write(LineCount, 6, AddresssBookLine[3])
ws.write(LineCount, 8, AddresssBookLine[4])
ws.write(LineCount, 9, AddresssBookLine[5])
ws.write(LineCount, 10, AddresssBookLine[6])
ws.write(LineCount, 11, AddresssBookLine[7])
ws.write(LineCount, 12, AddresssBookLine[8])
ws.write(LineCount, 13, AddresssBookLine[9])
LineCount = LineCount + 1
print(LineCount)
UserAddressID = []
LineCount = 1
wb.save(FileName+'.xls')