Created
October 7, 2024 03:49
-
-
Save rmtbb/741fe56b812e8074d86b7de0562f82c8 to your computer and use it in GitHub Desktop.
Coupa Roles And Permissions Combiner Script.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| This script processes multiple CSV files containing roles and permissions data, each residing in a 'data' subfolder relative to the script's location. Each CSV file is expected to have columns labeled 'Controller', 'Action', 'Permission ID', 'Description', and 'Enabled'. The script performs the following tasks: | |
| 1. Reads each CSV file from the 'data' subfolder. | |
| 2. Renames the 'Enabled' column of each file to reflect the name of the source file, with underscores and hyphens converted to spaces, to maintain uniqueness across different files. | |
| 3. Aligns all DataFrames on the 'Controller', 'Action', 'Permission ID', and 'Description' columns, assuming these columns are consistent across files. | |
| 4. Merges all processed DataFrames horizontally, creating a master DataFrame where each 'Enabled' column is distinctly named according to its original CSV file. | |
| 5. Saves the resulting master DataFrame to an Excel file named 'Master_Permissions_File.xlsx' in the script's running directory. | |
| Usage: | |
| Ensure that the 'data' subfolder contains the CSV files to be processed and that each file is structured correctly. Run the script from the directory containing the 'data' subfolder. | |
| """ | |
| import os | |
| import pandas as pd | |
| def process_folder(folder_path): | |
| # Adjusted to read from a subfolder named 'data' | |
| all_files = [f for f in os.listdir(folder_path) if f.endswith('.csv')] | |
| df_list = [] | |
| for file in all_files: | |
| file_path = os.path.join(folder_path, file) | |
| df = pd.read_csv(file_path) | |
| # Rename 'Enabled' column based on file name | |
| new_name = file.replace('_', ' ').replace('-', ' ').replace('.csv', '') | |
| df = df.rename(columns={'Enabled': new_name}) | |
| # Set index if needed to align all DataFrames | |
| df = df.set_index(['Controller', 'Action', 'Permission ID', 'Description']) | |
| df_list.append(df) | |
| # Combine all DataFrames | |
| master_df = pd.concat(df_list, axis=1).reset_index() | |
| # Save the master file in the directory where the script is running, not in the 'data' subfolder | |
| master_df.to_excel('Master_Permissions_File.xlsx', index=False) | |
| # Path to the subfolder 'data' relative to the script's running directory | |
| folder_path = './data' | |
| process_folder(folder_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment