|
| 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) |
0 commit comments