Skip to content

Commit

Permalink
Merge pull request #9 from meddlin/feature/refactor-workbooks-creation
Browse files Browse the repository at this point in the history
Refactor workbooks creation
  • Loading branch information
meddlin authored Feb 25, 2024
2 parents 2e8a60f + 82be739 commit e34ac07
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
63 changes: 34 additions & 29 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,49 +69,54 @@ 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())
abr_col_names = list(abridged[0].keys())
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)
Expand Down
4 changes: 2 additions & 2 deletions utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
"""
Expand Down

0 comments on commit e34ac07

Please sign in to comment.