0

我的故事是,我将继续获取一个包含内容列表的 .xlsx 文件。所有东西都存在于特定的工作表中。

我创建了一个 Tkinter 来帮助选择要选择的工作表。

从那里,它创建一个基本文件结构,其中工作表中的 A1 列作为父目录,列表中的所有其他列(A 列)作为子文件夹。

现在我遇到了麻烦。我想将 7 个文件从源 (Loc) 复制到刚刚创建的文件夹的每个迭代中。

一旦我创建了文件(.DocX 和 .ppt),我想用现有文件名前面的文件夹重命名它们(即如果源文件是“abc.docx”并且创建的新文件夹是“\ xyz”,那么创建的新文件将是“xyz abc.docx”

我知道这是一团糟,但这是我的第一个真实世界项目,我真的很想启动并运行它。

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

`
4

0 回答 0