0

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

`
  • 1
    So, currently, what problem you are facing with this code? Please add more details to the problem, this would help answerers to understand your problem well. I know you are facing problem with renaming but please add what you tried to rename. – hygull Jan 30 '19 at 02:30
  • 1
    The problem I'm having is iterating over a list to copy files into the folders in the list. Pretty much everything from #Create sub-folders down. With the shutil.copytree I keep getting that the folder already exists. I've tried using the copytree to just create the folders itself, but it does it for the first file, and the errors out with a massive list of the folders in error. The current code hits TypeError: expected str, bytes or os.PathLike object, not list – Adrian Charles Blood Jan 30 '19 at 02:48
  • What is the structure of your excel files, mean to say how its entries look? You can check https://stackoverflow.com/questions/1868714/how-do-i-copy-an-entire-directory-of-files-into-an-existing-directory-using-pyth. I think, it will be helpful to figure out your problem. Let me know in comment if you get stuck. – hygull Jan 30 '19 at 03:21
  • The structure is all in a single column (A) and is a varying length. The first entry is the Top directory and every other entry creates the subfolders. It is literally A1 - Project name, A2 - Subfolder1, A3 - Subfolder2, A4 - Subfolder3, etc... – Adrian Charles Blood Jan 30 '19 at 05:17

0 Answers0