Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. import xlsxwriter, os, csv
  2. import Tkinter as tk
  3. from tkFileDialog import askdirectory
  4. import Pmw
  5. root = tk.Tk()
  6. root.title('Combine files for LabRadar')
  7. root.geometry('430x300')
  8.  
  9. def combine(filePath, savePath, fileName):
  10.     """Combines .csv files from LabRadar into multisheet .xlxs file, Scans single shot directory
  11.        and saves .csv file paths into array, and .csv file names into array and added to multi-
  12.        sheet .xlxs file """
  13.     print (filePath, savePath, fileName)
  14.     fileDir = [] # creating array for file paths
  15.     sheetNumber = [] # creating array for file names
  16.     for root, dirs, files in os.walk(filePath): #Scanning root path for shot group
  17.         for file in sorted(files):
  18.             if file.endswith('.csv'): # Isolating only .csv Files
  19.                 sheetNumber.append(file) # Creating array for file names for sheets
  20.                 fileDir.append(root+ '/' + file) # Creating array for file paths
  21.     wb = xlsxwriter.Workbook(savePath + '/' + fileName + '.xlsx', {'strings_to_numbers': True})
  22.                                     #Creating workbook that has text as well as numbers as numbers
  23.     for i in range(0, len(sheetNumber)):
  24.         ws = wb.add_worksheet(sheetNumber[i]) # Creating all sheets
  25.  
  26.     for i in range(0, len(sheetNumber)):
  27.         ws = wb.get_worksheet_by_name(sheetNumber[i]) # Opening each sheet in order corisponding to
  28.                                                       # correct file
  29.         with open(fileDir[i],'rb') as f:
  30.             reader = csv.reader((line.replace('\0', '') for line in f), delimiter=';')
  31.             for r, row in enumerate(reader):
  32.                 for c, val in enumerate(row):
  33.                     ws.write(r, c, val) # Writing the values from .csv file to .xlsx file one line at a time
  34.                     #print (r, c, val)
  35. def creatLabels(count):
  36.     lblArray = []
  37.     num = 1
  38.     fld = 'SR'
  39.     for root, dirs, files in os.walk('/Users/jeremylysinger/Desktop/LBR', topdown=True):
  40.         for name in sorted(dirs):
  41.             if name == fld + (str(num).zfill(4)):
  42.                 lblArray.append(name)
  43.                 num += 1
  44.     return lblArray[count]
  45.  
  46. def fileNames(number):
  47.     global field
  48.     stringLabel = tk.Label(root, text='Shot Group Name for String ' +
  49.                                       str(number) + ':')
  50.     stringLabel.pack()
  51.     field = Pmw.EntryField(root, labelpos="w", label_text='Group:')
  52.     field.setentry(str(number))
  53.     field.pack()
  54. def nextWindow():
  55.     print field.get()
  56. def fileWindow():
  57.     global saveButton, convertButton, e, shotLabel, convertButton
  58.     openButton.pack_forget()
  59.     openLabel.pack_forget()
  60.     b.pack_forget()
  61.     saveButton = tk.Button(text='Save Location', command=saveLocation)
  62.     saveButton.pack(pady=10, padx=10)
  63.     e = 'Test 2'
  64.     convertButton = tk.Button(text='Convert!', command=lambda: convert(rootDir, saveDir, 'Shot String '))
  65.     convertButton.pack(pady=10, padx=10)
  66. def askFile():
  67.     global rootDir
  68.     rootDir = askdirectory()
  69.     #print rootDir
  70. def saveLocation():
  71.     global saveDir
  72.     saveDir = askdirectory()
  73.     #print saveDir
  74. def end():
  75.     root.distroy()
  76. def convert(path, save, sName):
  77.     saveButton.pack_forget()
  78.     convertButton.pack_forget()
  79.     count = 1
  80.     fld = 'SR'
  81.     for root, dirs, files in os.walk(path, topdown=True):
  82.         for name in sorted(dirs):
  83.             if name == fld + (str(count).zfill(4)):
  84.                 #combine(path + '/' + name, save, sName + str(count))
  85.                 fileNames(count)
  86.                 count += 1
  87.     nextButton = tk.Button(text='Next!', command = lambda : nextWindow())
  88.     nextButton.pack()
  89.  
  90. openLabel = tk.Label(root, text='Select the destination of combined shots')
  91. openLabel.pack()
  92. openButton = tk.Button(text='Open Folder', command=askFile)
  93. openButton.pack(pady=10, padx=10)
  94. b = tk.Button(root, text="Start", command=fileWindow)
  95. b.pack()
  96.  
  97.  
  98. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement