My story goes that I am going to continue getting a .xlsx file with a list of things. All of the things exist in a specific sheet.
I have created a Tkinter to help select which sheet to choose from.
From there, it creates a basic file structure with column A1 from the sheet as the Parent directory and all others in the list (column A) as the sub folders.
Now comes the bit that I'm having trouble with. I want to copy 7 files from a source (Loc) into each iteration of folders that were just created.
Once I've created the files (.DocX and .ppt) I want to rename each of them with the folder in front of the existing filename (ie if the source file was "abc.docx" and the new folder created was "\xyz", then the new file created would be "xyz abc.docx"
I know it's a mess but this is my first real world project and I'd really like to get it up and running.
from tkinter import *
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import tkinter.filedialog as filedialog
import pandas as pd
import xlrd
import string
import os
from tkinter import messagebox
import shutil
import sys
from pathlib import Path
# Define file types to open
ftypes = [
('Excel', '*.xls;*.xlsx;*.csv'),
('Text files', '*.txt'),
('All files', '*'),
]
#Start tkinter
master = Tk()
#Show an "Open" dialog box and return the path to the selected file
FileName = filedialog.askopenfilename(filetypes=ftypes)
#print(FileName)
#Looking at the file with pandas
xl = pd.ExcelFile(FileName)
SheetNames = xl.sheet_names # see all sheet names
L = Label(master, text = FileName)
L.pack()
#Create a selection window
w = Label(master, text="Select the sheet to create the directories from")
w.pack()
V = StringVar(master)
V.set(SheetNames[0]) # default value
#option menu on the tkinter window
w = OptionMenu(master, V, *SheetNames)
w.pack()
#Define what the OK and Close buttons do on the sheet selection window
def ok():
print (V.get())
master.quit()
def CloseWindow():
import sys
master.destroy()
sys.exit()
#OK and Close button display settings
OkButton = Button(master, text="OK", command=ok)
OkButton.pack()
CloseButton = Button(master, text="Close", command=CloseWindow)
CloseButton.pack()
#print (w)
mainloop()
#print (SheetNames)
#Set folder save location
SaveLocation = filedialog.askdirectory(title='Please select a directory to create the folders')
S = Label(master, text = "Save location " + SaveLocation)
S.pack()
#print (SaveLocation)
#print (V.get())
#Starting to iterate through the spreadsheet
Workbook = xlrd.open_workbook(FileName)
Sheet = Workbook.sheet_by_name(V.get())
#Number of rows in the sheet
SNRows = (Sheet.nrows)
#Value of first cell in the sheet
SCValues = Sheet.cell_value(0, 0)
print (SCValues)
print (SNRows)
#Create Parent folder
ParentFolder = (SaveLocation +"/" + SCValues)
os.path.exists(ParentFolder)
if not os.path.exists(ParentFolder):
os.makedirs(ParentFolder, exist_ok=True)
#Create sub-folders
DropBox = filedialog.askdirectory(title='Please select the base directory of DropBox')
FileLocation = "/Factory/Admin/Templates/1. Competency"
Loc = (DropBox + FileLocation)
for i in range(1, SNRows):
SF = Sheet.cell_value(i, 0)
SubFolders = (ParentFolder +"/" + SF)
#print (SF)
os.makedirs(SubFolders, exist_ok=True)
#shutil.copytree(Loc, SubFolders)
for folderName, S, File in os.walk(ParentFolder):
#print ('The current folder is ' + folderName)
shutil.copytree(Loc, S)
#print(SubFolders)
#Dropbox access to access contents of directory and copy into subfolders
for F, S, File in os.walk(ParentFolder):
print (S)
#Rename contents of subfolders
`