Skip to content

Commit 09113b4

Browse files
author
Shashwat Kumar
committed
clean up and formatting
1 parent 60e2d86 commit 09113b4

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

excel_merger/excel_merger.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
'''
1+
"""
22
Merges two excels into one copying all the sheets.
3-
'''
3+
"""
4+
import os
5+
46
import openpyxl
57
import pandas as pd
6-
import os
78

89

9-
def get_all_sheet_names(url, url2):
10-
'''
10+
def get_all_sheet_names(url_, url2_):
11+
"""
1112
Get the two xlsx files sheet names from their url
12-
'''
13-
xls = pd.read_excel(url, sheet_name = None)
14-
xls2 = pd.read_excel(url2, sheet_name = None)
15-
return xls.keys() , xls2.keys()
13+
"""
14+
xls = pd.read_excel(url_, sheet_name=None)
15+
xls2 = pd.read_excel(url2_, sheet_name=None)
16+
return xls.keys(), xls2.keys()
17+
1618

1719
def check_if_file_exists(destination_file):
18-
'''
20+
"""
1921
if a file does not exist, create an xlsx file where merged data is stored.
20-
'''
22+
"""
2123
if not os.path.exists(destination_file):
2224

23-
wb = openpyxl.Workbook()
24-
wb.save(destfile)
25+
wb_ = openpyxl.Workbook()
26+
wb_.save(destination_file)
27+
2528

2629
def write_to_excel(sheet_one_names, sheet_two_names, url, url2, destfile):
2730
"""
@@ -30,37 +33,37 @@ def write_to_excel(sheet_one_names, sheet_two_names, url, url2, destfile):
3033

3134
for i in sheet_one_names:
3235
data = pd.read_excel(url, sheet_name=i)
33-
with pd.ExcelWriter(destfile, engine="openpyxl",
34-
mode='a') as writer:
36+
with pd.ExcelWriter(destfile, engine="openpyxl", mode="a") as writer:
3537
data.to_excel(writer, index=False, sheet_name=i)
3638

3739
for i in sheet_two_names:
3840
data = pd.read_excel(url2, sheet_name=i)
39-
with pd.ExcelWriter(destfile, engine="openpyxl",
40-
mode='a') as writer:
41+
with pd.ExcelWriter(destfile, engine="openpyxl", mode="a") as writer:
4142
data.to_excel(writer, index=False, sheet_name=i)
4243

43-
#remove the extra Sheet added if exists while creating the destfile
44+
# remove the extra Sheet added if exists while creating the destfile
4445
if "Sheet" not in sheet_one_names and "Sheet" not in sheet_two_names:
4546
workbook1 = openpyxl.load_workbook(destfile)
46-
del workbook1['Sheet']
47+
del workbook1["Sheet"]
4748
workbook1.save(destfile)
4849

4950

5051
def run_the_flow(url, url2, destfile):
51-
'''
52+
"""
5253
Run the flow for the merging of two excels into one.
53-
'''
54+
"""
5455
sheet_one_names, sheet_two_names = get_all_sheet_names(url, url2)
5556
check_if_file_exists(destfile)
5657
write_to_excel(sheet_one_names, sheet_two_names, url, url2, destfile)
5758

58-
if __name__ == '__main__':
59-
6059

61-
url = r"C:\Users\ShashwatKumar\Desktop\open_source\Automation-scripts\excel_merger\files\FoodSales1-1.xlsx"
62-
url2 = r"C:\Users\ShashwatKumar\Desktop\open_source\Automation-scripts\excel_merger\files\FoodSales2-1.xlsx"
63-
destfile = r"C:\Users\ShashwatKumar\Desktop\open_source\Automation-scripts\excel_merger\merged\merged.xlsx"
60+
if __name__ == "__main__":
6461

65-
run_the_flow(url, url2, destfile)
62+
URL1 = r"C:\Users\ShashwatKumar\Desktop\open_source\
63+
Automation-scripts\excel_merger\files\FoodSales1-1.xlsx"
64+
URL2 = r"C:\Users\ShashwatKumar\Desktop\open_source\
65+
Automation-scripts\excel_merger\files\FoodSales2-1.xlsx"
66+
DEST = r"C:\Users\ShashwatKumar\Desktop\open_source\
67+
Automation-scripts\excel_merger\merged\merged.xlsx"
6668

69+
run_the_flow(URL1, URL2, DEST)

0 commit comments

Comments
 (0)