Files
Gainsight/Scripts/WalmartExcel/walmart.py

77 lines
2.2 KiB
Python
Raw Normal View History

2022-10-31 10:47:43 -04:00
import os
import sys
from datetime import date
import glob
import shutil
import csv
import pandas as pd
rootdir = "/Users/normrasmussen/Documents/Resources/Walmart/"
2022-12-22 18:15:51 -05:00
downloadir = "/Users/normrasmussen/Google Drive/My Drive/Shared with Clients/Walmart_Looker/"
basefile = "Walmart_Weekly_Base.xlsx"
2022-10-31 10:47:43 -04:00
def copytemplate(rootdir, basefile):
today = date.today()
today = today.strftime("%m.%d.%Y")
2022-12-22 18:15:51 -05:00
template = rootdir + basefile
2022-10-31 10:47:43 -04:00
todayFile = f"Walmart-{today}.xlsx"
if os.path.exists(rootdir):
2022-12-22 18:15:51 -05:00
shutil.copy2(template, rootdir + todayFile)
# dirfiles = os.listdir(rootdir)
# print(dirfiles)
currentDash = rootdir + f"Walmart-{today}.xlsx"
# print(currentDash)
2022-10-31 10:47:43 -04:00
findlatestExport(currentDash)
2022-12-22 18:15:51 -05:00
2022-10-31 10:47:43 -04:00
def findlatestExport(currentDash):
2022-12-22 18:15:51 -05:00
listfiles = glob.glob(downloadir + "*.csv")
2022-10-31 10:47:43 -04:00
latestdownload = max(listfiles, key=os.path.getctime)
2022-12-22 18:15:51 -05:00
# print(latestdownload)
2022-10-31 10:47:43 -04:00
copytoDash(latestdownload, currentDash)
2022-12-22 18:15:51 -05:00
2022-10-31 10:47:43 -04:00
def copytoDash(latestdownload, currentDash):
readExport = pd.read_csv(
2022-12-22 18:15:51 -05:00
latestdownload,
index_col=False,
header=0,
low_memory=False,
# dtype={"Progress": float},
)
2022-10-31 15:36:34 -04:00
print(readExport)
2022-12-22 18:15:51 -05:00
readExport['Progress'] = readExport['Progress'].str[:-1].apply(pd.to_numeric)
# readExport['Progress'].apply(pd.to_numeric, errors='ignore')
print(readExport['Progress'])
readExport.drop(readExport.filter(regex="Unname"), axis=1, inplace=True)
2022-10-31 10:47:43 -04:00
copiedData = readExport.copy()
2022-10-31 15:36:34 -04:00
bringtoExcel(latestdownload, currentDash, copiedData)
2022-10-31 10:47:43 -04:00
2022-12-22 18:15:51 -05:00
2022-10-31 15:36:34 -04:00
def bringtoExcel(latestdownload, currentDash, copiedData):
2022-10-31 10:47:43 -04:00
with pd.ExcelWriter(
2022-12-22 18:15:51 -05:00
currentDash,
mode="a",
engine="openpyxl",
if_sheet_exists="overlay",
# engine_kwargs={'options': {'strings_to_numbers': True}}
2022-10-31 10:47:43 -04:00
) as writer:
copiedData.to_excel(
2022-12-22 18:15:51 -05:00
writer,
engine="xlsxwriter",
sheet_name="Data",
index=False,
)
2022-10-31 10:47:43 -04:00
def cleanitUp(currentDash):
cleanExcel = pd.read_excel(currentDash, sheet_name="Data", index_col=None)
cleanExcel.columns.values[0] = "tmp"
cleanExcel.drop(columns="tmp", axis=1, inplace=True)
print(cleanExcel)
2022-12-22 18:15:51 -05:00
2022-10-31 10:47:43 -04:00
if __name__ == "__main__":
copytemplate(rootdir, basefile)