-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_merger.py
88 lines (67 loc) · 2.41 KB
/
file_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
import pandas as pd
import datetime
import os
# install pandas, openpyxl, xlrd
def xlsx_merger():
path = os.getcwd()
files = os.listdir(path)
files_xls = [f for f in files if f[-4:] == 'xlsx']
print(files_xls)
# read them in
excels = [pd.ExcelFile(name) for name in files_xls]
# turn them into dataframes
frames = [x.parse(x.sheet_names[0], header=None, index_col=None)
for x in excels]
# delete the first row for all frames except the first
# i.e. remove the header row -- assumes it's the first
frames[1:] = [df[1:] for df in frames[1:]]
# concatenate them..
combined = pd.concat(frames)
# write it out
combined.to_excel("Envios - " + str(start.strftime("%Y-%m-%d - %H")
) + "h.xlsx", header=False, index=False)
def xls_merger():
path = os.getcwd()
files = os.listdir(path)
files_xls = [f for f in files if f[-3:] == 'xls']
print(files_xls)
# read them in
excels = [pd.ExcelFile(name) for name in files_xls]
# turn them into dataframes
frames = [x.parse(x.sheet_names[0], header=None, index_col=None)
for x in excels]
# delete the first row for all frames except the first
# i.e. remove the header row -- assumes it's the first
frames[1:] = [df[1:] for df in frames[1:]]
# concatenate them..
combined = pd.concat(frames)
# write it out
combined.to_excel("Envios - " + str(start.strftime("%Y-%m-%d - %H")
) + "h.xlsx", header=False, index=False)
def csv_merger():
path = os.getcwd()
files = os.listdir(path)
filenames = [f for f in files if f[-3:] == 'csv']
print(filenames)
combined_csv = pd.concat([pd.read_csv(f) for f in filenames])
combined_csv.to_csv(
"Raskon - " + str(start.strftime("%Y-%m-%d - %H")) + "h.csv", header=True, index=False)
try:
print("Processo Iniciado")
file_type = input("Enter file type (csv, xls, or xlsx):")
start = datetime.datetime.now()
if file_type == "csv":
csv_merger()
elif file_type == "xls":
xls_merger()
elif file_type == "xlsx":
xlsx_merger()
else:
print("File type is incompatible")
# Calcular tempo de execução
end = datetime.datetime.now()
tempo = end - start
print("\nCSV Gerado em " + str(tempo))
except Exception as e:
print(e)
input("\nPress enter to exit")