specifiable column hiearachy list

This commit is contained in:
alex 2022-01-23 22:21:06 -05:00
parent 0c180a7f42
commit 79975256a6
2 changed files with 30 additions and 6 deletions

View File

@ -4,14 +4,17 @@ parser = argparse.ArgumentParser(description='Process some intzzz')
parser.add_argument('--in', '-i', dest='file_in', required=True) parser.add_argument('--in', '-i', dest='file_in', required=True)
parser.add_argument('--out', '-o') parser.add_argument('--out', '-o')
parser.add_argument('--range', '-r', dest='cell_range') parser.add_argument('--range', '-r', dest='cell_range')
parser.add_argument('column_groups', nargs='+')
args = parser.parse_args() args = parser.parse_args()
from spreadsheet import get_names_list from spreadsheet import get_names_list
names = get_names_list(args.file_in, args.cell_range) names = get_names_list(args.file_in, args.cell_range, args.column_groups)
print(names) print(names)
from directory import create_directories from directory import create_directories
create_directories(args.out, names) create_directories(args.out, names)
#print(args.column_groups)

View File

@ -41,7 +41,21 @@ def parse_range(cellrange):
return (min_row, min_col, max_row, max_col) return (min_row, min_col, max_row, max_col)
def get_names_list(filename, cellrange): def get_value(row, colrange):
cols = colrange.split(":")
indices = [custom_index(row, lambda cell: cell.column_letter == col) for col in cols]
name = " ".join([str(row[index].value) for index in indices])
return name
def custom_index(array, compare_function):
for i, v in enumerate(array):
if compare_function(v):
return i
def get_names_list(filename, cellrange, column_groups):
wb = load_workbook(filename = filename) wb = load_workbook(filename = filename)
names = [] names = []
@ -50,8 +64,15 @@ def get_names_list(filename, cellrange):
if cellrange: if cellrange:
min_row, min_col, max_row, max_col = parse_range(cellrange) min_row, min_col, max_row, max_col = parse_range(cellrange)
for row in wb.active.iter_rows(min_row=min_row, min_col=min_col, max_row=max_row, max_col=max_col, values_only=True): for row in wb.active.iter_rows(min_row=min_row, min_col=min_col, max_row=max_row, max_col=max_col):
name = " ".join([str(cell) for cell in row if cell is not None]) cells = [cell for cell in row if cell is not None]
names.append(name) paths = []
for colgroup in column_groups:
path = get_value(cells, colgroup)
paths.append(path)
return [name for name in names if name != ""] if "None" not in paths:
names.append("/".join(paths))
return names