├── Python_code ├── CombineFiles.py ├── Formulas.py ├── GetValue.py └── Search.py ├── README.md ├── datasets ├── SampleData.xlsx └── SampleData2.xlsx └── images └── thumbnail.png /Python_code/CombineFiles.py: -------------------------------------------------------------------------------- 1 | #pip3 install pandas 2 | import pandas as pd 3 | 4 | #specify the files location (or path) 5 | excel_files = ['files path here'] 6 | 7 | #create a blank dataframe 8 | merge = pd.DataFrame() 9 | 10 | #loop through every file in the list 11 | for file in excel_files: 12 | #read files into a dataframe and skip the first row (header) 13 | df = pd.read_excel(file, skiprows = 1) 14 | #append results to merge 15 | merge = merge.append(df, ignore_index = True) 16 | 17 | #create final workbook with merged results 18 | merge.to_excel('Merged_Files.xlsx') 19 | -------------------------------------------------------------------------------- /Python_code/Formulas.py: -------------------------------------------------------------------------------- 1 | #pip3 install openpyxl 2 | import openpyxl 3 | 4 | #specify the files location (or path) 5 | excel_files = ['files path here'] 6 | 7 | #loop through the files in the "excel_files" list 8 | for file in excel_files: 9 | wb = openpyxl.load_workbook(file) 10 | #locate worksheet 11 | worksheet = wb["SalesOrders"] 12 | #compute average in G46 13 | worksheet['G46'] = '=AVERAGE(G3:G45)' 14 | #save the file 15 | wb.save(file) 16 | 17 | -------------------------------------------------------------------------------- /Python_code/GetValue.py: -------------------------------------------------------------------------------- 1 | #pip3 install openpyxl 2 | import openpyxl 3 | 4 | #specify the files location (or path) 5 | excel_files = ['files path here'] 6 | 7 | #create an empty list to append values later on 8 | values = [] 9 | 10 | #loop through the files in the "excel_files" list 11 | for file in excel_files: 12 | workbook = openpyxl.load_workbook(file) 13 | #specify worksheet to select 14 | worksheet = workbook['SalesOrders'] 15 | #locate cell and get its value 16 | cell_value = worksheet['G11'].value 17 | #append value to "values" list 18 | values.append(cell_value) 19 | 20 | #print totals 21 | print(cell_value) 22 | -------------------------------------------------------------------------------- /Python_code/Search.py: -------------------------------------------------------------------------------- 1 | #pip3 install pandas 2 | import pandas as pd 3 | #pip3 install numpy 4 | import numpy as np 5 | 6 | #specify the files location (or path) 7 | files = ['files path here'] 8 | 9 | #loop through the "files" list 10 | for file in files: 11 | #read each file in a dataframe 12 | df = pd.read_excel(file) 13 | #locate the "Item" column and return the "Rep" where the condition matches "Pencil" 14 | pencil = df['Rep'].where(df['Item'] == 'Pencil').dropna() 15 | #print file name 16 | print(file) 17 | #print filtered results 18 | print(pencil) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Automating Excel in Python 2 | This repository contains some code snippets to automate excel in Python 3 | ![image1](images/thumbnail.png) 4 | -------------------------------------------------------------------------------- /datasets/SampleData.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DavideMerlin/Excel_Automation/97bf130e18ee874c396a400d71ace0c84230dd36/datasets/SampleData.xlsx -------------------------------------------------------------------------------- /datasets/SampleData2.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DavideMerlin/Excel_Automation/97bf130e18ee874c396a400d71ace0c84230dd36/datasets/SampleData2.xlsx -------------------------------------------------------------------------------- /images/thumbnail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DavideMerlin/Excel_Automation/97bf130e18ee874c396a400d71ace0c84230dd36/images/thumbnail.png --------------------------------------------------------------------------------