-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerger.py
97 lines (79 loc) · 3.61 KB
/
Merger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import fnmatch
import os
import pandas as pd
import pyreadstat as prd
import PySimpleGUI as sg
def FileFinder(d, k):
"""
Args:
d: string -- indicates the folder that files should be in
k: string -- part of the file name. Indicates which files should be added the the return list.
Returns: list of files
"""
# File Finder: Open and find all excel files. Will iterate over all files in values["-DIR-"].
savs = []
for root, dirs, files in os.walk(d):
for f in files:
if fnmatch.fnmatch(f, "*" + k + "*"):
savs.append(os.path.join(root, f))
return savs
def MergeRunner(inputs, files):
"""Merges the files that are passed in based on informaion from the GUI
Args:
inputs: determines which type of merge should be done depending on which file type was selected by the GUI,
and directs the merge operations
files: A list of files to be merged
Returns: outputs the file location to console
"""
# File Merger for spss files
if inputs["-FileType-"] == "spss":
filesList = []
valLabels = {}
# missingLabels = {}
for i in files:
newF, spssMeta = prd.read_sav(i, user_missing=True, disable_datetime_conversion=True)
filesList.append(newF)
# Loop through the value labels dictionary to update the sub-dicts
if not bool(valLabels):
valLabels.update(spssMeta.variable_value_labels)
else:
for j in valLabels:
valLabels[j].update(spssMeta.variable_value_labels[j])
# missingLabels.update(spssMeta.missing_ranges)
allFiles = pd.concat(filesList, sort=False)
prd.write_sav(allFiles, inputs["-DIR-"] + "/" + inputs["-OutFile-"] + ".sav",
variable_value_labels=valLabels)
return "merged spss file saved at: " + inputs["-DIR-"] + "/" + inputs["-OutFile-"] + ".sav"
# File Merger for excel files
if inputs["-FileType-"] == "excel":
filesList = []
for i in files:
filesList.append(pd.read_excel(i, sheet_name=inputs["-ExcelSheet-"]))
allFiles = pd.concat(filesList, sort=False)
allFiles.to_excel(inputs["-DIR-"] + "/" + inputs["-OutFile-"] + ".xlsx", index=False)
return "merged excel file saved at: " + inputs["-DIR-"] + "/" + inputs["-OutFile-"] + ".xlsx"
def main():
"""Runs everything"""
# Set up the GUI
sg.theme('DarkGrey4')
# All the stuff inside your window.
layout = [[sg.Text('Select the root directory'), sg.FolderBrowse(key="-DIR-"),
sg.Text('Select the file type'), sg.Combo(["excel", "spss"], key="-FileType-")],
[sg.Text("Text used for matching files (case sensitive)"), sg.InputText(key="-MatchKey-")],
[sg.Text('If excel, what is the sheet name? (case sensitive)'), sg.InputText(key="-ExcelSheet-")],
[sg.Text('Name the output file (no extension needed)'), sg.InputText(key="-OutFile-")],
[sg.Button('Ok'), sg.Button('Cancel')]]
# Create the Window
window = sg.Window('Window Title', layout)
# Event Loop to process "events" and get the "values" of the inputs
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
exit()
window.close()
# get the list of files to be merged
saveList = FileFinder(values["-DIR-"], values["-MatchKey-"])
# run the merge and output file location to the console
merger = MergeRunner(values, saveList)
print(merger)
if __name__ == '__main__':
main()