Skip to content

Commit 8793d09

Browse files
authored
Merge pull request #750 from gitshashwat/main
Excel-Merger
2 parents 0524dc0 + 09113b4 commit 8793d09

File tree

6 files changed

+83
-0
lines changed

6 files changed

+83
-0
lines changed

excel_merger/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Overall
2+
3+
It provides a way to merge two excel workbooks into one.
4+
5+
## Setup and activate virtual environment :
6+
For Unix based systems please execute the following command to create venv and install requirements.
7+
```
8+
make init
9+
source .venv/bin/activate
10+
```
11+
12+
**Dependencies on python3, pandas and openpyxl**

excel_merger/excel_merger.py

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

excel_merger/files/FoodSales1-1.xlsx

15.6 KB
Binary file not shown.

excel_merger/files/FoodSales2-1.xlsx

14.8 KB
Binary file not shown.

excel_merger/merged/merged.xlsx

11.6 KB
Binary file not shown.

excel_merger/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pandas
2+
openpyxl

0 commit comments

Comments
 (0)