1
- '''
1
+ """
2
2
Merges two excels into one copying all the sheets.
3
- '''
3
+ """
4
+ import os
5
+
4
6
import openpyxl
5
7
import pandas as pd
6
- import os
7
8
8
9
9
- def get_all_sheet_names (url , url2 ):
10
- '''
10
+ def get_all_sheet_names (url_ , url2_ ):
11
+ """
11
12
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
+
16
18
17
19
def check_if_file_exists (destination_file ):
18
- '''
20
+ """
19
21
if a file does not exist, create an xlsx file where merged data is stored.
20
- '''
22
+ """
21
23
if not os .path .exists (destination_file ):
22
24
23
- wb = openpyxl .Workbook ()
24
- wb .save (destfile )
25
+ wb_ = openpyxl .Workbook ()
26
+ wb_ .save (destination_file )
27
+
25
28
26
29
def write_to_excel (sheet_one_names , sheet_two_names , url , url2 , destfile ):
27
30
"""
@@ -30,37 +33,37 @@ def write_to_excel(sheet_one_names, sheet_two_names, url, url2, destfile):
30
33
31
34
for i in sheet_one_names :
32
35
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 :
35
37
data .to_excel (writer , index = False , sheet_name = i )
36
38
37
39
for i in sheet_two_names :
38
40
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 :
41
42
data .to_excel (writer , index = False , sheet_name = i )
42
43
43
- #remove the extra Sheet added if exists while creating the destfile
44
+ # remove the extra Sheet added if exists while creating the destfile
44
45
if "Sheet" not in sheet_one_names and "Sheet" not in sheet_two_names :
45
46
workbook1 = openpyxl .load_workbook (destfile )
46
- del workbook1 [' Sheet' ]
47
+ del workbook1 [" Sheet" ]
47
48
workbook1 .save (destfile )
48
49
49
50
50
51
def run_the_flow (url , url2 , destfile ):
51
- '''
52
+ """
52
53
Run the flow for the merging of two excels into one.
53
- '''
54
+ """
54
55
sheet_one_names , sheet_two_names = get_all_sheet_names (url , url2 )
55
56
check_if_file_exists (destfile )
56
57
write_to_excel (sheet_one_names , sheet_two_names , url , url2 , destfile )
57
58
58
- if __name__ == '__main__' :
59
-
60
59
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__" :
64
61
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"
66
68
69
+ run_the_flow (URL1 , URL2 , DEST )
0 commit comments