diff --git a/main.py b/main.py index e5f9d0b..6875e12 100644 --- a/main.py +++ b/main.py @@ -69,21 +69,27 @@ def read_csv(filename: str) -> dict[str, dict[str, str]]: return { 'full': full, 'abridged': abridged } -def create_workbooks(full: dict[str, str], abridged: dict[str, str], filter_columns: str, sheet_name: str, output: str): - """ Create workbooks and control what changes are made to them. (This is where the magic happens)""" - - # filter_columns = ['Posting Date', 'Amount', 'Description', 'Transaction Category', 'Extended Description'] - filter_columns = utility.format_filter_cols(filter_columns) - - wb = Workbook() - del wb['Sheet'] # Delete the default sheet +def create_full_workbook(workbook, worksheet, full: dict[str, str]): + """Manage creating the 'full' workbook. This one is basically a copy of the original CSV data.""" + workbook.active = worksheet + num_of_cols = len(full[0].keys()) + col_names = list(full[0].keys()) - ws1 = wb.create_sheet(sheet_name) - ws2 = wb.create_sheet('raw_data') + worksheet.append(col_names) # NOTE: Creates header row + bold_header(worksheet) # NOTE: Must execute AFTER data has been written to the header row. + full_sheet_row_counter = 0 - wb.active = ws1 - ws1.append(filter_columns) - bold_header(ws1) # NOTE: Must execute AFTER data has been written to the header row. + for f_row in full: + worksheet.append( utility.get_row(f_row, col_names) ) + full_sheet_row_counter += 1 + + autofit_columns(worksheet) + configure_filters(worksheet, full_sheet_row_counter, num_of_cols) + +def create_filtered_workbook(workbook, worksheet, abridged: dict[str, str], filter_columns: list[str]): + workbook.active = worksheet + worksheet.append(filter_columns) + bold_header(worksheet) # NOTE: Must execute AFTER data has been written to the header row. row_counter = 0 abr_num_of_cols = len(abridged[0].keys()) @@ -91,27 +97,26 @@ def create_workbooks(full: dict[str, str], abridged: dict[str, str], filter_colu for row in abridged: # TODO: Add ability to *add* columns instead of only filter what is already there. # ws1.append([row['Posting Date'], row['Amount'], row['Description'], row['Transaction Category'], row['Extended Description']]) - ws1.append( utility.get_row_filtered(row, abr_col_names, filter_columns) ) + worksheet.append( utility.get_row_filtered(row, abr_col_names, filter_columns) ) row_counter += 1 - autofit_columns(ws1) - configure_filters(ws1, row_counter, abr_num_of_cols) + autofit_columns(worksheet) + configure_filters(worksheet, row_counter, abr_num_of_cols) - - wb.active = ws2 - num_of_cols = len(full[0].keys()) - col_names = list(full[0].keys()) +def create_workbooks(full: dict[str, str], abridged: dict[str, str], filter_columns: str, sheet_name: str, output: str): + """Create workbooks and control what changes are made to them. (This is where the magic happens)""" - ws2.append(col_names) # NOTE: Creates header row - bold_header(ws2) # NOTE: Must execute AFTER data has been written to the header row. - full_sheet_row_counter = 0 - - for f_row in full: - ws2.append( utility.get_row(f_row, col_names) ) - full_sheet_row_counter += 1 + # filter_columns = ['Posting Date', 'Amount', 'Description', 'Transaction Category', 'Extended Description'] + filter_columns = utility.format_filter_cols(filter_columns) + + wb = Workbook() + del wb['Sheet'] # Delete the default sheet + + ws1 = wb.create_sheet(sheet_name) + ws2 = wb.create_sheet('raw_data') - autofit_columns(ws2) - configure_filters(ws2, full_sheet_row_counter, num_of_cols) + create_filtered_workbook(workbook=wb, worksheet=ws1, abridged=abridged, filter_columns=filter_columns) + create_full_workbook(workbook=wb, worksheet=ws2, full=full) set_zoom_scale(wb) wb.save(output) diff --git a/utility.py b/utility.py index 25ec43a..e0fcddb 100644 --- a/utility.py +++ b/utility.py @@ -7,7 +7,7 @@ def get_row(row: dict[str, str], column_names: list[str]): return data def get_row_filtered(row: dict[str, str], column_names: list[str], filter_columns: list[str]): - """ """ + """""" data = [] for col in column_names: @@ -17,7 +17,7 @@ def get_row_filtered(row: dict[str, str], column_names: list[str], filter_column return data def format_filter_cols(filter_cs_list: str): - """ Separate filter columns. + """Separate filter columns. Remove leading and trailiing whitespace from filter columns. Return as a list. """